Function calls cause stack asymmetry. The reason may be that the managed PInvoke signature does not match the unmanaged target signature. Just add the CallingConvention parameter to the DLlimPort,

[DllImport(PCAP_DLL, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]



Note the correspondence between C++ and NET data types:

/ / c + + : char * – – c # : the string / / / / c + + : incoming parameters char * – – c # : StringBuilder / / / c + + / out parameters: char * the variable name – – c # : ref string variable name / / c + + : char * input variable name – – c#:string input variable name //c++:char * output variable name – – c#:[MarshalAs(unmanagedtype.lpstr)] StringBuilder output variable name / / c + + : SHORT (SHORT) – – c # : System. Int16 / / c + + : LONG (LONG) – c # : System. Int32

Reprint collection:

// C++ :HANDLE(void *) — – C# : system.intptr // C++ :Byte(unsigned char) — – C # : System. Byte / / c + + : SHORT (SHORT) – – c # : System. Int16 / / c + + : WORD (unsigned SHORT) – c # : System. UInt16 / / c + + : INT – (INT) //c++:INT(INT) — c#: unsigned INT — UInt16 //c++: unsigned INT – – c#:System.UInt32 //c++:LONG(LONG) – – c#: system.int32 //c++:ULONG(unsigned LONG) – – c#:System //c++:DWORD(unsigned long) — – c#: system.uint32 //c++:DECIMAL — – c#: system.decimal //c++:BOOL(long) — c#: system.boolean / / c + + : CHAR (CHAR) – – c # : System. CHAR / / c + + : LPSTR (CHAR *) – c # : System. The String / / c + + : LPWSTR (would be *) – c # : System. String / / c + + : LPCSTR (const char *) – – c # : System. The String / / c + + : LPCWSTR (const would *) – c # : System. The String / / c + + : PCAHR – (char *) C # : System. The String / / c + + : BSTR – c # : System. The String / / c + + : FLOAT (FLOAT) – – c # : System. Single / / c + + : DOUBLE (DOUBLE) — – C # : System. Double / / c + + : the VARIANT – c # : System. The Object / / c + + : PBYTE (byte *) – c # : System. Byte []

/ / c + + : BSTR – – c # : StringBuilder / / c + + : LPCTSTR – c # : StringBuilder / / c + + : LPCTSTR – c # : the string / / c + + : LPTSTR – – C# :[MarshalAs(unmanagedtype.lptstr)] string c++: LPTStr – – c#: LPCWSTR – – c#:IntPtr / / c + + : BOOL – – c # : BOOL / / c + + : HMODULE – c # : IntPtr / / c + + : HINSTANCE – c # : IntPtr / / c + + : structure — – c # : public struct structure {}; //c++: struct ** variable name — -c #:out variable name // c#: struct & variable name — -c #:ref struct variable name

/ / c + + : WORD – – c # : ushort / / c + + : DWORD – c # : uint / / c + + : DWORD – c # : int

/ / c + + : UCHAR – – c # : int / / c + + : UCHAR – c # : byte / / c + + : UCHAR * – – c # : the string / / c + + : UCHAR * – – c # : IntPtr

/ / c + + : GUID – – c # : GUID / / c + + : Handle – c # : IntPtr / / c + + : HWND – c # : IntPtr / / c + + : DWORD – c # : int / / c + + : a COLORREF – c # : uint

//c++:unsigned char — – c#:ref byte //c++:unsigned char * — – c#:ref byte C# :[unmanagedtype.lparray] byte[] //c++:unsigned char * — c#:[unmanagedtype.lparray] Intptr

//c++:unsigned char & – – c#:ref byte //c++:unsigned char name – – c#:byte name //c++:unsigned short name – – c#:ushort name //c++:unsigned int name – – c#:uint name – /c++:unsigned long name – – c#:ulong name

//c++:char variable name – – c#:byte variable name // in c++ a character is one byte, in c# a character is two bytes //c++:char array name [array size] – – C# :MarshalAs(unmanagedtype. ByValTStr, SizeConst = SizeConst)] public string array name; ushort

/ / c + + : char * – – c # : the string / / / / c + + : incoming parameters char * – – c # : StringBuilder / / / c + + / out parameters: char * the variable name – – c # : ref string variable name / / c + + : char * input variable name – – c#:string input variable name //c++:char * output variable name – – c#:[MarshalAs(unmanagedtype.lpstr)] StringBuilder output variable name

/ / c + + : char * * — – c # : the string / / c + + : char * * the variable name – – c # : ref string variable name / / c + + : const char * – – c # : the string / / c + + : char [] – – C # : the string / / c + + : char variable names [the array size] – c # : [MarshalAs (UnmanagedType ByValTStr, SizeConst = array size)] public string variable name;

//c++:struct * struct — c#:ref — c#: delegate — c#: delegate

/ / c + + : int – – c # : int / / c + + : int – c # : ref int / / c + + : int & – c # : ref int / / c + + : int * – – c # : ref int / / in c # to be defined before invoking the int variable name = 0;

/ / c + + : * int – c # : IntPtr / / c + + : int32 PIPTR * – – c # : int32 [] / / c + + : float PIPTR * – – c # : float []

/ / c + + : double array name – – c # : * * ref double array name / / c + + : double * [] array name – – c # : ref double array name / / c + + : long – c # : int / / c + + : ulong – – c#:int

//c++:UINT8 * — c#:ref byte // UINT8 = new byte();

//c++:handle — – c#:IntPtr //c++: HWND — – c#:IntPtr

/ / c + + : void * – – c # : IntPtr / / c + + : void * user_obj_param – c # : IntPtr user_obj_param / / c + + : void * object name – – C# :([MarshalAs(unmanagedtype.asany)]Object name

//c++:short, short int, INT16, short — c#: system.int16 //c++:int, Long, long int, INT32, LONG32, BOOL, int — c#: INT32 //c++:__int64, INT64, //c++:unsigned char, UINT8, uint3, UInt16 //c++:unsigned, unsigned int, UINT32, ULONG32, DWORD32, ULONG, DWORD, UINT — c#:System.UInt32 //c++:unsigned __int64, UINT64, DWORDLONG, ULONGLONG — – c#:System.UInt64 //c++:float, float — c#:System.Single //c++:double, long double, DOUBLE – c # : System. DOUBLE

//Win32 Types — -clR Type

Delegate static extern int FunCallBack(string STR);

//unsigned char** ppImage = IntPtr ppImage //int& nWidth = ref int nWidth //int*, int&, C++ : typedef double (*fun_type1)(double); Corresponding c#:public delegate double fun_type1(double); C++ : char*; The corresponding c # : StringBuilder; // using Pointers in c# : place Pointers where unsafe is required

Public byte /* * typedef void (*CALLBACKFUN1W)(wchar_t*, void* pArg); * typedef void (*CALLBACKFUN1A)(char*, void* pArg); * bool BIOPRINT_SENSOR_API dllFun1(CALLBACKFUN1 pCallbackFun1, void* pArg); * [UnmanagedFunctionPointer(CallingConvention.Cdecl)] * public delegate void CallbackFunc1([MarshalAs(UnmanagedType.LPWStr)] StringBuilder strName, IntPtr pArg); * * * /


Scan the QR code to subscribe to my wechat official account: