动态调用DLL函数总是失败

heaoo 2007-03-24 10:56:28
//************mymath.h********************************
#ifndef _MATH_H_
#define _MATH_H_

#ifdef __cplusplus
#define EXPORT extern "C" __declspec(dllexport)
#else
#define EXPORT __declspec(dllexport)
#endif

#define DLLENTRY WINAPI

EXPORT int CALLBACK sub(int,int);
EXPORT int CALLBACK add(int,int);
EXPORT int CALLBACK mut(int,int);


#endif

//****************mymath.cpp**********************************
#include<windows.h>
#include"math.h"

int DLLENTRY DllMain(HINSTANCE hInstance,DWORD fdwReason,PVOID pvReserved)
{
return TRUE;
}

EXPORT int CALLBACK sub(int x,int y)
{
return x-y;
}

EXPORT int CALLBACK add(int x,int y)
{
return x+y;
}

EXPORT int CALLBACK mut(int x,int y)
{
return x*y;
}


//**************main.cpp*******************************************
#include "windows.h"

typedef int ( CALLBACK * PFUN)(int,int);

int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{

HINSTANCE hDll;
PFUN psub;

hDll=LoadLibrary("mymath.dll");

//测试加载mymath.dll是否成功
if(hDll==NULL)
{
MessageBox(NULL,TEXT("Fail to load math.dll "),TEXT("ERROR"),MB_OK|MB_ICONWARNING);
return 0;
}

psub=PFUN(GetProcAddress(hDll,"sub"));

  //测试取得函数sub地址是否成功(总是在这儿失败)
if(psub==NULL)
{
MessageBox(NULL,TEXT("Fail to load function sub "),TEXT("ERROR"),MB_OK|MB_ICONWARNING);
return 0;
}


return 0;
}

上面程序中,psub总是等于NULL,为什么不能成功调用函数sub?


...全文
246 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
WingForce 2007-03-24
  • 打赏
  • 举报
回复
psub=PFUN(GetProcAddress(hDll,"sub"));

后面调用一下GetLastError看看是虾米原因

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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