如何导入 C++ 的 dll命名空间内的类

发布于 2025-01-06 01:12:49 字数 927 浏览 0 评论 0原文

我读了一些文档,其中给出了与 C 兼容的函数的简单示例。

__declspec(dllexport) MyFunction();

我对此很满意。我编写了一个小应用程序,使用了这个dll的功能。我使用了与

LoadLibrary() 

函数的显式链接。 C 风格的函数可以正常工作。但是当我编写我的类时,

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

}
#endif

它编译良好并且创建了 Dll。在使用 C 风格函数时,我只是从 LoadLibrary() 和 GetProcAddress(...) 函数中获取函数指针。

我以前的用法是

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

但现在我不知道如何实例化我的类? 如何使用显式链接或隐式链接?

任何有关代码示例的帮助将不胜感激。

I read some documents which gives simple examples on functions compatible with C.

__declspec(dllexport) MyFunction();

I'm okey with that. I write a small application uses the functions of this dll. I used explicit linking with

LoadLibrary() 

function. C style functions work without problems. But when i write my class as

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

}
#endif

it compiles well and Dll is created. While working with C style functions i was simply taking a function pointer from LoadLibrary() and GetProcAddress(...) functions.

My previous usage is

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 can i instantiate my class?
How can i use explicit linkage or implicit linkage?

Any help with a code sample would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤檠 2025-01-13 01:12:49

如果您尝试实例化一个类,那么您需要在编译时了解其结构。您可以通过创建一个抽象类来实现此目的,该抽象类定义导入类必须重新定义的实例方法。例如:

//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

If you're trying to instantiate a class, then you need to know its structure on compilation time. You can achieve this by creating an abstract class that defines the instance methods that the imported class will have to redefine. For example:

//interface.h

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

Afterwards, in your DLL, you can include interface.h, inherit TestInterface and redefine the pure-virtual methods:

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

You could then define a function in your DLL which allocates a Test object:

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

And finally, when you import the DLL, look for the symbol "allocate_test" and use it:

TestInterface *(*test_fun)() = (TestInterface *(*test_fun)())GetProcAddress(hInstLibrary,"allocate_test");
TestInterface *test_ptr = test_fun();
test_ptr->Function(12); //use you object
深海里的那抹蓝 2025-01-13 01:12:49

首先,请注意这是 Microsoft 的特殊性。不同的规则
对于其他系统也适用。

像你那样写东西是行不通的,或者至少是痛苦的。你
需要在DLL中使用__declspec(dllexport)来定义
函数,但在编译调用的代码时 __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
{
    //  ...
};

这具有导出或导入所有函数和
类的静态成员。

First, note that this is a Microsoft particularity. Different rules
will hold for other systems.

Writing things as you did doesn't work, or at least, is painful. You
need to use __declspec(dllexport) in the DLL which defines the
functions, but __declspec(dllimport) when compiling code which invokes
the functions from another DLL. The usual way of handling this is to
use some specific macro name specifying the DLL, and do something like:

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

Put this in a header which is included in all of the headers in your
DLL, and define MYMODULE_DLL on the command line of the project.

Also, it's possible to export an entire class:

class MYMODULE_EXPORT DllTest
{
    //  ...
};

This has the effect of exporting or importing all of the functions and
static members of the class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文