DLL初始化的问题

liuyanrenliang 2008-11-10 10:55:28
各位大虾,请教了:
我想在DLL中封装一个类A,并在客户端程序显式地去调用此类,我该怎么去NEW该类呢?我不想在程序的开始设置:#program comment(lib,"A.LIB")或者在PROJECT->SETTING->LINK中去设置LIB,而是想通过程序来动态调用。
如程序所示:
A_EXAMPLE.DLL:
class AFX_EXT_CLASS A
{}
客户端程序:
int main()
{
HINSTANCE hIns = AfxLoadLibrary("A.DLL"); //获取DLL句柄
if(hIns != NULL)
{
//这里想得到类A实例化的指针
A* m_pA = new A; //这里编译提示错误,该怎么改呢?
}
}
这里我该怎么写呢?谢谢各位。
...全文
243 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
yayafu 2008-11-10
  • 打赏
  • 举报
回复
就这样了
ilovedrv 2008-11-10
  • 打赏
  • 举报
回复
我比较奇怪的是楼主为什么要把一件简单的事情弄复杂了,

:#program comment(lib,"A.LIB")或者在PROJECT->SETTING->LINK中去设置LIB

明明可以解决问题,确非要动态加载类,这个没试过,不知道怎么做

当然可以,在动态库里面做接口,包装该类,但这样不是多此一举码,导出类就是为了简单
cnzdgs 2008-11-10
  • 打赏
  • 举报
回复
如果要导出类,必须使用lib。
如果不导出类,只导出函数,可以用句柄的方式,导出一个CreateXxx函数,这个函数中用new构造类对象,然后把对象指针作为句柄返回,另外导出一个CloseXxx函数,函数中用delete释放对象,对象指针以句柄形式通过参数传递进来,函数内部做类型转换。再导出类的public成员函数,每个函数都加上一个句柄类型的参数给出对象指针,内部进行转换。
laolaoliu2002 2008-11-10
  • 打赏
  • 举报
回复
直接把类的指针当参数返回。
dll导出函数增加一个函数
A* createAclass()
{
return new A();
}
shailen126 2008-11-10
  • 打赏
  • 举报
回复
直接在DLL的代码中创建一个这个类的对象,不知道这个方法能不能达到你的要求
liuyanrenliang 2008-11-10
  • 打赏
  • 举报
回复
laolaoliu2002:首先谢谢你的回答,但是我说过了,不想在程序开始加上#pragma comment(lib,"dllTest.lib");
laolaoliu2002 2008-11-10
  • 打赏
  • 举报
回复
http://www.programsalon.com/downloads32/sourcecode/windows/detail102044.html----这里有例子代码。
http://www.pconline.com.cn/pcedu/empolder/gj/vc/0509/699672_9.html----这里也有例子代码。
laolaoliu2002 2008-11-10
  • 打赏
  • 举报
回复
DLL中定义的类可以在应用工程中使用。

  下面的例子里,我们在DLL中定义了point和circle两个类,并在应用工程中引用了它们(单击此处下载本工程附件)。

//文件名:point.h,point类的声明

#ifndef POINT_H

#define POINT_H

#ifdef DLL_FILE

class _declspec(dllexport) point //导出类point

#else

class _declspec(dllimport) point //导入类point

#endif

{

public:

float y;

float x;

point();

point(float x_coordinate, float y_coordinate);

};

#endif


//文件名:point.cpp,point类的实现

#ifndef DLL_FILE

#define DLL_FILE

#endif

#include "point.h"

//类point的缺省构造函数

point::point()

{

x = 0.0;

y = 0.0;

}

//类point的构造函数

point::point(float x_coordinate, float y_coordinate)

{

x = x_coordinate;

y = y_coordinate;

}


//文件名:circle.h,circle类的声明

#ifndef CIRCLE_H

#define CIRCLE_H

#include "point.h"

#ifdef DLL_FILE

class _declspec(dllexport)circle //导出类circle

#else

class _declspec(dllimport)circle //导入类circle

#endif

{

public:

void SetCentre(const point ¢rePoint);

void SetRadius(float r);

float GetGirth();

float GetArea();

circle();

private:

float radius;

point centre;

};

#endif


//文件名:circle.cpp,circle类的实现

#ifndef DLL_FILE

#define DLL_FILE

#endif

#include "circle.h"

#define PI 3.1415926

//circle类的构造函数

circle::circle()

{

centre = point(0, 0);

radius = 0;

}

//得到圆的面积

float circle::GetArea()

{

return PI *radius * radius;

}

//得到圆的周长

float circle::GetGirth()

{

return 2 *PI * radius;

}

//设置圆心坐标

void circle::SetCentre(const point ¢rePoint)

{

centre = centrePoint;

}

//设置圆的半径

void circle::SetRadius(float r)

{

radius = r;

}

类的引用:


#include "..\circle.h"  //包含类声明头文件

#pragma comment(lib,"dllTest.lib");


int main(int argc, char *argv[])

{

circle c;

point p(2.0, 2.0);

c.SetCentre(p);

c.SetRadius(1.0);

printf("area:%f girth:%f", c.GetArea(), c.GetGirth());


return 0;

}


  从上述源代码可以看出,由于在DLL的类实现代码中定义了宏DLL_FILE,故在DLL的实现中所包含的类声明实际上为:


class _declspec(dllexport) point //导出类point

{



}


  和


class _declspec(dllexport) circle //导出类circle

{



}


  而在应用工程中没有定义DLL_FILE,故其包含point.h和circle.h后引入的类声明为:


class _declspec(dllimport) point //导入类point

{



}


  和


class _declspec(dllimport) circle //导入类circle

{



}

comiunknown 2008-11-10
  • 打赏
  • 举报
回复
参数这样传
RAPIINIT ri;
if (pDllCeRapiInitEx)
(*pDllCeRapiInitEx)(&ri);
comiunknown 2008-11-10
  • 打赏
  • 举报
回复
楼主是想用函数指针来创建dll的类对象吧?
类似代码如下:
.h添加定义
typedef HRESULT (WINAPI *DLLCeRapiInitEx)(RAPIINIT*);
STDAPI CeRapiInitEx(RAPIINIT*);

.cpp中这样调用
DLLCeRapiInitEx pDllCeRapiInitEx;
g_hRapi = LoadLibrary( _T( "rapi.dll" ) );
pDllCeRapiInitEx = (DLLCeRapiInitEx)GetProcAddress( g_hRapi, "CeRapiInitEx");

路人乙2019 2008-11-10
  • 打赏
  • 举报
回复
模拟一个COM程序吧。
凤矶 2008-11-10
  • 打赏
  • 举报
回复
仿照一下COM,在DLL里建好一个对象(当然用指针保存),加一个接口“GetXXXObject()”返回指针。
bbs008 2008-11-10
  • 打赏
  • 举报
回复
差不多了

15,471

社区成员

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

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