如何导入 C++ 的 dll命名空间内的类
我读了一些文档,其中给出了与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您尝试实例化一个类,那么您需要在编译时了解其结构。您可以通过创建一个抽象类来实现此目的,该抽象类定义导入类必须重新定义的实例方法。例如:
之后,在您的 DLL 中,您可以包含 interface.h,继承 TestInterface 并重新定义纯虚拟方法:
然后您可以在 DLL 中定义一个分配 Test 对象的函数:
最后,当您导入 DLL 时,查找符号“allocate_test”并使用它:
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:
Afterwards, in your DLL, you can include interface.h, inherit TestInterface and redefine the pure-virtual methods:
You could then define a function in your DLL which allocates a Test object:
And finally, when you import the DLL, look for the symbol "allocate_test" and use it:
首先,请注意这是 Microsoft 的特殊性。不同的规则
对于其他系统也适用。
像你那样写东西是行不通的,或者至少是痛苦的。你
需要在DLL中使用
__declspec(dllexport)
来定义函数,但在编译调用的代码时
__declspec(dllimport)
来自另一个 DLL 的函数。处理这个问题的通常方法是
使用一些指定 DLL 的特定宏名称,并执行以下操作:
将其放入标头中,该标头包含在您的所有标头中
DLL,并在项目的命令行中定义
MYMODULE_DLL
。此外,还可以导出整个类:
这具有导出或导入所有函数和
类的静态成员。
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 thefunctions, but
__declspec(dllimport)
when compiling code which invokesthe functions from another DLL. The usual way of handling this is to
use some specific macro name specifying the DLL, and do something like:
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:
This has the effect of exporting or importing all of the functions and
static members of the class.