dll和lib
一般的动态库程序有lib文件和dll文件。
lib文件时必须在编译器就连接到应用程序中的,而dll文件时运行期才会被调用的。
如果有dll文件,那么对于的lib文件一般是一些索引信息,具体的实现在dll文件中。
如果只有lib文件,那么这个lib文件是静态编译出来的,索引和实现都在其中。
dll文件生成
头文件
源文件
C++项目引用
//dll import
#include <Windows.h>
#if false
typedef int (*function)(int a);
int main()
{
//HMODULE module = LoadLibraryA("Dll_CreateExample.dll");
HINSTANCE hDllInst = LoadLibrary(L"Dll_CreateExample.dll");
if (hDllInst)
{
function func = (function)GetProcAddress(hDllInst, "Factorial");
printf("%d \n", func(9));
}
system("pause");
std::cout << "Hello World!\n";
}
#endif
//lib import
//动态调用主要用到LoadLibrary,GetProcAddress和FreeLibrary三个函数
#if true
#define DllImport __declspec( dllimport )
#pragma comment (lib,"Dll_CreateExample.lib")
extern "C" DllImport int Factorial(int n);
int main()
{
std::cout << "Hello World!\n";
std::cout << Factorial(19) << std::endl;
}
#endif
c#项目引用dll
.....
using System.Runtime.InteropServices;
.......
[DllImport("Dll_CreateExample.dll", CallingConvention = CallingConvention.Cdecl)]
........
private void ViewContextInit()
{
tbxTips.Text = Factorial(4).ToString();
}
......