程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> .NET 調用 Win32 API

.NET 調用 Win32 API

編輯:C#入門知識

.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 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;
WINAPI 參見 CallingConvention 枚舉 #define WINAPI __stdcall
WORD ushort typedef unsigned short WORD;
WPARAM IntPtr, uint typedef UINT_PTR WPARAM;

 
提示
列表中指向某一類型的指針使用前綴表示,例如: DWORD* = PDWORD
某些類型(例如: UIntPtr)不兼容 CLS,所以使用了 IntPtr,你可以根據你實際的情況進行選擇
通常在 IDE 中輸入“=”號後Intellisense自動彈出的類型是最好的選擇
在 COM Interop 中使用字符串時,應當總是使用 WCHAR*,TCHAR* 等等來作為輸入;使用 string 或 StringBuilder 作為輸出。當你用一個 IntPtr 通過 Marshal.PtrToStructure() 方法來提取所有字符時會需要遞增指針,可以使用 pointer +=Marshal.SizeOf(<last object you got back>);
有時數據類型並不需要完全一致,譬如 int 可以用來替換 uint
需要把 IntPtr 轉換為 int 或其他類的時候,應使用 Marshal.PtrToStructure() 或 IntPtr 的成員方法
如果你使用的 API 建立在 ANSI 或 Unicode 的基礎上,首先應確保選擇了正確的API。參見: CharSet 枚舉
大多數的 API 可以寫成托管類型的聲明,但某些需要指針,在 C# 中使用 unsafe 關鍵字你就能使用指針,另外編譯時記得帶上 /unsafe 開關
如果想確保 gc 不回收API 調用時使用的 IntPtr,你可以使用 HandleRef 類型
當需要為API定義一個結構時,確保他們帶上 StructLayout.Sequential 屬性
使用 API 方法傳入/傳出數組時,先確認是一個指向數組的指針還是一個數組對象,如果是指針你需要先把它轉成 IntPtr
列表中多使用 IntPtr 來提取某個類型,有時你可以直接用 ref <datatype> 或 out <datatype>的形式,但是對於char*  你還是要用 IntPr 作為輸入,用 ref IntPtr 作為輸出
當你定義的函數無法正常工作,並不一定是你的函數定義寫錯了,這有可能是傳入了錯誤的數據或其他原因
僅在必要時使用 Marshal 及 MarshalAs,某些場合下他們消耗較多的處理時間

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved