Error occurred after converting C++ DLL to C#

Viewed 42

I tried working after converting a C++ DLL to use it in C# The hardware works through the dll, but the program closes as soon as it works.

C++ DLL code

int __stdcall K720_SendCmd(HANDLE ComHandle, unsigned char MacAddr, char *p_Cmd, int CmdLen, char *RecordInfo);

I converted this code to C# code like this

private string mDevicePort;
private IntPtr ComHandle;
private const byte MacAddr = 0;

[DllImport(@".C++_Dll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
unsafe public static extern int SendCmd(IntPtr ComHandle, byte MacAddr, string cmd, int cmdLen, StringBuilder RecordInfo);

[DllImport(@".C++_Dll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
unsafe public static extern int CommOpen(string Port);

unsafe public override int OpenDevice()
{
  int iReturn;
  iReturn = CommOpen(mDevicePort);
  IntPtr IntPtr = new IntPtr(iReturn);
  ComHandle = IntPtr;
  return iReturn;
}

unsafe public override int InitDevice()
{

   int iReturn;
   string recordInfo = "";
   StringBuilder RecordInfo = new StringBuilder(recordInfo);
   iReturn = SendCmd(ComHandle, MacAddr, "RS", 2, RecordInfo);
   return iReturn;
}

When the InitDevice function is executed, a signal is generated to the hardware and works, but the program dies as it is.

I got an error report like IntPtr CallWindowProc(IntPtr, IntPtr, Int32, IntPtr, IntPtr) @ MS.Win32.UnsafeNativeMethods,
System.AccessViolationException,
but I'm not sure what the problem is

Can anyone help?

in ComHandle open comhandle
in MacAddr machine address, effective val
in *p_Cmd Save command character string
in CmdLen Order character length
out RecrodInfo Save communication record of this command

In the c++ documentation, RecordInfo is out, so I used StringBuilder because I thought that I usually get a StringBuilder in C#.

0 Answers
Related