据我所知,如果填充兼容,跨库传递结构是安全的。因此,我使用包含单个成员的结构编写了一个测试,但在读取返回的结构时仍然出现运行时错误。
插件接口.h:
#ifndef PLUGIN_INTERFACE_H
#define PLUGIN_INTERFACE_H
typedef struct {
const char *name;
} DataStruct;
class PluginInterface {
public:
virtual DataStruct __cdecl getData() = 0;
};
#endif // PLUGIN_INTERFACE_H
插件.h:
#ifndef PLUGIN_H
#define PLUGIN_H
#include "PluginInterface.h"
class Plugin : public PluginInterface {
public:
DataStruct __cdecl getData();
};
#endif // PLUGIN_H
插件.cpp:
#include "Plugin.h"
DataStruct Plugin::getData() {
DataStruct data;
data.name = "name of plugin";
return data;
}
extern "C" __declspec(dllexport) PluginInterface* getInstance() {
return new Plugin;
}
主.cpp:
#include <iostream>
#include "windows.h"
#include "PluginInterface.h"
typedef PluginInterface* (*PluginCreator) ();
int main()
{
HINSTANCE handle = LoadLibrary("Plugin.dll");
if (handle == nullptr) {
std::cout << "Unable to open file!" << std::endl; return 0;
}
PluginCreator creator = (PluginCreator)GetProcAddress(handle, "getInstance");
if (creator == nullptr) {
std::cout << "Unable to load file!" << std::endl; return 0;
}
PluginInterface* plugin = creator();
if (plugin == nullptr) {
std::cout << "Unable to create plugin!" << std::endl; return 0;
}
DataStruct data = plugin->getData();
std::cout << "so far so good" << std::endl;
std::cout << data.name << std::endl; // Access violation
return 0;
}
我用 mingw 编译了插件,用 VS2012 编译了可执行文件。我还尝试用 int 替换 const char*,在这种情况下我得到一个随机整数。我知道传递带有单个元素的结构没有多大意义,但我仍然想知道问题出在哪里。