
正文
Windows动态链接库:dll与exe相互调用问题
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
本文回顾学习一下Windows动态链接库:dll与exe相互调用问题。一般滴,exe用来调用dll中的类或函数,但是dll中也可以调用exe中的类或函数,本文做一些尝试总结。
dll程序:
CoolFormat Source Formatter http://blog.csdn.net/akof1314
Calculator.h
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
// Calculator.h: interface for the Calculator class.
// //////////////////////////////////////////////////////////////////////
#if !defined(AFX_ALCULATOR_H__D072BBB5_615B_45A9_9864_80C2291FA695__INCLUDED_)
#pragma once
class IHelper;
void registerHelper(IHelper *iHelper);
extern "C" _declspec(dllexport) Calculator *getCalculator(); #endif // !defined(AFX_ALCULATOR_H__D072BBB5_615B_45A9_9864_80C2291FA695__INCLUDED_) |
CoolFormat Source Formatter http://blog.csdn.net/akof1314
Calculator.cpp
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
// Calculator.cpp: implementation of the Calculator class.
// //////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "../vs_dll_caller/Helper.h"
//////////////////////////////////////////////////////////////////////
Calculator::Calculator()
Calculator::~Calculator()
void Calculator::registerHelper(IHelper *iHelper)
void Calculator::registerHelper(Helper *helper)
int Calculator::add(int num1, int num2)
int Calculator::sub(int num1, int num2)
Calculator *getCalculator()
|
exe程序:
CoolFormat Source Formatter http://blog.csdn.net/akof1314
IHelper.h
|
1
2 3 4 5 6 7 8 9 10 11 |
#pragma once
/************************************************************************/ // 抽象类 */ // 该类在exe中,方便dll中调用 /************************************************************************/
class IHelper
|
CoolFormat Source Formatter http://blog.csdn.net/akof1314
Helper.h
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
// Helper.h: interface for the Helper class.
// //////////////////////////////////////////////////////////////////////
#if !defined(AFX_HELPER_H__9234DC8C_56CC_4765_BDA6_276F30C52221__INCLUDED_)
#pragma once
#include "iHelper.h"
class Helper
virtual void print(const char *str); }; #endif // !defined(AFX_HELPER_H__9234DC8C_56CC_4765_BDA6_276F30C52221__INCLUDED_) |
CoolFormat Source Formatter http://blog.csdn.net/akof1314
Helper.cpp
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
// Helper.cpp: implementation of the Helper class.
// //////////////////////////////////////////////////////////////////////
#include "Helper.h"
//////////////////////////////////////////////////////////////////////
Helper::Helper()
}
Helper::~Helper()
}
void Helper::print(const char *str)
|
CoolFormat Source Formatter http://blog.csdn.net/akof1314
main.cpp
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#include <Windows.h>
#include <tchar.h> #include <iostream> #include "Helper.h" #include "../vs_dll/Calculator.h" typedef Calculator*(*Cal)();
int main()
// 隐式调用
// 显式调用
}
std::cin.get();
|
总结 :一般滴,dll是用来被exe调用的,如果dll要调用exe中的类,可以给dll注册一个该类的指针即可,而且该指针为抽象类指针。






![【单机游戏】[角色扮演] BS1277 [PC游戏][偷窃少女的教育方法][DLL个人汉化版][50M] 【单机游戏】[角色扮演] BS1277 [PC游戏][偷窃少女的教育方法][DLL个人汉化版][50M]](https://www.04ip.com/template/qe/style/noimg/11.jpg)
