Windows中的庫編程
阿新 • • 發佈:2019-04-30
variables include space get alt des https c++ 運行
Windows操作系統中,庫分為動態鏈接庫(dll)和靜態鏈接庫(lib)
動態鏈接庫(DLL Dynamic Linkable Library)
DLL是Windows中實現代碼共享的一種方式。它是一個二進制式文件,不可單獨運行,需要調用方調用才能運行。在Windows中,DLL可以被多種編程語言所支持。
1、通過關鍵字_declspec(dllexport導出)
新建一個C++ dll工程 DllExportTest
新建一個DllExportTest..h文件
輸入
1 #pragma once 2 3 __declspec(dllexport) voidPrintHelloWorld();
在DllExportTest.cpp中輸入
1 #include "stdafx.h" 2 #include"DllExportTest..h" 3 #include<iostream> 4 5 using namespace std; 6 7 void PrintHelloWorld() 8 { 9 cout << "HelloWorld" << endl; 10 }
2、通過模塊定義文件導出
新建一個C++ dll工程 DllExportWithDEFTest
新建一個DllExportWithDEFTest.h,輸入
1 #pragma once 2 3 void PrintHelloWorld();
在DllExportWithDEFTest.cpp中輸入
1 #include "stdafx.h" 2 #include"DllExportWithDEFTest.h" 3 #include<iostream> 4 5 using namespace std; 6 7 void PrintHelloWorld() 8 { 9 cout << "HelloWorld" << endl; 10 }
然後在“源文件”上右鍵選擇“添加新項”,選擇“代碼”->”模塊定義文件(.def)“
.def常用格式如下
LIBRARY DllName //dll名字, DllName可以為空 DESCRIPTION DllDescription //dll 描述 這一行可以省略 EXPORTS FuncOrVariables //要導出的函數名或變量的名字
我在前面的代碼中定義了PrintHelloWorld函數,要將它導出,可以這樣定義.def文件
LIBRARY
EXPORTS
PrintHelloWorld
如果要將PrintHelloWorld函數導出為其它的名字,如MyFuncName,可以這樣定義.def文件
LIBRARY
EXPORTS
MyFuncName = PrintHelloWorld
如何使用導出的函數
C#調用可以參考以下鏈接
https://www.cnblogs.com/zhaotianff/p/8991847.html
C++調用
隱式鏈接
Windows中的庫編程