.NET 调用 Win32 API
.NET 调用 API 声明格式
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 dwLanguageId 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 dwLanguageId 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 dwLanguageId,
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 dwLanguageId,
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);数据类型转换表: C++ 到 .NET
类型 .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 LPCWSTR LPCTSTR;
#else
typedef LPCSTR LPCTSTR;
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
补充:软件开发 , C# ,