. NET calls Win32 API

Source: Internet
Author: User

. NET call API declaration format
VB. NET
[Vb] Declare Function <Name> Lib <dll name> <Optional fields> (<params>) As <return type>
 
'Example:
Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA "(_
ByVal dwFlags As Integer, ByRef lpSource As Object ,_
ByVal dwMessageId As Integer, ByVal dwreceivageid As Integer ,_
ByVal lpBuffer As String, ByVal nSize As Integer ,_
ByRef Arguments As Integer) As Integer

Declare Function <Name> Lib <dll name> <Optional fields> (<params>) As <return type>

'Example:
Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA "(_
ByVal dwFlags As Integer, ByRef lpSource As Object ,_
ByVal dwMessageId As Integer, ByVal dwreceivageid As Integer ,_
ByVal lpBuffer As String, ByVal nSize As Integer ,_
ByRef Arguments As Integer) As IntegerC #
[Csharp] view plaincopyprint? [DllImport ("<dll name>", <optional fields>)]
Static extern <return type> <Method name> (<Params>) [DllImport ("kernel32.dll", SetLastError = true)]
 
// Example:
Static extern int FormatMessageA (int dwFlags,
Ref object lpSource,
Int dwMessageId,
Int dwageid,
String lpBuffer,
Int nSize,
Ref int Arguments)

[DllImport ("<dll name>", <optional fields>)]
Static extern <return type> <Method name> (<Params>) [DllImport ("kernel32.dll", SetLastError = true)]

// Example:
Static extern int FormatMessageA (int dwFlags,
Ref object lpSource,
Int dwMessageId,
Int dwageid,
String lpBuffer,
Int nSize,
Ref int Arguments) Managed C ++. NET
[Cpp] [DllImport ("<Dll name>", <optional fields>)] static <return type> <Function name> (<Params> );
 
// Example:
[DllImport ("KERNEL32.DLL", EntryPoint = "MoveFileW", SetLastError = true, CharSet = CharSet: Unicode, ExactSpelling = true, CallingConvention = CallingConvention: StdCall)]
Static bool MoveFile (String ^ src, String ^ dst );

[DllImport ("<Dll name>", <optional fields>)] static <return type> <Function name> (<Params> );

// Example:
[DllImport ("KERNEL32.DLL", EntryPoint = "MoveFileW", SetLastError = true, CharSet = CharSet: Unicode, ExactSpelling = true, CallingConvention = CallingConvention: StdCall)]
Static bool MoveFile (String ^ src, String ^ dst); data type conversion table: C ++ to. NET
Type. net c ++
 
ATOM
Ushort typedef word atom;
BOOL
Bool, int typedef int BOOL;
BOOLEAN
Bool, byte typedef byte boolean;
BYTE byte typedef unsigned char BYTE;
CALLBACK delegate # define CALLBACK _ stdcall
CHAR char typedef char CHAR;
COLORREF uint typedef dword colorref;
CONST const # define CONST const
DWORD uint typedef unsigned long DWORD;
DWORDLONG ulong typedef ulonglong dwordlong;
DWORD_PTR
DWORD *
Uint, IntPtr typedef ULONG_PTR DWORD_PTR;
DWORD32 uint typedef unsigned int DWORD32;
DWORD64 ulong typedef unsigned _ int64 DWORD64;
FLOAT single typedef float FLOAT;
HACCEL IntPtr typedef handle haccel;
HANDLE IntPtr typedef pvoid handle;
HBITMAP IntPtr typedef handle hbitmap;
HBRUSH IntPtr typedef handle hbrush;
HCOLORSPACE IntPtr if (WINVER> = 0x0400)
Typedef handle hcolorspace;
HCONV IntPtr typedef handle hconv;
HCONVLIST IntPtr typedef handle hconvlist;
HCURSOR IntPtr typedef hicon hcursor;
HDC IntPtr typedef handle hdc;
HDDEDATA IntPtr typedef handle hddedata;
HDESK IntPtr typedef handle hdesk;
HDROP IntPtr typedef handle hdrop;
HDWP IntPtr typedef handle hdwp;
HENHMETAFILE IntPtr typedef handle henhmetafile;
HFILE int typedef int HFILE;
HFONT IntPtr typedef handle hfont;
HGDIOBJ IntPtr typedef handle hgdiobj;
HGLOBAL IntPtr typedef handle hglobal;
HHOOK IntPtr typedef handle hhook;
HICON IntPtr typedef handle hicon;
HINSTANCE IntPtr typedef handle hinstance;
HKEY IntPtr typedef handle hkey;
HKL IntPtr typedef handle hkl;
HLOCAL IntPtr typedef handle hlocal;
HMENU IntPtr typedef handle hmenu;
HMETAFILE IntPtr typedef handle hmetafile;
HMODULE IntPtr typedef hinstance hmodule;
HMONITOR IntPtr if (WINVER> = 0x0500)

