Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

dll

c++-cli

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.

like image 233
Sujay Ghosh Avatar asked Apr 19 '12 10:04

Sujay Ghosh


People also ask

How do I export a class from a DLL?

Use __declspec(dllexport) to export the function or method. You can export an entire C++ class by placing the __declspec(dllexport) before the class name, or you can export a single method by placing __declspec(dllexport) before the method name. Make class methods static and public.

What does __ Declspec Dllexport do?

__declspec(dllexport) adds the export directive to the object file so you do not need to use a . def file. This convenience is most apparent when trying to export decorated C++ function names.

Which keyword can be used to disable name decoration when exporting a function from a DLL?

dllexport of a C++ function will expose the function with C++ name mangling. To disable name mangling either use a . def file (EXPORTS keyword) or declare the function as extern “C”.


1 Answers

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 {
...
};
like image 129
Tassos Avatar answered Sep 20 '22 12:09

Tassos