C # Some Understanding of calling the c ++ dynamic library,
Calling the c ++ dynamic library is generally written in this way.
[DllImport ("UCamer. dll ", CallingConvention = CallingConvention. winapi)] public extern static void Disp_Destroy (IntPtr hShow); the first parameter of DllImport UCamer. dll is the path of the dynamic library dll, which is placed in the root directory of the program running or c: in windows/sytem32, The CallingConvention parameter is c #. Calling c ++ is an enumeration. msdn explains the Cdecl caller to clear the stack. This allows you to call functions with varargs (such as Printf) so that they can be used to accept variable parameters. FastCall does not support this call convention. StdCall is called to clear the stack. This is the default convention for using platform invoke to call unmanaged functions. The first parameter of ThisCall is the this pointer, which is stored in the register ECX. Other parameters are pushed to the stack. This call Convention is used to call methods of Classes exported from unmanaged DLL. The Winapi member is not a call convention, but a default platform call convention. For example, in Windows, the default value is StdCall, and in Windows CE. NET, the default value is Cdecl. From the above perspective, the Winapi method automatically selects the call protocol according to the system. ThisCall is a method for Calling c ++ classes. In general, we can choose Winapi. C # Another difficulty in calling dll: data type conversion http://wenku.baidu.com/link?url=SihlxtHC-HMcEhq3izpd2bux8rNaKOMTpu8NPqjdYlLSwYSV1CqNJdVbxkaZm7OqaaSTEK-KUJqX5jbtkdpnUZ_38No4tsrgqCsf7Th5dqK Baidu Library summarized the corresponding data types of c ++ and c # in this article. But why do we need to talk about it here? 1. Baidu Library, in most of DU Niang's data type conversion, char [] of the return value type in c ++ is converted to char [] without any changes. In c ++, char occupies a byte, Which is assic encoded. Char in c # occupies 2 bytes (I tested it in the Chinese version of ). In this case, the conversion may cause problems, such as out-of-bounds or read/write to protected memory. The solution is to convert char [] to byte [] in c #, and then convert it using the Encoding. Assic. getstring method. 2. About pointers, all pointers in c ++ use Intptr on c. If the returned Intptr is an array pointer, how do we read the elements in the array in c? Marshal. Copy (); Marshal class is c # dedicated to converting unmanaged memory into managed memory artifacts without unsafe. The copy method in Marshal is the most common method. There are 16 reloads. It can solve most of the problems you have encountered. The following names are explained for each overload in msdn: the static members of the public method are supported by Silverlight for Windows Phone. The Copy (Byte [], Int32, IntPtr, Int32) security key is supported by Xbox 360. Copy the data in a one-dimensional hosted 8-bit unsigned integer array to the unmanaged memory pointer. Static members of public methods are supported by Silverlight for Windows Phone and supported by Xbox 360. Copy (Char [], Int32, IntPtr, Int32) is critical to security. Copy data from a one-dimensional hosted character array to an unmanaged memory pointer. Public Methods static members are supported by Silverlight for Windows Phone. Xbox 360 supports Copy (Double [], Int32, IntPtr, Int32) Security. Copy data from a one-dimensional hosted double-precision floating point group to an unmanaged memory pointer. Static members of public methods are supported by Silverlight for Windows Phone and supported by Xbox 360. Copy (Int16 [], Int32, IntPtr, Int32) is critical to security. Copy the data in a one-dimensional hosted 16-bit signed integer array to an unmanaged memory pointer. Static members of the public method are supported by Silverlight for Windows Phone and supported by Xbox 360. Copy (Int32 [], Int32, IntPtr, Int32) is critical to security. Copy data from a one-dimensional hosted 32-bit signed integer array to an unmanaged memory pointer. Static members of the public method are supported by Silverlight for Windows Phone and supported by Xbox 360. Copy (Int64 [], Int32, IntPtr, Int32) is critical to security. Copy the data in a one-dimensional hosted 64-bit signed integer array to an unmanaged memory pointer. Static members of public methods are supported by Silverlight for Windows Phone and supported by Xbox 360. Copy (IntPtr, Byte [], Int32, Int32) is critical to security. Copy data from an unmanaged memory pointer to a hosted 8-bit unsigned integer array. Static members of the public method are supported by Silverlight for Windows Phone and supported by Xbox 360. Copy (IntPtr, Char [], Int32, Int32) is critical to security. Copy data from an unmanaged memory pointer to a hosted character array. Static members of public methods are supported by Silverlight for Windows Phone and supported by Xbox 360. Copy (IntPtr, Double [], Int32, Int32) is critical to security. Copy data from an unmanaged memory pointer to a hosted double-precision floating point group. Static members of public methods are supported by Silverlight for Windows Phone and supported by Xbox 360. Copy (IntPtr, Int16 [], Int32, Int32) is critical to security. Copy the data in the unmanaged memory pointer to the managed 16-bit signed integer array. Static members of the public method are supported by Silverlight for Windows Phone and supported by Xbox 360. Copy (IntPtr, Int32 [], Int32, Int32) is critical to security. Copy the data in the unmanaged memory pointer to the hosted 32-bit signed integer array. Static members of public methods are supported by Silverlight for Windows Phone and supported by Xbox 360. Copy (IntPtr, Int64 [], Int32, Int32) is critical to security. Copy data from an unmanaged memory pointer to a hosted 64-bit signed integer array. Static members of public methods are supported by Silverlight for Windows Phone and supported by Xbox 360. Copy (IntPtr, Single [], Int32, Int32) is critical to security. Copy data from an unmanaged memory pointer to a hosted Single-precision floating point group. Static members of the public method are supported by Silverlight for Windows Phone and supported by Xbox 360. Copy (Single [], Int32, IntPtr, Int32) is critical to security. Copy data from a one-dimensional hosted Single-precision floating point group to an unmanaged memory pointer. We can also use shared memory for operations. Some codes are as follows: copy the code // initialize the returned image size and other information. myContext = new VlcControlWpfRendererContext (width, height, System. windows. media. pixelFormats. bgr24); // create the shared memory region myBitmapSectionPointer = Win32Interop. createFileMapping (new IntPtr (-1), IntPtr. zero, Win32Interop. pageAccess. readWrite, 0, myContext. size, null); // obtain the first address of the shared memory map = Win32Interop. mapViewOfFile (myBitmapSectionPointer, Win32Interop. fileMapAccess. allAccess, 0, 0, (uint) MyContext. size); // swap the received image to the Win32Interop in the shared memory area. copyMemory (map, data, myContext. size); // convert the array in the shared memory to the image myBitmap = (InteropBitmap) Imaging. createBitmapSourceFromMemorySection (myBitmapSectionPointer, myContext. width, myContext. height, myContext. pixelFormat, myContext. stride, 0); copy the code here is a helper class for shared memory, mainly some api functions. In fact, Intptr is essentially an Int, and Int is the same in c # And Int32. So basically the pointer, Long and int are all int in c. This is just to help you know the type in c ++ and facilitate conversion. 3. The function pointer in c ++ and the delegate in c # Are the typedef VOID (WINAPI * PUSERCALL) (PUCHAR pData, ULONG Length, PVOID pUserData) defined in c ++ ); the example in c # is as follows: public delegate void PUSERCALL (IntPtr pData, uint Length, UInt32 pUserData); 4. We know that int occupies 4 bytes. Below is a c ++ method U_CAMER long winapi CAMER_GetPropery (HANDLE hCamer, _ CMRCTL Propery ); suppose we translate this function into the following function in c # [DllImport ("UCamer. dll ", CallingConvention = CallingConvention. winapi)] public extern static Uint16 CAMER_GetPropery (IntPtr hCamer, CMRCTL Propery); we call this method in c # uint16 m_HiWi_temp = (uint) BCamera. CAMER_GetPropery (m_hCamer, CMRCTL. OUT_SIZE); found a very interesting problem, the call here is no problem. It also has a value, but it is two bytes of the four bytes in int32. Let's look at the original c ++ call to this function * (PULONG) m_HiWi) = * (PULONG) m_Display) = CAMER_GetPropery (m_hCamer, OUT_SIZE); m_hShow = Disp_Create (m_hWnd, m_HiWi [1], m_HiWi [0], m_nColor, (USERDRAW) (m_ReDrawLine = TRUE )? DrawLine: NULL), this); originally, the CAMER_GetPropery function returns only one long type. Through pointer conversion in c ++. Converts the long type to pulong, which is also an array of ulong. How can we call it in c #? int m_HiWi_temp = BCamera. CAMER_GetPropery (m_hCamer, CMRCTL. OUT_SIZE); byte [] m_byte_HiWi = BitConverter. getBytes (m_HiWi_temp); byte [] temp1 = new byte [2] {m_byte_HiWi [0], m_byte_HiWi [1]}; byte [] temp2 = new byte [2] {m_byte_HiWi [2], m_byte_HiWi [3]}; int width = BitConverter. toInt16 (temp1, 0); int high = BitConverter. toInt16 (temp2, 0); in this example, c ++ sometimes returns an int type, but in c ++, you can easily set the int type The pointer can be easily converted into two uint16 arrays. So we must pay attention to the conversion in c. Summary: in fact, the conversion of data types mainly refers to the conversion of data storage space. How much space is occupied by the Data Types in c ++, you only need to convert it to the data type that occupies the same space in c. It's just c #. It's easier to see which data type is used for corresponding operations.