Typedef handle hmonitor;
 
HPALETTE IntPtr typedef handle hpalette;
HPEN IntPtr typedef handle hpen;
HRESULT int, uint typedef long hresult;
HRGN IntPtr typedef handle hrgn;
HRSRC IntPtr typedef handle hrsrc;
HSZ IntPtr typedef handle hsz;
HWINSTA IntPtr typedef handle winsta;
HWND IntPtr typedef handle hwnd;
INT int typedef int INT;
INT_PTR IntPtr # if defined (_ WIN64)
Typedef _ int64 INT_PTR;
# Else
Typedef int INT_PTR;
 
INT32 int typedef signed int INT32;
INT64 long typedef signed _ int64 INT64;
LANGID ushort, int typedef word langid;
LCID uint typedef dword lcid;
LCTYPE uint typedef dword lctype;
LGRPID uint typedef dword lgrpid;
LONG int typedef long LONG;
LONGLONG long # if! Defined (_ M_IX86)
Typedef _ int64 LONGLONG;
# Else
Typedef double LONGLONG;
 
LONG_PTR IntPtr # if defined (_ WIN64)
Typedef _ int64 LONG_PTR;
# Else
Typedef long LONG_PTR;
 
LONG32 int typedef signed int LONG32;
LONG64 long typedef _ int64 LONG64;
LPARAM IntPtr typedef LONG_PTR LPARAM;
LPBOOL
Bool *
IntPtr, bool typedef BOOL * LPBOOL;
LPBYTE
Byte *
IntPtr, byte typedef BYTE * LPBYTE;
LPCOLORREF IntPtr, uint typedef DWORD * LPCOLORREF;
LPCSTR string, IntPtr, StringBuilder typedef const char * LPCSTR;
LPCTSTR string, IntPtr, StringBuilder # ifdef UNICODE
Typedef maid;
# Else
Typedef maid;
 
LPCVOID IntPtr typedef CONST void * LPCVOID;
LPCWSTR string, IntPtr, StringBuilder typedef const wchar * LPCWSTR;
LPDWORD IntPtr, uint typedef DWORD * LPDWORD;
LPHANDLE IntPtr typedef HANDLE * LPHANDLE;
LPINT IntPtr, int typedef int * LPINT;
LPLONG IntPtr, int typedef long * LPLONG;
LPSTR string, IntPtr, StringBuilder typedef CHAR * LPSTR;
LPTSTR string, IntPtr, StringBuilder # ifdef UNICODE
Typedef lpwstr lptstr;
# Else
Typedef lpstr lptstr;
 
