How to import C ++ class dll into namespace

I read several documents that give simple examples of functions compatible with C.

__declspec(dllexport) MyFunction();

I'm against it. I am writing a small application using the functions of this DLL. I used an explicit connection with

LoadLibrary() 

function. C style functions work without problems. But when I write my class like

namespace DllTest
{
class Test
{
public:
    __declspec(dllexport) Test();
    __declspec(dllexport) void Function( int );
    __declspec(dllexport) int getBar(void);
private:
    int bar;
};

}
#endif

It compiles and is created by Dll. While working with C style functions, I simply used a pointer to the functions from the LoadLibrary () and GetProcAddress (...) functions.

My previous use

typedef void (*Function)(int);

int main()
{
   Function _Function;
   HINSTANCE hInstLibrary = LoadLibrary(TEXT("test.dll"));

   if (hInstLibrary)
   {
      _Function = (Function)GetProcAddress(hInstLibrary,"Function");
     if (_Function)
     {
        // use the function

But now I have no idea how I can instantiate a class? How can I use explicit links or implicit links?

Any help with sample code would be appreciated.

+5
source share
2 answers

, . , , , . :

//interface.h

class TestInterface
{
public:
     virtual void Function( int ) = 0;
     virtual int getBar(void) = 0;
};

DLL interface.h, TestInterface :

//test.h
namespace DllTest {
    class Test : public TestInterface
    {
    public:
         Test();
         void Function( int );
         int getBar(void);
    private:
        int bar;
    };
};

DLL, Test:

extern "C" __declspec(dllexport) TestInterface *allocate_test() {
    return new DllTest::Test();
}

, , DLL, "allocate_test" :

TestInterface *(*test_fun)() = (TestInterface *(*test_fun)())GetProcAddress(hInstLibrary,"allocate_test");
TestInterface *test_ptr = test_fun();
test_ptr->Function(12); //use you object
+6

-, , Microsoft. .

, , , , . __declspec(dllexport) DLL, , __declspec(dllimport) , DLL. , DLL, - :

#ifdef __WIN32
#ifdef MYMODULE_DLL
#define MYMODULE_EXPORT __declspec(dllexport)
#else
#define MYMODULE_EXPORT __declspec(dllimport)
#endif
#else
#define MYMODULE_EXPORT
#endif

, DLL MYMODULE_DLL .

, :

class MYMODULE_EXPORT DllTest
{
    //  ...
};

.

+2

All Articles