使用 dllexport 从 DLL 导出函数 [英] Exporting functions from a DLL with dllexport
问题描述
我想要一个从 C++ Windows DLL 导出函数的简单示例.
I'd like a simple example of exporting a function from a C++ Windows DLL.
我想查看标题、.cpp
文件和 .def
文件(如果绝对需要).
I'd like to see the header, the .cpp
file, and the .def
file (if absolutely required).
我希望导出的名称未修饰.我想使用最标准的调用约定(__stdcall
?).我想要使用 __declspec(dllexport)
而不必使用 .def
文件.
I'd like the exported name to be undecorated. I'd like to use the most standard calling convention (__stdcall
?). I'd like the use __declspec(dllexport)
and not have to use a .def
file.
例如:
//header
extern "C"
{
__declspec(dllexport) int __stdcall foo(long bar);
}
//cpp
int __stdcall foo(long bar)
{
return 0;
}
我试图避免链接器在名称中添加下划线和/或数字(字节数?).
I'm trying to avoid the linker added underscores and/or numbers (byte counts?) to the name.
我不支持 dllimport
和 dllexport
使用相同的标头.我不想要任何关于导出 C++ 类方法的信息,只想要 C 风格的全局函数.
I'm OK with not supporting dllimport
and dllexport
using the same header. I don't want any information about exporting C++ class methods, just c-style global functions.
更新
不包括调用约定(并使用 extern "C"
)为我提供了我喜欢的导出名称,但这意味着什么?我得到的 pinvoke (.NET)、declare (vb6) 和 GetProcAddress
所期望的默认调用约定是什么?(我猜对于 GetProcAddress
这将取决于调用者创建的函数指针).
Not including the calling convention (and using extern "C"
) gives me the export names as I like, but what does that mean? Is whatever default calling convention I'm getting what pinvoke (.NET), declare (vb6), and GetProcAddress
would expect? (I guess for GetProcAddress
it would depend on the function pointer the caller created).
我希望在没有头文件的情况下使用这个 DLL,所以我真的不需要很多花哨的 #defines
来使调用者可以使用头文件.
I want this DLL to be used without a header file, so I don't really need the a lot of the fancy #defines
to make the header usable by a caller.
我的回答是,我必须使用 *.def
文件.
I'm OK with an answer being that I have to use a *.def
file.
推荐答案
如果您想要纯 C 导出,请使用 C 项目而不是 C++.C++ DLL 依赖于所有 C++ 主义(命名空间等)的名称修改.您可以通过进入 C/C++-> 高级下的项目设置将代码编译为 C,有一个选项编译为"对应于编译器开关/TP 和/TC.
If you want plain C exports, use a C project not C++. C++ DLLs rely on name-mangling for all the C++isms (namespaces etc...). You can compile your code as C by going into your project settings under C/C++->Advanced, there is an option "Compile As" which corresponds to the compiler switches /TP and /TC.
如果您仍然想使用 C++ 来编写您的 lib 的内部结构,但导出一些未修改的函数以供在 C++ 之外使用,请参阅下面的第二部分.
If you still want to use C++ to write the internals of your lib but export some functions unmangled for use outside C++, see the second section below.
您真正想做的是在标头中定义一个条件宏,该标头将包含在您的 DLL 项目中的所有源文件中:
What you really want to do is define a conditional macro in a header that will be included in all of the source files in your DLL project:
#ifdef LIBRARY_EXPORTS
# define LIBRARY_API __declspec(dllexport)
#else
# define LIBRARY_API __declspec(dllimport)
#endif
然后在要导出的函数上使用LIBRARY_API
:
Then on a function that you want to be exported you use LIBRARY_API
:
LIBRARY_API int GetCoolInteger();
在您的库构建项目中创建一个定义 LIBRARY_EXPORTS
这将导致您的函数被导出以用于您的 DLL 构建.
In your library build project create a define LIBRARY_EXPORTS
this will cause your functions to be exported for your DLL build.
由于 LIBRARY_EXPORTS
不会在使用 DLL 的项目中定义,当该项目包含库的头文件时,所有函数都将被导入.
Since LIBRARY_EXPORTS
will not be defined in a project consuming the DLL, when that project includes the header file of your library all of the functions will be imported instead.
如果您的库是跨平台的,您可以在不在 Windows 上时将 LIBRARY_API 定义为空:
If your library is to be cross-platform you can define LIBRARY_API as nothing when not on Windows:
#ifdef _WIN32
# ifdef LIBRARY_EXPORTS
# define LIBRARY_API __declspec(dllexport)
# else
# define LIBRARY_API __declspec(dllimport)
# endif
#elif
# define LIBRARY_API
#endif
使用 dllexport/dllimport 时不需要使用 DEF 文件,如果使用 DEF 文件则不需要使用 dllexport/dllimport.这两种方法以不同的方式完成相同的任务,我认为 dllexport/dllimport 是两者中推荐的方法.
When using dllexport/dllimport you do not need to use DEF files, if you use DEF files you do not need to use dllexport/dllimport. The two methods accomplish the same task different ways, I believe that dllexport/dllimport is the recommended method out of the two.
如果您需要它来使用 LoadLibrary 和 GetProcAddress,或者可能从其他语言(即来自 .NET 的 PInvoke,或 Python/R 中的 FFI 等)导入,您可以使用 extern "C"
内联使用 dllexport 告诉 C++ 编译器不要修改名称.由于我们使用的是 GetProcAddress 而不是 dllimport,因此我们不需要从上面执行 ifdef 舞蹈,只需一个简单的 dllexport:
If you need this to use LoadLibrary and GetProcAddress, or maybe importing from another language (i.e PInvoke from .NET, or FFI in Python/R etc) you can use extern "C"
inline with your dllexport to tell the C++ compiler not to mangle the names. And since we are using GetProcAddress instead of dllimport we don't need to do the ifdef dance from above, just a simple dllexport:
代码:
#define EXTERN_DLL_EXPORT extern "C" __declspec(dllexport)
EXTERN_DLL_EXPORT int getEngineVersion() {
return 1;
}
EXTERN_DLL_EXPORT void registerPlugin(Kernel &K) {
K.getGraphicsServer().addGraphicsDriver(
auto_ptr<GraphicsServer::GraphicsDriver>(new OpenGLGraphicsDriver())
);
}
这是 Dumpbin/exports 的导出结果:
And here's what the exports look like with Dumpbin /exports:
Dump of file opengl_plugin.dll
File Type: DLL
Section contains the following exports for opengl_plugin.dll
00000000 characteristics
49866068 time date stamp Sun Feb 01 19:54:32 2009
0.00 version
1 ordinal base
2 number of functions
2 number of names
ordinal hint RVA name
1 0 0001110E getEngineVersion = @ILT+265(_getEngineVersion)
2 1 00011028 registerPlugin = @ILT+35(_registerPlugin)
所以这段代码工作正常:
So this code works fine:
m_hDLL = ::LoadLibrary(T"opengl_plugin.dll");
m_pfnGetEngineVersion = reinterpret_cast<fnGetEngineVersion *>(
::GetProcAddress(m_hDLL, "getEngineVersion")
);
m_pfnRegisterPlugin = reinterpret_cast<fnRegisterPlugin *>(
::GetProcAddress(m_hDLL, "registerPlugin")
);
这篇关于使用 dllexport 从 DLL 导出函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!