LPVOID IntPtr typedef void * LPVOID;
LPWORD IntPtr, ushort typedef WORD * LPWORD;
LPWSTR string, IntPtr, StringBuilder typedef WCHAR * LPWSTR;
LRESULT IntPtr, int typedef LONG_PTR LRESULT;
PBOOL IntPtr, bool typedef BOOL * PBOOL;
PBOOLEAN IntPtr, bool typedef BOOLEAN * PBOOLEAN;
PBYTE IntPtr, byte typedef BYTE * PBYTE;
PCHAR IntPtr, char typedef CHAR * PCHAR;
PCSTR string, IntPtr, StringBuilder typedef const char * PCSTR;
PCTSTR string, IntPtr, StringBuilder # ifdef UNICODE
Typedef lpcwstr pctstr;
# Else
Typedef lpcstr pctstr;
 
PCWSTR string, IntPtr, StringBuilder typedef const wchar * PCWSTR;
PDWORD IntPtr, uint typedef DWORD * PDWORD;
PDWORDLONG IntPtr, ulong typedef DWORDLONG * PDWORDLONG;
PDWORD_PTR IntPtr, uint typedef DWORD_PTR * PDWORD_PTR;
PDWORD32 IntPtr, uint typedef DWORD32 * PDWORD32;
PDWORD64 IntPtr, ulong typedef DWORD64 * PDWORD64;
PFLOAT IntPtr, single typedef FLOAT * PFLOAT;
PHANDLE IntPtr typedef HANDLE * PHANDLE;
PHKEY IntPtr typedef HKEY * PHKEY;
PINT IntPtr, int typedef int * PINT;
PINT_PTR IntPtr typedef INT_PTR * PINT_PTR;
PINT32 IntPtr, int typedef INT32 * PINT32;
PINT64 IntPtr, long typedef INT64 * PINT64;
PLCID IntPtr, uint typedef pdword plcid;
PLONG IntPtr, int typedef LONG * PLONG;
PLONGLONG IntPtr, long typedef LONGLONG * PLONGLONG;
PLONG_PTR IntPtr, int typedef LONG_PTR * PLONG_PTR;
PLONG32 IntPtr, int typedef LONG32 * PLONG32;
PLONG64 IntPtr, long typedef LONG64 * PLONG64;
POINTER_32 IntPtr, int # if defined (_ WIN64)
# Define POINTER_32 _ ptr32
# Else
# Define POINTER32
 
POINTER_64 IntPtr, long # define POINTER_64 _ ptr64
PSHORT IntPtr, short typedef SHORT * PSHORT;
PSIZE_T IntPtr typedef SIZE_T * PSIZE_T;
PSSIZE_T IntPtr typedef SSIZE_T * PSSIZE_T;
PSTR IntPtr, string, StringBuilder typedef CHAR * PSTR;
PTBYTE IntPtr, char typedef TBYTE * PTBYTE;
PTCHAR IntPtr, string, StringBuilder typedef TCHAR * PTCHAR;
PTSTR IntPtr, string, StringBuilder # ifdef UNICODE
Typedef lpwstr ptstr;
# Else
Typedef lpstr ptstr;
 
PUCHAR IntPtr, string, StringBuilder typedef UCHAR * PUCHAR;
PUINT IntPtr, uint typedef UINT * PUINT;
PUINT_PTR IntPtr, uint typedef UINT_PTR * PUINT_PTR;
PUINT32 IntPtr, uint typedef UINT32 * PUINT32;
PUINT64 IntPtr, ulong typedef UINT64 * PUINT64;
PULONG IntPtr, uint typedef ULONG * PULONG;
PULONGLONG IntPtr, ulong typedef ULONGLONG * PULONGLONG;
PULONG_PTR IntPtr, uint: typedef ULONG_PTR * PULONG_PTR;
PULONG32 IntPtr, uint typedef ULONG32 * PULONG32;
PULONG64 IntPtr, ulong typedef ULONG64 * PULONG64;
PUSHORT IntPtr, ushort typedef USHORT * PUSHORT;
PVOID IntPtr typedef void * PVOID;
PWCHAR IntPtr, string typedef WCHAR * PWCHAR;
PWORD IntPtr, ushort typedef WORD * PWORD;
PWSTR IntPtr, string, StringBuilder typedef WCHAR * PWSTR;
SC _HANDLE IntPtr typedef HANDLE SC _HANDLE;
SC _LOCK IntPtr typedef LPVOID SC _LOCK;
SERVICE_STATUS_HANDLE IntPtr typedef HANDLE SERVICE_STATUS_HANDLE;
SHORT short typedef short SHORT;
SIZE_T uint, IntPtr typedef ULONG_PTR SIZE_T;
SSIZE_T int, IntPtr typedef LONG_PTR SSIZE_T;
TBYTE char # ifdef UNICODE
Typedef wchar tbyte;
# Else
Typedef unsigned char TBYTE;
 
