http://www.optimize0.jp/doc/csharp/index
・#define
// C++
#define DWORD PAGE_NOACCESS 0x01
↓
// C#
public const uint PAGE_NOACCESS = 0x01;
・#typedef
// C++
typedef unsigned long DWORD;
↓
// C#
using DWORD = System.UInt32;
・構造体
// C++
struct TestA
{
int a;
char b;
};
↓
// C#
[StructLayout(LayoutKind.Sequential)]
public struct TestA
{
public System.UInt16 a;
public System.Byte b;
}
・WinAPI
////////////////////////////////////
// case1 構造体で値を渡す
// WinAPI32
void SetA(const TestA* s);
↓
// C#
[DllImport("user32", EntryPoint = "SetA")]
public static extern void SetAWin32(IntPtr lParam);
public void SetA(ref TestA s)
{
// メモリを確保
IntPtr p = Marshal.AllocCoTaskMem(Marshal.SizeOf(s));
// マネージドからアンマネージドへの変換を伴うコピー
Marshal.StructureToPtr(s, p, false);
// Win32API呼び出し
SetAWin32(p);
// メモリを解放
Marshal.FreeCoTaskMem(p);
}
////////////////////////////////////
// case2 構造体に値を返す
// WinAPI
void GetA(TestStructA* s);
↓
// C#
[DllImport("user32", EntryPoint = "GetA")]
public static extern void GetAWin32(IntPtr lParam);
public void GetA(out TestA s)
{
// メモリを確保
IntPtr p = Marshal.AllocCoTaskMem(Marshal.SizeOf(s));
// Win32API呼び出し
GetAWin32(p);
// メモリに格納された情報をコピーする
s = (TestA)Marshal.PtrToStructure(p, s.GetType());
// メモリを解放
Marshal.FreeCoTaskMem(p);
}
最終更新:2009年01月08日 12:23