DLL简单的例子无法调试成功???

xuxian02092213 2008-10-28 07:43:45
在Windows核心编程第五版上有段关于隐式连接的例子,可是我怎么调试都不通,代码如下:


// DLL下的.h
#ifdef MYLIBAPI
#else
#define MYLIBAPI extern "C" __declspec(dllexport)
#endif

MYLIBAPI int g_nResult;

MYLIBAPI int Add(int nLeft,int nRight);


//DLL下的CPP文件
#include <windows.h>
#define MYLIBAPI extern "C" __declspec(dllexport)

#include "MyLib.h"

int g_nResult;

int Add(int nLeft,int nRight)
{
return g_nResult=nLeft+nRight;
}



//exe端的cpp文件

#include <windows.h>
//#include <strsafe.h>
#include <stdio.h>
#include <stdlib.h>

#include "D:\Test\TEST\Dll\MyLib.h"
void main()
{
int nLeft=10,nRight=25;

printf("The 10+25=%d\n and the g_nResult=%d",Add(nLeft,nRight),g_nResult);
}


在vc6.0下和VS2005环境下都无法调试通过,首先是将DLL端编译生成,然后在编译exe的cpp文件,都是说找不到
Add()
和那个g_nResult,不知道那位高手能指点下,不胜感激!!
...全文
120 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
太乙 2008-10-28
  • 打赏
  • 举报
回复


还要把那个.dll文件拷贝到.exe工程目录下!!!!
Zark 2008-10-28
  • 打赏
  • 举报
回复
隐式连接需要下列三个条件:

A header file (.h file) containing the declarations of the exported functions and/or C++ classes. The classes, functions, and data should all have __declspec(dllimport), for more information, see dllexport, dllimport.

An import library (.LIB files) to link with. (The linker creates the import library when the DLL is built.)

The actual DLL (.dll file).

头文件,
IMPORT 库文件.
DLL 文件.

太乙 2008-10-28
  • 打赏
  • 举报
回复


#pragma comment( lib, "D:\\Test\\TEST\\Dll\\debug\\MyLib.lib" )


没有这句,你只包含个头文件咋弄??

太乙 2008-10-28
  • 打赏
  • 举报
回复
呵呵,lz不好意思哈~~漏了一点:



下面是完整的代码:



// DLL下的.h
#ifdef MYLIBAPI
#else
#define MYLIBAPI extern "C" __declspec(dllexport)
#endif

MYLIBAPI int g_nResult;

MYLIBAPI int Add(int nLeft,int nRight);


//DLL下的CPP文件
#include <windows.h>
#define MYLIBAPI extern "C" __declspec(dllexport)

#include "MyLib.h"

int g_nResult;

int Add(int nLeft,int nRight)
{
return g_nResult=nLeft+nRight;
}



//exe端的cpp文件

#include <windows.h>
//#include <strsafe.h>
#include <stdio.h>
#include <stdlib.h>

#include "D:\Test\TEST\Dll\MyLib.h"
#pragma comment( lib, "D:\\Test\\TEST\\Dll\\debug\\MyLib.lib" )
void main()
{
int nLeft=10,nRight=25;

printf("The 10+25=%d\n and the g_nResult=%d",Add(nLeft,nRight),g_nResult);
}





呵呵,不好意思咯!


太乙 2008-10-28
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 xuxian02092213 的回复:]
我在.h端改为 dllimport了,在.cpp端改为 dllexport了,但是还是不行啊,还请赐教啊,5楼的是怎么调通的啊
[/Quote]

我晕~~~~~


// DLL下的.h
#ifdef MYLIBAPI
#else
#define MYLIBAPI extern "C" __declspec(dllimport)
#endif

MYLIBAPI int g_nResult;

MYLIBAPI int Add(int nLeft,int nRight);


//DLL下的CPP文件
#include <windows.h>
#define MYLIBAPI extern "C" __declspec(dllimport)

#include "MyLib.h"

int g_nResult;

int Add(int nLeft,int nRight)
{
return g_nResult=nLeft+nRight;
}



//exe端的cpp文件

#include <windows.h>
//#include <strsafe.h>
#include <stdio.h>
#include <stdlib.h>

#include "D:\Test\TEST\Dll\MyLib.h"
void main()
{
int nLeft=10,nRight=25;

printf("The 10+25=%d\n and the g_nResult=%d",Add(nLeft,nRight),g_nResult);
}




xuxian02092213 2008-10-28
  • 打赏
  • 举报
回复
我在.h端改为 dllimport了,在.cpp端改为 dllexport了,但是还是不行啊,还请赐教啊,5楼的是怎么调通的啊
三文鱼也会飞 2008-10-28
  • 打赏
  • 举报
回复
LZ你没有import呀
lijinfenghust 2008-10-28
  • 打赏
  • 举报
回复
dll的那个工程下面会生成一个 lib文件 需要在exe工程 的属性 链接器->命令行 里面输入这个lib
另外 需要把dll 和lib文件都放到exe的工程下面
帅得不敢出门 2008-10-28
  • 打赏
  • 举报
回复
VC中如何调用DLL中的函数
http://hi.baidu.com/oceaning/blog/item/d2b1c654b4e33e1a3a293535.html
太乙 2008-10-28
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xuxian02092213 的回复:]
我写的是隐式连接,似乎不需要LoadLibrary函数的吧,那个可是显示连接的哦
[/Quote]

呵呵,上面我是测试了一下下,你照下面改!~
太乙 2008-10-28
  • 打赏
  • 举报
回复
lz有俩地儿错了:



既然是隐式链接,那么:

#define MYLIBAPI extern "C" __declspec(dllexport)
改为:
#define MYLIBAPI extern "C" __declspec(dllimport)


两处都得改~~

改了就好使!

xuxian02092213 2008-10-28
  • 打赏
  • 举报
回复
我写的是隐式连接,似乎不需要LoadLibrary函数的吧,那个可是显示连接的哦
太乙 2008-10-28
  • 打赏
  • 举报
回复
#include <windows.h>
//#include <strsafe.h>
#include <stdio.h>
#include <stdlib.h>

//#include "C:\Documents and Settings\Administrator\桌面\test\dll\dll.h"
typedef int (*padd)(int,int);
void main()
{
int nLeft=10,nRight=25;
HMODULE module = ::LoadLibrary("C:\\Documents and Settings\\Administrator\\桌面\\test\\dll\\Debug\\dll.dll");
padd pa = (padd)GetProcAddress(module,"Add");
printf("The 10+25=%d\n ",pa(nLeft,nRight));
}
xuxian02092213 2008-10-28
  • 打赏
  • 举报
回复
先建立一个普通的工程啊,然后在这个工程中添加Dll工程啊,这个有何不对吗
太乙 2008-10-28
  • 打赏
  • 举报
回复
晕,lz建的可是dll???

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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