#ifdef CONTEXT_EXPORTS
#define CONTEXT_API __declspec (dllexport)
#else
#define CONTEXT_API __declspec (dllimport)
#endifclass BaseContext
{ }
class NodeContext: public BaseContext
{ }
class ElementContext: public NodeContext
{ }
问题:
1. 如果外界要使用ElementContext类, 只需要在BaseContext前家导出宏吗?
class CONTEXT_API BaseContext
{ }
2. 如果要使用显式调用DLL,而外界使用的类ElementContext有一个带参赛的构造,该如何使用?
#define CONTEXT_API __declspec (dllexport)
#else
#define CONTEXT_API __declspec (dllimport)
#endifclass BaseContext
{ }
class NodeContext: public BaseContext
{ }
class ElementContext: public NodeContext
{ }
问题:
1. 如果外界要使用ElementContext类, 只需要在BaseContext前家导出宏吗?
class CONTEXT_API BaseContext
{ }
2. 如果要使用显式调用DLL,而外界使用的类ElementContext有一个带参赛的构造,该如何使用?
2. 在DLL中的类ElementContext,在客户端这样使用:
ElementContext* root1 = NULL;
编译的出现这样的错误:
error C2220: warning treated as error - no object file generated
warning C4189: 'root1' : local variable is initialized but not referenced
Demo.h//
class Demo
{
public:
Demo();
Demo(char* pChar)
void Display() const;
long Get() const;
void Set(int pInt);
private:
long m_Long;
}
当在客户端使用显示调用时:
#include "Demo.h"typedef void (CALLBACK *PDISPLAY) ();main(int argc, char** argv)
{
HMODULE hMod = LoadLibrary ("Demo.dll");
if (NULL == hMod)
{
printf ("LoadLibrary failed\n");
return 1;
}
PDISPLAY pDisplay = (PDISPLAY) GetProcAddress(hMod, "Display");
if (NULL == pDisplay)
{
printf ("GetProcAddress Display failed\n");
return 1;
}
pDisplay("Hello");
假设这样可以成功,那么是不是说用explicit linking方法调用DLL的客户端,都不能通过对象调用类中的方法(即Demo test; test.Display("Hello"); ),而必须使用函数指针。
可以导出你dll中的任何可访问的元素。
DLL//
class Demo1
{
public:
Demo1() {}
~Demo1() {} void Display() const {...};
}
class Demo2
public:
Demo2() {}
~Demo2() {} void Display() const {...}
}
Client//
typedef void (*Dispaly)();main()
{
Display pDisplay;
//如果要使用Demo1中的Dispaly()怎么办?
}