C++動態(顯式)調用 C++ dll


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,沙奇碼" ~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM