Windows制作动态库(.dll)

2022-07-08 09:17:40

文件目录如下:
在这里插入图片描述
将项目dll_providerConfiguration Type设置为Dynamic Library (.dll)
文件内容如下:

IInterface.h

#ifndefIINTERFACE_H_#defineIINTERFACE_H_#include<windows.h>#defineXEXPORT__declspec(dllexport)extern"C"{
	XEXPORTvoid __cdeclPrint(constchar*s);
	XEXPORTint __cdeclIntegerAdd(constint a,constint b);}#endif

IInterface.cpp

#include"IInterface.h"#include<iostream>voidPrint(constchar*s){
    std::cout<< s<< std::endl;}intIntegerAdd(constint a,constint b){return a+ b;}

example.cpp

#include<windows.h>#include<iostream>#include"../dll_provider/IInterface.h"typedefvoid(*pFunc1)(constchar*s);typedefint(*pFunc2)(constint a,constint b);intmain(){
	HINSTANCE hDLL=LoadLibrary("dll_provider.dll");if(hDLL!=nullptr){
		pFunc1 Print=(pFunc1)GetProcAddress(hDLL,"Print");if(Print!=nullptr){Print("Hello World");}else{
			std::cerr<<"Print is nullptr"<< std::endl;}
		pFunc2 IntegerAdd=(pFunc2)GetProcAddress(hDLL,"IntegerAdd");if(IntegerAdd!=nullptr){int a=1, b=2;
			std::cout<<"a + b = "<<IntegerAdd(a, b)<< std::endl;}else{
			std::cerr<<"Integer is nullptr"<< std::endl;}}else{
		std::cerr<<"hDll is nullptr"<< std::endl;}return0;}
  • 作者:walsons
  • 原文链接:https://blog.csdn.net/weixin_44720401/article/details/122176139
    更新时间:2022-07-08 09:17:40