用模块文件创建DLL,如何进行隐式调用?

woshihuzi 2017-09-19 03:41:34
我用声明导出函数的办法,做出的DLL能够进行显式调用和隐式调用,但用模块文件导出的DLL,只能进行显式调用,目前还没掌握隐式调用的方法,特来请教各位高手。

//DllTest.h
#ifdef DLL_EXPORT
#define DLL_API extern "C" _declspec(dllexport)
#else
#define DLL_API extern "C" _declspec(dllimport)
#endif
DLL_API int add(int a,int b);


//DllTest.cpp
#define DLL_EXPORT
#include "DllTest.h"
int add(int a,int b)
{
return a+b;
}

//隐式调用代码
#include <stdio.h>
#include <conio.h>
#include "DllTest.h"
#pragma comment(lib, "DllTest.lib")
int main()
{
int a = 8, b = 3;
printf("%d+%d=%d\n", a, b, add(a, b));
getch();
return 0;
}

请问用模块文件创建的DLL没有头文件,我们如何进行隐式调用?
LIBRARY DllTest
EXPORTS
add

//DllTest.cpp
int add(int a, int b)
{
return a+b;
}
...全文
982 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
woshihuzi 2017-09-20
  • 打赏
  • 举报
回复
谢谢各位兄弟,结贴。
woshihuzi 2017-09-20
  • 打赏
  • 举报
回复
我自己找到答案了,记录一下: 必须自己写一个头文件DllTest.h,不能照搬如下的内容: /DllTest.h #ifdef DLL_EXPORT #define DLL_API extern "C" _declspec(dllexport) #else #define DLL_API extern "C" _declspec(dllimport) #endif DLL_API int add(int a,int b); 我从网上查到隐式调用模块文件生成DLL时,函数声明必须跟生成DLL时的函数定义完全一致,包括修饰符。 我照搬DllTest.h的时候,函数声明是:extern "C" _declspec(dllimport) int add(int a,int b); 而生成DLL时,这些修饰符是不存在的。不一致就会编译出错。 自己编写的头文件如下: int add(int a,int b); int add(int a,int b);之所以出错,就是因为两者不一致。 自己写的DllTest.h内容如下: int add(int a,int b);
woshihuzi 2017-09-20
  • 打赏
  • 举报
回复
使用GetProcAddress得到你要调用的函数地址,然后调用,具体用法网上很多讲解 ===================== 这个属于显式加载,我问的是隐式加载如何能编译成功并运行程序。 没有引用dll工程中的Lib ===================== 如下的一句就是引入Lib文件 #pragma comment(lib, "DllTest.lib")
真相重于对错 2017-09-19
  • 打赏
  • 举报
回复
没有引用dll工程中的Lib
paschen 2017-09-19
  • 打赏
  • 举报
回复
使用GetProcAddress得到你要调用的函数地址,然后调用,具体用法网上很多讲解
lzlliuzunli 2017-09-19
  • 打赏
  • 举报
回复
写一个头文件
woshihuzi 2017-09-19
  • 打赏
  • 举报
回复
用下面的这个方法写头文件也不行 //DllTest.h extern "C" _declspec(dllimport) int add(int a, int b); #include <stdio.h> #include <conio.h> #include "DllTest.h" int main() { int a = 8, b = 3; printf("%d+%d=%d\n", a, b, add(a, b)); getch(); return 0; } 编译还是提示出错: 隐式加载DLL.obj : error LNK2001: unresolved external symbol __imp__add
woshihuzi 2017-09-19
  • 打赏
  • 举报
回复
如果仍然用上面的头文件和调用方法的话,编译会出错。 //DllTest.h #ifdef DLL_EXPORT #define DLL_API extern "C" _declspec(dllexport) #else #define DLL_API extern "C" _declspec(dllimport) #endif DLL_API int add(int a,int b); //隐式调用代码 #include <stdio.h> #include <conio.h> #include "DllTest.h" #pragma comment(lib, "DllTest.lib") int main() { int a = 8, b = 3; printf("%d+%d=%d\n", a, b, add(a, b)); getch(); return 0; } 编译错误: error LNK2001: unresolved external symbol __imp__add

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