P Invoke struct structure, invokestruct Structure
1. Obtain Struct
CHCNetSDK. NET_DVR_PTZPOS pos = new CameraTest. CHCNetSDK. NET_DVR_PTZPOS (); int size = Marshal. sizeOf (typeof (CHCNetSDK. NET_DVR_PTZPOS); IntPtr = Marshal. allocHGlobal (size); // apply for memory uint bytesReturnd = 0; try {if (! CHCNetSDK. NET_DVR_GetDVRConfig (userId, CHCNetSDK. NET_DVR_GET_PTZPOS, Model. invalid Data. IPCChannel, ptr, (uint) size, ref bytesReturnd) {IPCHelper. logErrr (nameof (CHCNetSDK. NET_DVR_GetDVRConfig); this. results = ConstInfos. INVOKE_SDK_ERROR; return;} pos = (CHCNetSDK. NET_DVR_PTZPOS) Marshal. ptrToStructure (ptr, typeof (CHCNetSDK. NET_DVR_PTZPOS); // pointer to structure} finally {Marshal. freeHGlobal (ptr); // release pointer}
Ii. Transmit Struct
// Set the PTZ parameter CHCNetSDK. NET_DVR_PTZPOS pos = new CameraTest. CHCNetSDK. NET_DVR_PTZPOS () {wAction = (ushort) action, wPanPos = GetSetPTZ (rectArgs. x), wTiltPos = GetSetPTZ (rectArgs. y), wZoomPos = GetSetPTZ (rectArgs. width)}; LogHelper. action ("SET_PTZ, InterAction value: {0}, P: {1}, T: {2}, Z: {3}", pos. wAction, pos. wPanPos, pos. wTiltPos, pos. wZoomPos); int size = Marshal. sizeOf (typeof (CHCNetSDK. NET_DVR_PTZPOS )); IntPtr ptr = Marshal. AllocHGlobal (size); // apply for memory Marshal. StructureToPtr (pos, ptr, true); // pass data to the pointer try {if (! CHCNetSDK. NET_DVR_SetDVRConfig (userId, CHCNetSDK. NET_DVR_SET_PTZPOS, Model. invalid Data. IPCChannel, ptr, (uint) size) {IPCHelper. logErrr (nameof (CHCNetSDK. NET_DVR_SetDVRConfig); this. results = ConstInfos. INVOKE_SDK_ERROR; return ;}} finally {Marshal. freeHGlobal (ptr); // release pointer}
Iii. Correspondence between C ++ type and C # type
// 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 #: System. Int16
// C ++: INT (int) ---- c #: System. Int32
// C ++: UINT (unsigned int) ---- c #: System. UInt16
// C ++: UINT (unsigned int) ---- c #: System. UInt32
// C ++: LONG (long) ---- c #: System. Int32
// C ++: ULONG (unsigned long) ---- c #: System. UInt32
// 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. String
// C ++: LPWSTR (wchar_t *) ---- c #: System. String
// C ++: LPCSTR (const char *) ---- c #: System. String
// C ++: LPCWSTR (const wchar_t *) ---- c #: System. String
// C ++: PCAHR (char *) ---- c #: System. String
// C ++: BSTR ---- c #: System. String
// C ++: FLOAT (float) ---- c #: System. Single
// C ++: DOUBLE (double) ---- c #: System. Double
// C ++: VARIANT ---- c #: System. Object
// C ++: PBYTE (byte *) ---- c #: System. Byte []
// C ++: BSTR ---- c #: StringBuilder
// C ++: LPCTSTR ---- c #: StringBuilder
// C ++: LPCTSTR ---- c #: string
// C ++: LPTSTR ---- c #: [financialas (UnmanagedType. LPTStr)] string
// C ++: LPTSTR output variable name ---- c #: StringBuilder output variable name
// C ++: LPCWSTR ---- c #: IntPtr
// C ++: BOOL ---- c #: bool
// C ++: HMODULE ---- c #: IntPtr
// C ++: HINSTANCE ---- c #: IntPtr
// C ++: struct ---- c #: public struct {};
// C ++: struct ** variable name ---- c #: out variable name // C # declare the variable name after the struct is instantiated in advance
// 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 #: string
// C ++: UCHAR * ---- c #: IntPtr
// C ++: GUID ---- c #: Guid
// C ++: Handle ---- c #: IntPtr
// C ++: HWND ---- c #: IntPtr
// C ++: DWORD ---- c #: int
// C ++: COLORREF ---- c #: uint
// C ++: unsigned char ---- c #: byte
// C ++: unsigned char * ---- c #: ref byte
// C ++: unsigned char * ---- c #: [financialas (UnmanagedType. LPArray)] byte []
// C ++: unsigned char * ---- c #: [financialas (UnmanagedType. LPArray)] Intptr
// C ++: unsigned char & ---- c #: ref byte
// C ++: unsigned char variable name ---- c #: byte variable name
// C ++: unsigned short variable name ---- c #: ushort variable name
// C ++: unsigned int variable name ---- c #: uint variable name
// C ++: unsigned long variable name ---- c #: ulong variable name
// C ++: char variable name ---- c #: byte variable name // one character in C ++ is represented by one byte, and one character in C # is represented by two bytes.
// C ++: char array name [array size] ---- c #: financialas (UnmanagedType. ByValTStr, SizeConst = array size)] public string Array name; ushort
// C ++: char * ---- c #: string // input parameters
// C ++: char * ---- c #: StringBuilder // output parameters
// C ++: char * 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 #: string
// C ++: char ** variable name ---- c #: ref string variable name
// C ++: const char * ---- c #: string
// C ++: char [] ---- c #: string
// C ++: char variable name [array size] ---- c #: [financialas (UnmanagedType. ByValTStr, SizeConst = array size)] public string variable name;
// C ++: struct name * variable name ---- c #: ref struct name variable name
// C ++: Delegate variable name ---- c #: Delegate variable name
// C ++: int ---- c #: int
// C ++: int ---- c #: ref int
// C ++: int & ---- c #: ref int
// C ++: int * ---- c #: ref int // C # The int variable name = 0 must be defined before the call;
// 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 // The byte variable name = new byte () needs to be defined before calling in ref byte // C ();
// 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 # :( [externalas (UnmanagedType. AsAny)] Object Name
// C ++: char, INT8, SBYTE, CHAR ---- c #: System. SByte
// C ++: short, short int, INT16, SHORT ---- c #: System. Int16
// C ++: int, long, long int, INT32, LONG32, BOOL, INT ---- c #: System. Int32
// C ++ :__ int64, INT64, LONGLONG ---- c #: System. Int64
// C ++: unsigned char, UINT8, UCHAR, BYTE ---- c #: System. Byte
// C ++: unsigned short, UINT16, USHORT, WORD, ATOM, WCHAR, _ wchar_t ---- c #: System. 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
Iv. MSDN references
Https://msdn.microsoft.com/library/aa288468 (v = vs.71). aspx
Https://msdn.microsoft.com/library/aa645736 (v = vs.71). aspx