【发布时间】:2014-06-02 14:19:56
【问题描述】:
我正在开发一个项目,我正在创建一个小型 DLL,然后还创建一个 Windows 应用程序来使用它。
我不知道发生了什么。
我在 DLL 中有一个名为“startPicadorVisual”的函数,它接受一个参数,即 std::string。
在依赖于 DLL 的应用程序中,我在一个主要是自动生成的 .h 文件中有以下代码:
typedef void (__stdcall *f_startPicadorVisual)(string s);
namespace PicadorPrototype {
f_startPicadorVisual startPicadorVisual;
Form1(void) {
//Load DLL Funcs
HINSTANCE hGetProcIDDLL = LoadLibrary(L"..\\Debug\\Picador.dll");
if (!hGetProcIDDLL) {
std::cout << "could not load the dynamic library" << std::endl;
throw "Bad Stuff";
}
startPicadorVisual = (f_startPicadorVisual)GetProcAddress(hGetProcIDDLL, "startPicadorVisual");
if (!startPicadorVisual) {
std::cout << "could not locate the function" << std::endl;
throw "More Bad Stuff";
}
当我调用 GetProcAddress 时第二步失败时。
函数在我的DLL中定义如下:
void __declspec(dllexport) startPicadorVisual(string fixtureNamet);
PicadorResults __declspec(dllexport) getPicadorReading(string fixtureName);
谁能告诉我为什么这不起作用?
【问题讨论】:
-
我相信这是因为名称错误。 stackoverflow.com/questions/1467144/…
-
使用 DLL 的最简单方法是允许 C 运行时执行加载库和与动态库的链接。当您编译和链接一个 DLL 时,至少应该创建两个东西:(1) .dll 本身和 (2) 一个包含 .dll 存根的 .lib 文件。您可以链接 .lib 文件并将 .dll 文件与可执行文件一起包含在内。链接器将与 .lib 文件中的存根链接,然后处理加载库并将各种存根与 .dll 中的实际函数连接。
-
谢谢@RichardChambers 我仍然对为什么这段代码不起作用很感兴趣,但你的评论让我再次考虑使用那个系统,而且现在时间不在我身边,我认为最好去做就对了。现在一切正常。
-
@IvanGrynko 我认为您可能是对的,不幸的是我没有时间回去检查。我认为这是一件好事,这意味着我的项目正在取得进展。