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)
Http://topic.csdn.net/u/20090928/11/af7848c6-5071-41aa-92e2-e8d626d6aefe.html
Http://blog.csdn.net/dz45693/archive/2009/09/26/4598867.aspx
Http://www.cnblogs.com/yiki/archive/2008/10/29/1321848.html
Http://blog.csdn.net/okadler0518/archive/2009/06/22/4289679.aspx
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
- Int getwindowtext (
- Hwnd,
- Lptstr lpstring,
- Int nmaxcount
- );
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.
GetWindowText
Http://msdn.microsoft.com/en-us/library/ms633520%28VS.85%29.aspx
GetForegroundWindow
Http://msdn.microsoft.com/en-us/library/ms633505%28VS.85%29.aspx
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
- [System. runtime. interopservices. structlayoutattribute (system. runtime. interopservices. layoutkind. Sequential, charset = system. runtime. interopservices. charset. Unicode)]
- Public struct shfileinfow {
- /// Hicon-> hicon __*
- Public System. intptr hicon;
- /// Int
- Public int iicon;
- /// DWORD-> unsigned int
- Public uint dwattributes;
- /// Wchar [260]
- [System. runtime. interopservices. financialasattribute (system. runtime. interopservices. unmanagedtype. byvaltstr, sizeconst = 260)]
- Public String szdisplayname;
- /// Wchar [80]
- [System. Runtime. InteropServices. financialasattribute (System. Runtime. InteropServices. UnmanagedType. ByValTStr, SizeConst = 80)]
- Public string szTypeName;
- }
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
- [System. Runtime. InteropServices. DllImportAttribute ("user32.dll", EntryPoint = "GetWindowText")]
- 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
HWND IntPtr
Void * IntPtr
Int
Int * ref int
* Int IntPtr
Unsigned int uint
COLORREF 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