怎样调用DLL中的函数类?

Noripchen 2005-09-26 10:49:40
有如下的DLL定义:

/////////////////////////////////////////////////////////
--------------------MyDll.h----------------------
#ifdef MYDLL_EXPORTS
#define MYDLL_API __declspec(dllexport)
#else
#define MYDLL_API __declspec(dllimport)
#endif

// This class is exported from the MyDll.dll
class MYDLL_API CMyDll {
public:
CMyDll(void);
// TODO: add your methods here.
int Add(int,int);

};

extern MYDLL_API int nMyDll;

MYDLL_API int fnMyDll(void);

MYDLL_API int __stdcall add(int a,int b);
---------------------MyDll.cpp-----------------------
#include "stdafx.h"
#include "MyDll.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
MYDLL_API int nMyDll=0;

// This is an example of an exported function.
MYDLL_API int fnMyDll(void)
{
return 42;
}
MYDLL_API int __stdcall add(int a,int b)
{
return (a+b);
}

// This is the constructor of a class that has been exported.
// see MyDll.h for the class definition
CMyDll::CMyDll()
{
return;
}
int CMyDll::Add(int a,int b)
{
return a+b;
};
-----------------------------------MyDll.def----------------------
LIBRARY MyDll
EXPORTS
add @1

////////////////////////////////////////////////////////

如果要调用函数类的成员函数是不是这样:
typedef int (*PF)(int,int);
PF pfun;
pf=(PF)LoadLibrary(hDll,"CMyDll::Add");

或者不是或者根本不可以?
...全文
166 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jagen在路上 2005-09-27
  • 打赏
  • 举报
回复
但是你能保证类中所有的函数都是静态的?那还有什么用呢?
Jagen在路上 2005-09-27
  • 打赏
  • 举报
回复
呵呵,楼上的,那自然没有问题了。
Noripchen 2005-09-27
  • 打赏
  • 举报
回复
成员的调用需要传递this指针给他,否则访问不了类的成员变量了????

如果成员函数是静态函数呢?
Jagen在路上 2005-09-26
  • 打赏
  • 举报
回复
不可以的,成员的调用需要传递this指针给他,否则访问不了类的成员变量了。
不过有一个方法是,利用一个C函数,传递一个虚基类的指针,这有点类似于COM的方法,比较管用。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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