1

I have a large array in C# that I would like to pass to Python for processing. Instead of sending the values of the array, I would like to send a pointer and have Python access it that way. Here is my C# code:

NamedPipeServerStream server = null;
BinaryReader br = null;
BinaryWriter bw = null;

Thread thread = new Thread(() =>
    {
        server = new NamedPipeServerStream("NPtest");

        Console.WriteLine("Waiting for connection...");
        server.WaitForConnection();

        Console.WriteLine("Connected.");
        br = new BinaryReader(server);
        bw = new BinaryWriter(server);
    })
{ IsBackground = true };
thread.Start();

Process process = new Process() { StartInfo = new ProcessStartInfo("python.exe", "script.py") { UseShellExecute = false } };
process.Start();
Thread.Sleep(1000);

byte[] bytes = new byte[] { 32, 164, 9 };

IntPtr pointer = Marshal.AllocHGlobal(bytes.Length);
Marshal.Copy(bytes, 0, pointer, bytes.Length);
bw.Write(bytes.Length);
bw.Write(pointer.ToInt64());
Console.ReadKey(true);
Marshal.FreeHGlobal(pointer);

server.Close();
server.Dispose();

And here is script.py:

import struct
import ctypes
import numpy

f = open(r'\\.\pipe\NPtest', 'r+b', 0)

length = struct.unpack('i', f.read(4))[0]
f.seek(0)
print(length)
pointer = struct.unpack('q', f.read(8))[0]
f.seek(0)
print(hex(pointer))
data = numpy.ctypeslib.as_array(ctypes.cast(pointer, ctypes.POINTER(ctypes.c_ubyte * length)), shape=(length, ))
print(data)

When I get to the second to last line of the Python, it crashes without an error. I'm guessing it has to do with Python not being able to access that memory. Is there any way around this? Or is there some other architecture where I could pass a large array from C# to Python so processing can be performed?

2
  • It's unclear what you think should be happening when you pass pointer across process boundary... You may be interested in this search results bing.com/search?q=c%23+shared+memory+python instead... Commented Mar 25, 2019 at 20:22
  • @AlexeiLevenkov I was hoping to read (but not change) the data in Python via the pointer, but perhaps that is misguided. Commented Mar 27, 2019 at 16:31

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.