Assuming your native call is something like unsigned char* InitDef(unsigned char*, long long &baudRate)
then you probably want to use IntPtr
in the place of your unsigned char*
. But first since it's managed/unmanaged mixing you should allocate that byte[]
buffer for input and output :
byte[] pInBuffer = Encoding.ASCII.GetBytes("COM3");
// allocate unmanaged memory
IntPtr inputBuffer = Marshal.AllocHGlobal(pInBuffer.Length * sizeof(byte));
Marshal.Copy(pInBuffer, 0, inputBuffer, pInBuffer.Length);
// now your inputBuffer contains a native pointer to the `unsigned char*`
// and since your function returns a new pointer to some `unsigned char*`
// just retrieve it to IntPtr
IntPtr result = InitDev(inputBuffer, 9600);
// free your allocated memory
Marshal.FreeHGlobal(inputBuffer);
At this stage your result
contains the value returned from InitDev
.
new definition of InitDev
function
[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("*.dll", CallingConvention = CallingConvention.Cdecl,CharSet =CharSet.Auto)]
public static extern IntPtr InitDev(IntPtr comport, [Out] long BaudRate);
All of the necessary calls, you can find on this msdn page
EDIT :
Since your native call looks like this :extern unsigned char __stdcall InitDev(unsigned char comport,long BaudRate);
I'm guessing that the first parameter is just the last digit ( index ) of your "COMn"
because unsigned char
's lowest possible value is 0 and COM's lowest index can be 0.
Try using this snippet :
[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("*.dll", CallingConvention = CallingConvention.Cdecl,CharSet =CharSet.Auto)]
public static extern byte InitDev(byte comport, long BaudRate);
And then call it like so :
byte comIdx = 3;
byte result = InitDev(comIdx, 9600);
// byte should yield correct result now