新手求救高手(dll)
我写了一个最简单的dll程序,刚刚学习,只想测试最简单的功能
dll文件是win32dll,引入dll采用显示加载的方法。
源程序如下,感觉没错。Loadlibrary也能成功,但就是GetProcAddress怎么也成功不了。
定义Dll,用了vc6向导生成的程序:
//Dll1.h
#ifdef DLL1_EXPORTS
#define DLL1_API __declspec(dllexport)
#else
#define DLL1_API __declspec(dllimport)
#endif
// This class is exported from the Dll1.dll
class DLL1_API CDll1 {
public:
CDll1(void);
// TODO: add your methods here.
};
extern DLL1_API int nDll1;
DLL1_API int fnDll1(void);
// Dll1.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include "Dll1.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
// This is an example of an exported variable
DLL1_API int nDll1=0;
// This is an example of an exported function.
DLL1_API int fnDll1(void)
{
return 42;
}
// This is the constructor of a class that has been exported.
// see Dll1.h for the class definition
CDll1::CDll1()
{
return;
}
使用DLL: 在MFC程序的OnDraw里加入如下代码进行测试
void CDLL123View::OnDraw(CDC* pDC)
{
CDLL123Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
HINSTANCE hDLL;
typedef int (CALLBACK* LPFN)(void);
LPFN lpfn;
hDLL=LoadLibrary("DLL1");
if(hDLL != NULL)
{
lpfn=(LPFN)GetProcAddress(hDLL,"fnDll1");
if(!lpfn)
{
FreeLibrary(hDLL);
AfxMessageBox("出错");
}
else{
int a;
a=lpfn();
AfxMessageBox("成功");
}
}
// TODO: add draw code for native data here
}
结果调试的时候,lpfn每次都等于0x0000000.
试了好多次了,毕设要用到Dll,但最简单的都掌握不了,大虾救命呀!!!!!
问题点数:0、回复次数:2Top
1 楼vcforever(累)回复于 2004-05-01 01:36:34 得分 0
在你的程序中添加一个.def文件,把你的导出函数写在EXPORTS节下面
EXPORTS
你的导出函数名称
然后在使用显式加载的方法就可以了!Top
2 楼cdcjk(攀辉)回复于 2004-05-02 11:19:45 得分 0
upTop




