LIB
使用VS编译
先建立一个静态链接库,
#pragma once #include<Windows.h> void Lib();
#include "pch.h" #include "framework.h" #include "StaticLib1.h"
void Lib() { MessageBox(NULL, L"Lib", L"you", MB_OK); }
|
生成
然后建立一个空项目调用生成的lib文件
#include"StaticLib1.h" #include<stdio.h> #pragma comment(lib,"./StaticLib1.lib") int main() { Lib(); return 0; }
|
即可实现调用
DLL
生成
#include "pch.h" #include<Windows.h> BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: MessageBox(NULL, L"DLL_PROCESS_ATTACH",L"dll",MB_OK); break; case DLL_THREAD_ATTACH: MessageBox(NULL, L"DLL_THREAD_ATTACH", L"dll", MB_OK); break; case DLL_THREAD_DETACH: MessageBox(NULL, L"DLL_THREAD_DETACH", L"dll", MB_OK); break; case DLL_PROCESS_DETACH: MessageBox(NULL, L"DLL_PROCESS_DETACH", L"dll", MB_OK); break; } return TRUE; }
|
调用
#include<stdio.h> #include<Windows.h> int main() { HMODULE hMod = LoadLibrary("Dll1.dll"); FreeLibrary(hMod); return 0; }
|
但这样dll是无输出表的,且不光要在DLL中写函数,而且需要声明
#pragma once #include"pch.h" #include <Windows.h> void __declspec(dllexport) show();
#include"1.h" #include "pch.h" void show() { MessageBox(NULL, L"Show", L"DLL", MB_OK); }
|
调用
#include<stdio.h> #include<Windows.h> typedef void(*SHOW)(); int main() { HMODULE hMod = LoadLibrary("Dll1.dll"); SHOW m = (SHOW) GetProcAddress(hMod,"show"); m(); FreeLibrary(hMod); return 0; }
|
或者export 建立一个def
MFC 动态链接库
和上面的动态链接差不多,等学了MFC再详细写