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

前提是你 提交于 2019-11-30 07:37:24

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 {
...
};

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.

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).

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