生成DLL:
新建項目-C++-動態鏈接庫(DLL)
項目-右鍵-屬性-C/C++-預編譯頭:不使用預編譯頭。
項目-右鍵-屬性-C/C++-高級:編譯爲:C。
添加相應的.h和.c文件
在.h中,對外開放的接口前 加註 __declspec(dllexport)
調用DLL:
將.dll 和 .lib放在與Debug同級目錄
項目右鍵-屬性-VC++目錄-庫目錄-將.dll和.lib所在文件夾目錄寫入庫目錄中
項目-資源文件右鍵添加現有項-將.lib添加進去 或者 直接#pragma comment(lib,"Dll1.lib")
需要使用三個函數:LoadLibrary,GetProcAddress和FreeLibrary
HMODULE module = LoadLibraryA("CreatDll.dll");
if (module == NULL)
{
system("error load");
}
test_print = (void(*)(char const*))GetProcAddress(module, "test_print");
if ( test_print != NULL)
{
test_print("輸出測試");
}
FreeLibrary(module);