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