C # Call C ++ Dll

Source: Internet
Author: User

Now, we will summarize the question "type comparison of function parameters when C # Calls the non-hosted C ++ code of DLL.

Search with these keywords. There are a lot of such content on the Internet, such as the following links.


C # comparison with C ++ data types (the content of the last three articles is the same)







However, the above ing may sometimes cause problems.

For example, all the above posts map lptstr to string,

However, when processing the getwindowtext function, this lptstr is used to return the result.

Therefore, using string here does not work, but stringbuffer is required.


Note: The prototype of getwindowtext

CPP Code  
  1. Int getwindowtext (
  2. Hwnd,
  3. Lptstr lpstring,
  4. Int nmaxcount
  5. );



If the problem is solved by simply checking the links above, I will not write this blog.

My main purpose is to introduce the other two methods.


Method 1:

View the MSDN of the Web version.

Let's take a look at the following two connections. In the Community Content section, we provide the prototype of C # and VB calls.

Of course, not all functions correspond to the full Community Content section.

But some give some constant values, some give some struct definitions, in short, this part of content still has reference value.

Note: MSDN installed on the local machine does not contain Community Content.









Method 2:

P/invoke InterOP Assistant


Enter the Type, Constant, and Procedure you want, and the corresponding code (C #, or VB) is automatically generated ).


For example.

If I want SHGetFileInfo to call this function, the type SHFileInfo is used.

The following result is displayed when I query the SHFileInfo type in P/Invoke Interop Assistant:

C # code  
  1. [System. runtime. interopservices. structlayoutattribute (system. runtime. interopservices. layoutkind. Sequential, charset = system. runtime. interopservices. charset. Unicode)]
  2. Public struct shfileinfow {
  3. /// Hicon-> hicon __*
  4. Public System. intptr hicon;
  5. /// Int
  6. Public int iicon;
  7. /// DWORD-> unsigned int
  8. Public uint dwattributes;
  9. /// Wchar [260]
  10. [System. runtime. interopservices. financialasattribute (system. runtime. interopservices. unmanagedtype. byvaltstr, sizeconst = 260)]
  11. Public String szdisplayname;
  12. /// Wchar [80]
  13. [System. Runtime. InteropServices. financialasattribute (System. Runtime. InteropServices. UnmanagedType. ByValTStr, SizeConst = 80)]
  14. Public string szTypeName;
  15. }



Here is another example.

For the previously mentioned GetWindowText function, if it is queried in Procedure of P/Invoke Interop Assistant.

The following code is also obtained:


C # code  
  1. [System. Runtime. InteropServices. DllImportAttribute ("user32.dll", EntryPoint = "GetWindowText")]
  2. Public static extern int GetWindowText (IntPtr hWnd, StringBuilder lpString, int nMaxCount );



The code above has been sorted out by me, And the automatically generated items have poor readability and redundancy.

However, this tool is very useful.


To sum up

With this tool, the C # And C ++ data types mentioned in the beginning of this article are also compared.

Analysis, search, no more Google.

It is very convenient to call the unmanaged dll code in C.


For convenience, I also reproduced the "C # And C ++ data type table" as follows:


C ++ C #
WORD ushort
DWORD uint
UCHAR int/byte can be replaced by int in most cases. If strict alignment is required, bytebyte should be used.
UCHAR * string/IntPtr
Unsigned char * [financialas (UnmanagedType. LPArray)] byte []/? (Intptr)
Char * string
LPCTSTR string
LPTSTR [financialas (UnmanagedType. LPTStr)] string
Long int
Ulong uint
Handle IntPtr
Void * IntPtr
Int * ref int
* Int IntPtr
Unsigned int uint



Data Type ing between APIs and C #
API data type Type description C # type API data type Type description C # type
WORD 16-bit unsigned integer Ushort CHAR Character Char
LONG 32-bit unsigned integer Int DWORDLONG 64-bit long integer Long
DWORD 32-bit unsigned integer Uint HDC Device description table handle Int
HANDLE Handle, 32-bit integer Int HGDIOBJ GDI object handle Int
UINT 32-bit unsigned integer Uint HINSTANCE Instance handle Int
BOOL 32-bit Boolean integer Bool HWM Window handle Int
LPSTR 32-bit pointer to a character String HPARAM 32-bit message Parameters Int
LPCSTR 32-bit pointer to a common character String LPARAM 32-bit message Parameters Int
BYTE Bytes Byte Wparam 32-bit message Parameters Int



BOOL = System. Int32
BOOLEAN = System. Int32
BYTE = System. UInt16
CHAR = System. Int16
COLORREF = System. UInt32
DWORD = System. UInt32
DWORD32 = System. UInt32
DWORD64 = System. UInt64
FLOAT = System. Float
HACCEL = System. IntPtr
HANDLE = System. IntPtr
HBITMAP = System. IntPtr
Hbrush = system. intptr
Hconv = system. intptr
Hconvlist = system. intptr
Hcursor = system. intptr
HDC = system. intptr
Hddedata = system. intptr
Hdesk = system. intptr
Hdrop = system. intptr
Hdwp = system. intptr
Henhmetafile = system. intptr
Hfile = system. intptr
Hfont = system. intptr
Hgdiobj = system. intptr
Hglobal = system. intptr
Hhook = system. intptr
Hicon = system. intptr
Himagelist = system. intptr
Himc = system. intptr
Hinstance = system. intptr
Hkey = system. intptr
Hlocal = system. intptr
Hmenu = system. intptr
Hmetafile = system. intptr
Hmodule = system. intptr
Hmonitor = system. intptr
HPALETTE = System. IntPtr
HPEN = System. IntPtr
HRGN = System. IntPtr
HRSRC = System. IntPtr
HSZ = System. IntPtr
HWINSTA = System. IntPtr
HWND = System. IntPtr
INT = System. Int32
INT32 = System. Int32
INT64 = System. Int64
LONG = System. Int32
LONG32 = System. Int32
LONG64 = System. Int64
LONGLONG = System. Int64
LPARAM = System. IntPtr
LPBOOL = System. Int16 []
LPBYTE = System. UInt16 []
LPCOLORREF = System. UInt32 []
LPCSTR = System. String
LPCTSTR = System. String
LPCVOID = System. UInt32
LPCWSTR = System. String
LPDWORD = System. UInt32 []
LPHANDLE = System. UInt32
LPINT = System. Int32 []
LPLONG = System. Int32 []
Lpstr = system. String
Lptstr = system. String
Lpvoid = system. uint32
Lpword = system. int32 []
Lpwstr = system. String
Lresult = system. intptr
Pbool = system. int16 []
Pboolean = system. int16 []
Pbyte = system. uint16 []
Pchar = system. Char []
Pcstr = system. String
Pctstr = system. String
Pcwch = system. uint32
Pcwstr = system. uint32
Pdword = system. int32 []
Pfloat = system. Float []
Phandle = system. uint32
Phkey = system. uint32
Pint = system. int32 []
Plcid = system. uint32
Plong = system. int32 []
Pluid = system. uint32
Pshort = system. int16 []
Pstr = system. String
Ptbyte = system. Char []
Ptchar = system. Char []
PTSTR = System. String
PUCHAR = System. Char []
PUINT = System. UInt32 []
PULONG = System. UInt32 []
PUSHORT = System. UInt16 []
PVOID = System. UInt32
PWCHAR = System. Char []
PWORD = System. Int16 []
PWSTR = System. String
REGSAM = System. UInt32
SC _HANDLE = System. IntPtr
SC _LOCK = System. IntPtr
SHORT = System. Int16
SIZE_T = System. UInt32
SSIZE _ = System. UInt32
TBYTE = System. Char
TCHAR = System. Char
UCHAR = System. Byte
UINT = System. UInt32
UINT32 = System. UInt32
UINT64 = System. UInt64
ULONG = System. UInt32
ULONG32 = System. UInt32
ULONG64 = System. UInt64
ULONGLONG = System. UInt64
USHORT = System. UInt16
WORD = System. UInt16
WPARAM = System. IntPtr

<--------- Supplement ----------->

Description of unmanaged C-language managed classes in Wtypes. h
HANDLE void * System. IntPtr 32-bit
BYTE unsigned char System. Byte 8-bit
SHORT short System. Int16-bit
WORD unsigned short System. UInt16 16-bit
INT int System. Int32 32-bit
UINT unsigned int System. UInt32 32-bit
LONG long System. Int32 32-bit
BOOL long System. Int32 32-bit
DWORD unsigned long System. UInt32 32-bit
ULONG unsigned long System. UInt32 32-bit
CHAR char System. Char is modified with ANSI.
LPSTR char * System. String or System. StringBuilder is modified with ANSI.
LPCSTR Const char * System. String or System. StringBuilder is modified with ANSI.
LPWSTR wchar_t * System. String or System. StringBuilder is modified with Unicode.
Lpcwstr const wchar_t * system. string or system. stringbuilder are modified with Unicode.
Float float system. Single 32-bit
Double double system. Double 64-bit

Related Article

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.