從DLL 動態載入函數
問題:
如何使用LoadLibrary 存取句柄定義的函數DLL?
LoadLibrary 將 DLL 載入到記憶體中,但不會自動匯入其函數。這需要第二個WinAPI函數:GetProcAddress。
範例:
#include <windows.h> #include <iostream> typedef int (__stdcall *f_funci)(); int main() { HINSTANCE hGetProcIDDLL = LoadLibrary("C:\Documents and Settings\User\Desktop\test.dll"); if (!hGetProcIDDLL) { std::cout << "could not load the dynamic library" << std::endl; return EXIT_FAILURE; } // Resolve function address using GetProcAddress f_funci funci = (f_funci) GetProcAddress(hGetProcIDDLL, "funci"); if (!funci) { std::cout << "could not locate the function" << std::endl; return EXIT_FAILURE; } std::cout << "funci() returned " << funci() << std::endl; // Free the library handle when no longer needed FreeLibrary(hGetProcIDDLL); return EXIT_SUCCESS; }
Dll匯出:
確保功能使用__declspec(dllexport) 和__dcall DLL 正確匯出屬性:
int __declspec(dllexport) __stdcall funci() { // ... }
以上是如何使用 LoadLibrary 和 GetProcAddress 存取 DLL 函數?的詳細內容。更多資訊請關注PHP中文網其他相關文章!