c# - How can I pass MemoryStream data to unmanaged C++ DLL using P/Invoke -
I need your help with the following scenario:
I have some data from hardware in MemoryStream I am reading (C #) and in memory I have to use DLL (pointers ??) data implemented in unmanaged C ++ data (in the stream) very large (megabytes) I understand that I can invoke this Dell P / but I am not sure how to pass the indicators / references to the stream data for C ++ API?
I have to admit that I am new to C # because I want to use unsafe / unsafe because the data is large or is the inimitable because the memorystream object is managed by GC? Some example code / detailed descriptions will be very useful.
Itemprop = "Text">
If it is just expecting bytes, then you can read memorystream in a byte array and then pass a pointer by method.
You must declare an external method:
[DllImport ("mylibrary.dll", chartset = charset.auto)] Public static extern bool doSomething (IntPtr rawData, int DataLength);
Then, read the bytes from MemoryStream in a byte array. Assign all that:
Once allocated, you can use GCHandle to prevent managed object from being collected by a garbage collector, when an unmanaged client only holds the reference. Without such a handle, the garbage collector can be collected before the unmanaged client completes his work.
And finally, use the AddrOfPinnedObject method to get an IntPtr to pass a C ++ DLL
Private zero call TheMethod (MemoryStream MemStream) {byte [] rawData = new byte [memStream.Length]; Memstream.Read (rawData, 0, memStream.Length); GCandal RawDatahandal = GCIndale. Eloc (raw data, pinched GC & Dell type); IntPtr Address = Handle AddrOfPinnedObject (); DoSomething (address, rawData.Length); RawDataHandle.Free (); }
Comments
Post a Comment