B
Beorne
I have to pass a byte array as an input parameter to a function in a
propertary dll.
The c++ signature of the original function is the following:
---------- C++ ----------
int ncWrite(unsigned long ObjHandle, unsigned long DataSize, void*
DataPtr)
---------- end C++ ----------
---------- C# ----------
[DllImport("Nican.dll")]
unsafe static extern int ncWrite(uint ObjHandle, uint DataSize,
IntPtr DataPtr);
---------- end C# ----------
And I use it as:
---------- C# ----------
byte[] DataWrite = {0x23, 0x23, 0x30, 0x03, 0x78, 0xEC, 0xFF, 0xFF };
int myStatus;
IntPtr ptr = Marshal.AllocHGlobal(8);
Marshal.Copy(DataWrite, 0, ptr, 8);
myStatus = ncWrite(WriteAddress, 8 , ptr);
Marshal.FreeHGlobal(ptr);
---------- end C# ----------
This works, but having to handle unmanaged code I''m worried by memory
leaks (this function is called thousdands of times per minute).
Is it the usage correct? Have I choosen the right Marshal methods
(Marshal.AllocHGlobal, Marshal.Copy, Marshal.FreeHGlobal)?
Thank you very much
propertary dll.
The c++ signature of the original function is the following:
---------- C++ ----------
int ncWrite(unsigned long ObjHandle, unsigned long DataSize, void*
DataPtr)
---------- end C++ ----------
From C# I have defined this function as:
---------- C# ----------
[DllImport("Nican.dll")]
unsafe static extern int ncWrite(uint ObjHandle, uint DataSize,
IntPtr DataPtr);
---------- end C# ----------
And I use it as:
---------- C# ----------
byte[] DataWrite = {0x23, 0x23, 0x30, 0x03, 0x78, 0xEC, 0xFF, 0xFF };
int myStatus;
IntPtr ptr = Marshal.AllocHGlobal(8);
Marshal.Copy(DataWrite, 0, ptr, 8);
myStatus = ncWrite(WriteAddress, 8 , ptr);
Marshal.FreeHGlobal(ptr);
---------- end C# ----------
This works, but having to handle unmanaged code I''m worried by memory
leaks (this function is called thousdands of times per minute).
Is it the usage correct? Have I choosen the right Marshal methods
(Marshal.AllocHGlobal, Marshal.Copy, Marshal.FreeHGlobal)?
Thank you very much