TCHAR char # ifdef UNICODE
Typedef wchar tchar;
# Else
Typedef char TCHAR;
 
UCHAR char typedef unsigned char UCHAR;
UINT uint typedef unsigned int UINT;
UINT_PTR UIntPtr, uint # if defined (_ WIN64)
Typedef unsigned _ int64 UINT_PTR;
# Else
Typedef unsigned int UINT_PTR;
 
UINT32 uint typedef unsigned int UINT32;
 
UINT64 ulong typedef usigned _ int64 UINT64;
 
ULONG uint typedef unsigned long ULONG;
ULONGLONG ulong # if! Defined (_ M_IX86)
Typedef unsigned _ int64 ULONGLONG;
# Else
Typedef double ULONGLONG
 
ULONG_PTR IntPtr, uint # if defined (_ WIN64)
Typedef unsigned _ int64 ULONG_PTR;
# Else
Typedef unsigned long ULONG_PTR;
 
ULONG32 uint typedef unsigned int ULONG32;
ULONG64 ulong typedef unsigned _ int64 ULONG64;
USHORT ushort typedef unsigned short USHORT;
USN long typedef longlong usn;
VOID void # define VOID void
WCHAR char typedef wchar_t WCHAR;
For details about WINAPI, see CallingConvention enumeration # define WINAPI _ stdcall
WORD ushort typedef unsigned short WORD;
WPARAM IntPtr, uint typedef UINT_PTR WPARAM;

 
Prompt
A pointer to a certain type in the list is indicated by a prefix, for example, DWORD * = PDWORD.
Some types (for example, UIntPtr) are not compatible with CLS, so you can choose based on your actual situation when using IntPtr.
Generally, it is the best option to enter "=" in the IDE and then the type Automatically pops up by intelliisense.
When using strings in COM Interop, WCHAR * And TCHAR * should always be used as input; string or StringBuilder should be used as output. When you use an IntPtr to extract all characters using the Marshal. PtrToStructure () method, you need to increment the pointer. you can use pointer + = Marshal. SizeOf (<last object you got back> );
Sometimes the data types do not need to be completely consistent. For example, int can be used to replace uint.
To convert IntPtr to int or another class, use the member method of Marshal. PtrToStructure () or IntPtr.
If the API you are using is based on ANSI or Unicode, you should first ensure that the correct API is selected. See CharSet enumeration.
Most APIs can be written as hosted type declarations, but some require pointers. You can use the unsafe keyword in C #, and remember to enable/unsafe during compilation.
To ensure that gc does not recycle the IntPtr used for API calls, you can use the HandleRef type.
When you need to define a structure for the API, make sure that they carry the StructLayout. Sequential attribute.
When using the API to pass in/out an array, check whether it is a pointer to an array or an array object. If it is a pointer, convert it to IntPtr first.
IntPtr is used in the list to extract a certain type. Sometimes you can directly use the ref <datatype> or out <datatype> Format, but you still need IntPr as the input for char, use ref IntPtr as output
When the function you defined does not work properly, it may not be because your function definition is wrong. This may be because the wrong data is passed in or another cause.
They only use Marshal and MarshalAs when necessary. In some cases, they consume a lot of processing time.

 

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.