| 場景 | 所需檔案 | 說明 |
|---|---|---|
| 動態連結 + 隱式載入 | .h + .lib + .dll | .lib 為導入庫,連結期解析符號;.dll 執行期載入 |
| 動態連結 + 顯式載入 | .h + .dll | 執行期 LoadLibrary/GetProcAddress 手動取得函式指標,無需 .lib |
| 純 C 介面 + 顯式載入 | 僅 .dll | 呼叫端自行宣告函式指標型別,完全不需 .h/.lib |
// 呼叫端
typedef void (*FuncPtr)(int);
HMODULE hDll = LoadLibrary(L"MyLib.dll");
if (hDll) {
FuncPtr func = (FuncPtr)GetProcAddress(hDll, "MyFunction");
if (func) func(42);
FreeLibrary(hDll);
}
extern "C" 避免 C++ 名稱修飾,最大化跨編譯器相容性VS_VERSION_INFO 資源,方便執行期檢查版本Dependencies (現代 Dependency Walker) 驗證 DLL 相依性「編譯後只有一個 DLL 就能給別人用」—— 這只有在對方使用顯式載入且自行宣告函式簽名時才成立。隱式連結(最常見用法)必須有 .lib 導入庫。