1、創建DLL新項目Dll1,Dll1.cpp:
1 extern "C" __declspec(dllexport) const char* myfunc() 2 { 3 return "hello,沙奇碼"; 4 }
生成后,將Dll1.dll置於之后創建控制台程序應用程序同目錄下。
2、創建一個C++控制台程序用於調用Dll1.dll測試,ConsoleApplication1.cpp:
1 #include <Windows.h> 2 #include <iostream> 3 using namespace std; 4 5 typedef const char*(*testFunc)(); 6 7 void main() 8 { 9 HINSTANCE hDll = LoadLibrary("Dll1.dll"); 10 testFunc tf = (testFunc)GetProcAddress(hDll,"myfunc"); 11 if(!tf) 12 { 13 cout<<"Error"<<endl; 14 } 15 else 16 { 17 cout<<tf()<<endl; 18 } 19 FreeLibrary(hDll); 20 system("pause"); 21 }
運行控制台程序,輸出 "hello,沙奇碼" ~