Windows Data Type.NET Data TypeBOOL, BOOLEANBoolean or Int32BSTRStringBYTEByteCHARCharDOUBLEDoubleDWordInt32 or UInt32FLOATSingleHANDLE (and all other handle types, such as HFONT and HMENU)IntPtr, UintPtr or HandleRef HRESULTInt32 or UInt32INTInt32LANGIDInt16 or UInt16LCIDInt32 or UInt32LONGInt32LPARAMIntPtr, UintPtr or ObjectLPCSTRStringLPCTSTRStringLPCWSTRStringLPSTRString or StringBuilder*LPTSTRString or StringBuilderLPWSTRString or StringBuilderLPVOIDIntPtr, UintPtr or ObjectLRESULTIntPtrSAFEARRAY.NET array typeSHORTInt16TCHARCharUCHARSByteUINTInt32 or UInt32ULONGInt32 or UInt32VARIANTObjectVARIANT_BOOLBooleanWCHARCharWORDInt16 or UInt16WPARAMIntPtr, UintPtr or Object另: 在進行string轉換時,需要加入前綴[MarshalAs(UnmanagedType.LPStr)]lpdword 對應于 ref intC/C++C#HANDLE, LPDWORD, LPVOID, void*IntPtrLPCTSTR, LPCTSTR, LPSTR, char*, const char*, Wchar_t*, LPWSTRString [in], StringBuilder [in, out]DWORD, unsigned long, UlongUInt32, [MarshalAs(UnmanagedType.U4)]boolboolLP<struct>[In] ref <struct>SIZE_TuintLPDWORDout uintLPTSTR[Out] StringBuilderPULARGE_INTEGERout ulongWORDuInt16Byte, unsigned charbyteShortInt16Long, intInt32floatsingledoubledoubleNULL pointerIntPtr.ZeroUintUint32C#調用DLL文件時參數對應表Wtypes.h中的非托管類型 非托管 C語言類型 托管類名 說明 HANDLE void* System.IntPtr 32 位 BYTE unsigned char System.Byte 8 位 SHORT short System.Int16 16 位 WORD unsigned short System.UInt16 16 位 INT int System.Int32 32 位 UINT unsigned int System.UInt32 32 位 LONG long System.Int32 32 位 BOOL long System.Int32 32 位 DWORD unsigned long System.UInt32 32 位 ULONG unsigned long System.UInt32 32 位 CHAR char System.Char 用 ANSI 修飾。LPSTR char* System.String 或 System.StringBuilder 用 ANSI 修飾。 LPCSTR Const char* System.String 或System.StringBuilder 用 ANSI 修飾。 LPWSTR wchar_t* System.String 或System.StringBuilder 用 Unicode 修飾。 LPCWSTR Const wchar_t* System.String 或System.StringBuilder 用 Unicode 修飾。 FLOAT Float System.Single 32 位 DOUBLE Double System.Double 64 位習慣用C#寫東西,但平時又會碰到很多要用win32 API的地方,所以經常要用DllImport,但win32函數的類型寫法是很龐雜的,相信為之困擾的不止我一個,現在我整理一份我個人的理解如下,希望高人不吝賜教。我的基本原則有如下幾點:1、下面都是針對32位系統的,所以int是32位.long也是32位;2、各種句柄類的(H開頭),我認為一律是System.IntPtr,到目前為止沒發現出錯;如果哪位在使用中出錯,請指出;3、LP和P,我實在不懂(對C 不太了解),對于LP和P開頭的函數,如果是和STR有關的,一律寫為System.String,像PLCID這樣指向什么東西的,寫為System.UInt32(因為指向另一個地址,那就是指針,指針是32位吧),int之類的數值型,那我就寫為int[],以方便.net程序引用(寫成System.UInt32的話,給API調用應該也不會出錯,但.net程序就不好引用了)如有意見,歡迎指教。謝謝BOOL=System.Int32BOOLEAN=System.Int32BYTE=System.UInt16CHAR=System.Int16COLORREF=System.UInt32DWORD=System.UInt32DWORD32=System.UInt32DWORD64=System.UInt64FLOAT=System.FloatHACCEL=System.IntPtrHANDLE=System.IntPtrHBITMAP=System.IntPtrHBRUSH=System.IntPtrHCONV=System.IntPtrHCONVLIST=System.IntPtrHCURSOR=System.IntPtrHDC=System.IntPtrHDDEDATA=System.IntPtrHDESK=System.IntPtrHDROP=System.IntPtrHDWP=System.IntPtrHENHMETAFILE=System.IntPtrHFILE=System.IntPtrHFONT=System.IntPtrHGDIOBJ=System.IntPtrHGLOBAL=System.IntPtrHHOOK=System.IntPtrHICON=System.IntPtrHIMAGELIST=System.IntPtrHIMC=System.IntPtrHINSTANCE=System.IntPtrHKEY=System.IntPtrHLOCAL=System.IntPtrHMENU=System.IntPtrHMETAFILE=System.IntPtrHMODULE=System.IntPtrHMONITOR=System.IntPtrHPALETTE=System.IntPtrHPEN=System.IntPtrHRGN=System.IntPtrHRSRC=System.IntPtrHSZ=System.IntPtrHWINSTA=System.IntPtrHWND=System.IntPtrINT=System.Int32INT32=System.Int32INT64=System.Int64LONG=System.Int32LONG32=System.Int32LONG64=System.Int64LONGLONG=System.Int64LPARAM=System.IntPtrLPBOOL=System.Int16[]LPBYTE=System.UInt16[]LPCOLORREF=System.UInt32[]LPCSTR=System.StringLPCTSTR=System.StringLPCVOID=System.UInt32LPCWSTR=System.StringLPDWORD=System.UInt32[]LPHANDLE=System.UInt32LPINT=System.Int32[]LPLONG=System.Int32[]LPSTR=System.StringLPTSTR=System.StringLPVOID=System.UInt32LPWORD=System.Int32[]LPWSTR=System.StringLRESULT=System.IntPtrPBOOL=System.Int16[]PBOOLEAN=System.Int16[]PBYTE=System.UInt16[]PCHAR=System.Char[]PCSTR=System.StringPCTSTR=System.StringPCWCH=System.UInt32PCWSTR=System.UInt32PDWORD=System.Int32[]PFLOAT=System.Float[]PHANDLE=System.UInt32PHKEY=System.UInt32PINT=System.Int32[]PLCID=System.UInt32PLONG=System.Int32[]PLUID=System.UInt32PSHORT=System.Int16[]PSTR=System.StringPTBYTE=System.Char[]PTCHAR=System.Char[]PTSTR=System.StringPUCHAR=System.Char[]PUINT=System.UInt32[]PULONG=System.UInt32[]PUSHORT=System.UInt16[]PVOID=System.UInt32PWCHAR=System.Char[]PWORD=System.Int16[]PWSTR=System.StringREGSAM=System.UInt32SC_HANDLE=System.IntPtrSC_LOCK=System.IntPtrSHORT=System.Int16SIZE_T=System.UInt32SSIZE_=System.UInt32TBYTE=System.CharTCHAR=System.CharUCHAR=System.Wtypes.h 中的非托管類型 非托管 C 語言類型 托管類名 說明 HANDLE void* System.IntPtr 在 32 位 Windows 操作系統上為 32 位,在 64 位 Windows 操作系統上為 64 位。BYTE unsigned char System.Byte 8 位SHORT short System.Int16 16 位WORD unsigned short System.UInt16 16 位INT int System.Int32 32 位UINT unsigned int System.UInt32 32 位LONG long System.Int32 32 位BOOL long System.Int32 32 位DWORD unsigned long System.UInt32 32 位ULONG unsigned long System.UInt32 32 位CHAR char System.Char 用 ANSI 修飾。LPSTR char* System.String 或 System.Text.StringBuilder用 ANSI 修飾。LPCSTR Const char* System.String 或 System.Text.StringBuilder用 ANSI 修飾。LPWSTR wchar_t* System.String 或 System.Text.StringBuilder用 Unicode 修飾。LPCWSTR Const wchar_t* System.String 或 System.Text.StringBuilder用 Unicode 修飾。FLOAT Float System.Single 32 位DOUBLE Double System.Double 64 位
新聞熱點
疑難解答