C # calls for some understanding of C + + dynamic libraries

Source: Internet
Author: User
Tags silverlight

Call C + + Dynamic Library We write like this.

[DllImport ("UCamer.dll", callingconvention = callingconvention.winapi)]public extern static void Disp_destroy (INTPTR Hshow) The first parameter of the;D Llimport is the path to the dynamic library DLL, which is placed in the root directory of the program run or under C:windows/sytem32 callingconvention parameter is the way C # calls C + + is an enumeration of MSDN explanations as follows CdeclThe caller cleans up the stack. This enables you to call a function with varargs (such as Printf) so that it can be used to accept a variable number of arguments. FastcallThis calling convention is not supported. StdCallThe callee cleans up the stack. This is the default convention for calling unmanaged functions using platform invoke. ThisCallThe first parameter is the this pointer, which is stored in the register ECX. Other parameters are pushed onto the stack. This calling convention is used to call methods on classes exported from unmanaged DLLs. WinapiThis member is not actually a calling convention, but instead uses the default platform calling convention. For example, the default is stdcall on Windows, and CDECL is the default on Windows Ce.net. From the above, the WINAPI Way is based onsystemAutomatically selects the calling protocol. Instead, ThisCall is a method of calling a C + + class.  So in general we can choose WINAPI. C # Calling DLL Another difficulty: data type conversion http://wenku.baidu.com/link?url= Sihlxthc-hmcehq3izpd2bux8rnakomtpu8npqjdyllswysv1cqnjdvbxkazm7oqaastek-kujqx5jbtkdpnuz_38no4tsrgqcsf7th5dqk Baidu Library This article basically summed up the corresponding data types of C + + and C #.   But why do we have to say it here? 1, Baidu Library This article, including most of the Niang type conversion data in C + + in the return value type char[] all converted to char[] did not make any changes. In C + +, char occupies one byte, assic encoded.   Char in C # accounts for 2 bytes (I tested it in the Chinese version of VS).   If this translates into problems, it is easy to cross-border, or read and write to protected memory, and so on. The solution is to turn char[] into C # byte[] and then transform it with the Encoding.Assic.getstring method. 2, about pointers, C + + all pointers in C # with IntPtr, the problem comes again.   If the returned IntPtr is a pointer to a number of arrays, how do we read the elements inside the array in C #?   Marshal.Copy (); The Marshal class is an artifact in C # that specifically converts unmanaged memory into managed memory, without the need for unsafe. The copy method in Marshal is the most commonly used method. There are 16 overloads inside. Can solve most of the problems you can meet at the moment. The following names are explained for each overload in MSDNDescriptionPublic method static members are supported by Silverlight for Windows Phone support by Xbox 360Copy (byte[], Int32, INTPTR, Int32) security key. Copies data from a one-dimensional, managed 8-bit unsigned integer array to an unmanaged memory pointer. Public method static members are supported by Silverlight for Windows Phone support by Xbox 360Copy (char[], Int32, INTPTR, Int32) security key. Copies data from a one-dimensional, managed character array to an unmanaged memory pointer. Public method static members are supported by Silverlight for Windows Phone support by Xbox 360Copy (double[], Int32, INTPTR, Int32) security key. Copies data from a one-dimensional, managed double-precision floating-point array to an unmanaged memory pointer. Public method static members are supported by Silverlight for Windows Phone support by Xbox 360Copy (int16[], Int32, INTPTR, Int32) security key. Copies data from a one-dimensional, managed 16-bit signed integer array to an unmanaged memory pointer. Public method static members are supported by Silverlight for Windows Phone support by Xbox 360Copy (int32[], Int32, INTPTR, Int32) security key. Copies data from a one-dimensional, managed 32-bit signed integer array to an unmanaged memory pointer. Public method static members are supported by Silverlight for Windows Phone support by Xbox 360Copy (int64[], Int32, INTPTR, Int32) security key. Copies data from a one-dimensional, managed 64-bit signed integer array to an unmanaged memory pointer. Public method static members are supported by Silverlight for Windows Phone support by Xbox 360Copy (INTPTR, byte[], Int32, Int32) security key. Copies data from an unmanaged memory pointer to an array of managed 8-bit unsigned integers. Public method static members are supported by Silverlight for Windows Phone support by Xbox 360Copy (INTPTR, char[], Int32, Int32) security key. Copies data from an unmanaged memory pointer to a managed character array. Public method static members are supported by Silverlight for Windows Phone support by Xbox 360Copy (INTPTR, double[], Int32, Int32) security key. Copies data from an unmanaged memory pointer to a managed double-precision floating-point array. Public method static members are supported by Silverlight for Windows Phone support by Xbox 360Copy (INTPTR, int16[], Int32, Int32) security key. Copies data from an unmanaged memory pointer to a managed 16-bit signed integer array. Public method static members are supported by Silverlight for Windows Phone support by Xbox 360Copy (INTPTR, int32[], Int32, Int32) security key. Copies data from an unmanaged memory pointer to a managed 32-bit signed integer array. Public method static members are supported by Silverlight for Windows Phone support by Xbox 360Copy (INTPTR, int64[], Int32, Int32) security key. Copies data from an unmanaged memory pointer to a managed 64-bit signed integer array. Public method static members are supported by Silverlight for Windows Phone support by Xbox 360Copy (INTPTR, single[], Int32, Int32) security key. Copies data from an unmanaged memory pointer to a managed single-precision floating-point array. Public method static members are supported by Silverlight for Windows Phone support by Xbox 360Copy (single[], Int32, INTPTR, Int32) security key. Copies data from a one-dimensional, managed single-precision floating-point array to an unmanaged memory pointer.    We can also use shared memory as a way to operate. Part of the code is as follows   copy code//Initialize returns information such as the size of the picture mycontext = new Vlccontrolwpfrenderercontext (width, height, SYSTEM.WINDOWS.MEDIA.PIXELFORMATS.BGR24)//Create shared memory Area Mybitmapsectionpointer = win32interop.createfilemapping (new INTPTR ( -1), IntPtr.Zero, Win32Interop.PageAccess.ReadWrite, 0, mycontext.size, NULL);//Get the first address of shared memory  map = Win32interop.mapviewoffile (Mybitmapsectionpointer, Win32Interop.FileMapAccess.AllAccess, 0, 0, (UINT) mycontext.size);//Copy the received image into the shared memory area win32interop.copymemory (map, data, mycontext.size);//Convert an array in shared memory to a picture Mybitmap = ( Interopbitmap) imaging.createbitmapsourcefrommemorysection (Mybitmapsectionpointer, MyContext.Width, Mycontext.height, Mycontext.pixelformat, mycontext.stride, 0); Copy code    here is the auxiliary class for shared memory   mainly some API functions   actually IntPtr is also a int,int in C # and Int32 is the same. So basically the pointer, long,int in C # is int. Just to make it easy for everyone to know what type of C + + he is, convenient to convert. function pointers in 3,c++   delegates in C #   This is the definition of function pointers in C + +  typedef VOID (WINAPI *pusercall) (Puchar pData, ULONG Length, PVOID p UserData); corresponds to C #The example below  public delegate void Pusercall (IntPtr pData, uint Length, UInt32 puserdata);  4, we know that int is 4 bytes.     below this is a method of C + +   U_camer LONG WINAPI camer_getpropery (HANDLE hcamer, _cmrctl propery); If we translate this function to 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 question, the call here is no problem. There is also a value, but he is taking the int32 in 4 bytes of 2 bytes.   We 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); the Camer_getpropery function returned only a long type. The conversion through pointers in C + +. The long type is converted to Pulong, which is also an array of ulong.   How do we call it in C #   int m_hiwi_temp = Bcamera.camer_getpropery (M_hcamer, Cmrctl. out_size);              &NBSp byte[] M_byte_hiwi = bitconverter.getbytes (m_hiwi_temp);                byte[] tem P1 = 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 = Bitcon Verter. ToInt16 (temp1, 0);                int high = bitconverter.toint16 (temp2, 0); Here's the example. The sub is that C + + sometimes really returns an int type, but in C + + it is easy to easily convert the int type to an array of two uint16 by pointers. So there must be some attention in C # when we convert.   Summary: In fact, the conversion of data type is mainly the conversion of data storage space, the data type in C + + occupies much space, as long as the conversion into C # in the equivalent space of the data type can be. Just C # See which data types are easier to manipulate.

C # calls some understanding of C + + dynamic libraries

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.