#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有一个带参赛的构造,该如何使用?

解决方案 »

  1.   

    要用什么类就导出什么类,外界要使用ElementContext类,就导出该类class CONTEXT_API ElementContext,只导出基类是没用的。
      

  2.   

    1. 如果外界使用的类ElementContext在DLL中使用了其他的类, 这些类需不需要加导出宏?
    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
      

  3.   

    现在DLL中的类是这样的:
    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"); ),而必须使用函数指针。
      

  4.   

    一样可以,入口不同而己,如你导出整个类,new 出这个类就可直接用,如你直导出类中的某个方法(可通过def定义)也可以,即导出类同时导出函数也可以。
    可以导出你dll中的任何可访问的元素。
      

  5.   

    还有些不明白, 如果使用new那么就要使用*.h头文件(不想暴露头文件)。现在两个类中有两个相同的名的函数。
    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()怎么办?
    }