How do I export class functions, but not the entire class in a DLL

别等时光非礼了梦想. 提交于 2019-12-30 02:26:05

问题


I have developed a Win32 DLL, providing the details below, and want to create a CLI/C++ wrapper for the functions Connnect and LogOut.

I know that entire classes and functions can be exported from a DLL.

class CClientLib
{
 public:
CClientLib (void);
// TODO: add your methods here.
__declspec(dllexport) bool Connect(char* strAccountUID,char* strAccountPWD);
__declspec(dllexport) void LogOut();

 private :

    Account::Ref UserAccount ;
void set_ActiveAccount(Account::Ref act)
{
   // Set the active account
}

Account::Ref get_ActiveAccount()
{
  return UserAccount;
    }

};

I want to have the class as the exported functions, Connect and LogOut, uses the function set / get.

Is it possible only to export the functions Connect and LogOut, and not the entire class.


回答1:


I would recommend to declare an interface that will be exported and then implement it by your internal class.

class __declspec(dllexport) IClientLib {
 public:
    virtual bool Connect(char* strAccountUID,char* strAccountPWD) = 0;
    virtual void LogOut() = 0;
};

class CClientLib: public IClientLib {
...
};



回答2:


Consider what you are asking about... both Connect and Logout are non-static member functions, which means that you need to call them on an instance of the type: intense.Connect(). Now, if you do not export the class itself, you will not be able to create the instance, and whether Connect and Logout are exported or not makes no difference, they would be unusable.

An alternative would be not to export any of them, and provide exported free functions with the same functionality that apply to some instance of your class that is not visible outside of your DLL. Alternatively offer a third function that creates a handle into your library, where handle could be an opaque (void*) pointer to a dynamically allocated instance of your type, and then pass the handle as an extra argument to your free function versions of Connect and Logout. You will also need to add a fourth function to release that object.




回答3:


You can make it by simply creating two functions wich call the desired class methods end export them. i.e.

CCLientLib g_clientLib;

__declspec(dllexport) void CClientLib_LogOut()
{
    g_clientLib.LogOut();
}

But I don't understand why you need this, since it's enough to mark the methods you don't want to be accessible with the "private" modifier (as done in your example).



来源:https://stackoverflow.com/questions/10226580/how-do-i-export-class-functions-but-not-the-entire-class-in-a-dll

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!