原計劃寫完 H.264 轉 AVI 才總結,因時間與需求關係決定提前結束系列。若後續有新功能開發,將以續集形式分享 (預計 1-2 個月後)。
Marshal.AllocHGlobal / FreeHGlobalGlobalAlloc/GlobalFree,從進程非受控堆配置記憶體finally 區塊或 using 模式中呼叫 Marshal.FreeHGlobal 釋放OutOfMemoryExceptionIntPtr ptr = Marshal.AllocHGlobal(size);
try {
// 使用 ptr
} finally {
Marshal.FreeHGlobal(ptr);
}
ref| VC++ | C# | 說明 |
|---|---|---|
RECT *rect | ref Rectangle rect | Rectangle 為對應結構體 |
DS_BOARD_DETAIL &detail | ref DS_BOARD_DETAIL detail | 結構體必須 ref 才能回傳資料 |
| VC++ | C# | 取得方式 |
|---|---|---|
HWND, HANDLE | IntPtr | Control.Handle, panel.Handle |
BYTE*, char* | IntPtr 或 byte[] | 配合 Marshal.Copy 互轉 |
| VC++ | C# |
|---|---|
COLORREF (0xBBGGRR) | int Color.FromArgb(255, 0, 255).ToArgb() (0xAARRGGBB) |
注意:VC++ COLORREF 為 BGR 順序,C# ToArgb() 為 ARGB,紅藍通道需對調。
| VC++ | C# |
|---|---|
int*, ULONG*, DWORD* | out int, out uint, out long |
VC++ int/long (通常 32-bit) 對應 C# int/long 均可,只要不超出範圍。實務上 int 最常用,long 用於 64-bit 數值。
char* (ANSI) → C# [MarshalAs(UnmanagedType.LPStr)] string 或 StringBuilderwchar_t* (Unicode) → C# [MarshalAs(UnmanagedType.LPWStr)] stringStringBuilder(capacity)下篇將續述:委托/回調陷阱、陣列封送、錯誤處理模式等進階主題。