C++(Win 32) |
C# |
char** |
作為輸入參數轉為char[],通過Encoding類對這個string[]進行編碼后得到的一個char[] |
作為輸出參數轉為byte[],通過Encoding類對這個byte[]進行解碼,得到字符串 |
|
C++ Dll接口: void CplusplusToCsharp(in char** AgentID, out char** AgentIP); C#中的聲明: [DllImport("Example.dll")] public static extern void CplusplusToCsharp(char[] AgentID, byte[] AgentIP); C#中的調用: Encoding encode = Encoding.Default; byte[] tAgentID; byte[] tAgentIP; string[] AgentIP; tAgentID = new byte[100]; tAgentIP = new byte[100]; CplusplusToCsharp(encode.GetChars(tAgentID), tAgentIP); AgentIP[i] = encode.GetString(tAgentIP,i*Length,Length); |
|
Handle |
IntPtr |
Hwnd |
IntPtr |
int* |
ref int |
int& |
ref int |
void* |
IntPtr |
unsigned char* |
ref byte |
BOOL |
bool |
DWORD |
int 或 uint(int 更常用一些) |
枚舉類型 |
Win32: BOOL MessageBeep(UINT uType // 聲音類型); 其中的聲音類型為枚舉類型中的某一值。 C#: 用戶需要自己定義一個枚舉類型: public enum BeepType { SimpleBeep = -1, IconAsterisk = 0x00000040, IconExclamation = 0x00000030, IconHand = 0x00000010, IconQuestion = 0x00000020, Ok = 0x00000000, } C#中導入該函數: [DllImport("user32.dll")] public static extern bool MessageBeep(BeepType beepType); C#中調用該函數: MessageBeep(BeepType.IconQuestion); |
結構類型 |
Win32: 使用結構指針作為參數的函數: BOOL GetSystemPowerStatus( LPSYSTEM_POWER_STATUS lpSystemPowerStatus ); Win32中該結構體的定義: typedef struct _SYSTEM_POWER_STATUS { BYTE ACLineStatus; BYTE BatteryFlag; BYTE BatteryLifePercent; BYTE Reserved1; DWORD BatteryLifeTime; DWORD BatteryFullLifeTime; } SYSTEM_POWER_STATUS, *LPSYSTEM_POWER_STATUS; C#: 用戶自定義相應的結構體: struct SystemPowerStatus { byte ACLineStatus; byte batteryFlag; byte batteryLifePercent; byte reserved1; int batteryLifeTime; int batteryFullLifeTime; } C#中導入該函數: [DllImport("kernel32.dll")] public static extern bool GetSystemPowerStatus( ref SystemPowerStatus systemPowerStatus); C#中調用該函數: SystemPowerStatus sps; ….sps初始化賦值…… GetSystemPowerStatus(ref sps); |
字符串 |
對於字符串的處理分為以下幾種情況: 1、 字符串常量指針的處理(LPCTSTR),也適應於字符串常量的處理,.net中的string類型是不可變的類型。 2、 字符串緩沖區的處理(char*),即對於變長字符串的處理,.net中StringBuilder可用作緩沖區 Win32: BOOL GetFile(LPCTSTR lpRootPathName); C#: 函數聲明: [DllImport("kernel32.dll", CharSet = CharSet.Auto)] static extern bool GetFile ( [MarshalAs(UnmanagedType.LPTStr)] string rootPathName); 函數調用: string pathname; GetFile(pathname); 備注: DllImport中的CharSet是為了說明自動地調用該函數相關的Ansi版本或者Unicode版本
變長字符串處理: C#: 函數聲明: [DllImport("kernel32.dll", CharSet = CharSet.Auto)] public static extern int GetShortPathName( [MarshalAs(UnmanagedType.LPTStr)] string path, [MarshalAs(UnmanagedType.LPTStr)] StringBuilder shortPath, int shortPathLength); 函數調用: StringBuilder shortPath = new StringBuilder(80); int result = GetShortPathName( @"d:\test.jpg", shortPath, shortPath.Capacity); string s = shortPath.ToString(); |
struct |
具有內嵌字符數組的結構: Win32: typedef struct _TIME_ZONE_INFORMATION { LONG Bias; WCHAR StandardName[ 32 ]; SYSTEMTIME StandardDate; LONG StandardBias; WCHAR DaylightName[ 32 ]; SYSTEMTIME DaylightDate; LONG DaylightBias; } TIME_ZONE_INFORMATION, *PTIME_ZONE_INFORMATION; C#: [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] struct TimeZoneInformation { public int bias; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string standardName; SystemTime standardDate; public int standardBias; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string daylightName; SystemTime daylightDate; public int daylightBias; } |
具有回調的函數 |
Win32: BOOL EnumDesktops( HWINSTA hwinsta, // 窗口實例的句柄 DESKTOPENUMPROC lpEnumFunc, // 回調函數 LPARAM lParam // 用於回調函數的值 ); 回調函數DESKTOPENUMPROC的聲明: BOOL CALLBACK EnumDesktopProc( LPTSTR lpszDesktop, // 桌面名稱 LPARAM lParam // 用戶定義的值 ); C#: 將回調函數的聲明轉化為委托: delegate bool EnumDesktopProc( [MarshalAs(UnmanagedType.LPTStr)] string desktopName, int lParam); 該函數在C#中的聲明: [DllImport("user32.dll", CharSet = CharSet.Auto)] |
該表對C#中調用win32函數,以及c++編寫的dll時參數及返回值的轉換做了一個小的總結,如果想進一步了解這方面內容的話,可以參照msdn中“互操作封送處理”一節。