创建/调用 DLL 时出现问题
我正在尝试创建一个稍后将在 Inno Setup 中使用的 DLL。 我设法使用 Pelles 作为 IDE 创建一个 DLL,代码如下:
#include <windows.h>
__declspec(dllexport) int sumT(){
return 2;
}
然后我使用以下 Delphi 代码调用将 DLL 映射到 Inno Setup 中的函数:
function Hellow() : Integer ;
external 'sumT@files:yyy.dll stdcall';
它按预期工作,返回 2。我也可以从rundll32.exe - 如果我在 DLL 中添加一个 MsgBox,它就会出现,证明一切都按预期工作。
现在,当我尝试将值传递给 DLL 时,问题就开始了,例如:
#include <windows.h>
__declspec(dllexport) int sumT(int sumTah){
return sumTah;
}
它停止工作了! 在 Inno Setup 中,我给出了以下消息:
“运行时错误(-1:0)”
如果我尝试rundll32.exe,我得到:
yyy.dll 错误
缺少条目:sumT
我不打算在此处粘贴 Delphi 代码,因为我几乎尝试了所有内容,与 C 代码相同,我尝试使用 __stdcall 代替,将参数声明为 INT、UINT...以及其他我忘记的事情。
我还查阅了 MSDN,但找不到与此特定问题相关的任何内容。
那么,有人可以帮忙吗?
谢谢
编辑:我正在用 C 进行编译,所以不需要“extern”。
I'm trying to create a DLL that will later be used in Inno Setup.
I managed to create a DLL using Pelles as an IDE, with the following code:
#include <windows.h>
__declspec(dllexport) int sumT(){
return 2;
}
Then I call map the DLL to a function in Inno Setup, using the following Delphi code:
function Hellow() : Integer ;
external 'sumT@files:yyy.dll stdcall';
It works as expected, returning 2. I can also call it from rundll32.exe - if I add a MsgBox in the DLL, it will appear, proving that everything works as expected.
Now the problem starts when I try to pass a value to the DLL, like that - for example:
#include <windows.h>
__declspec(dllexport) int sumT(int sumTah){
return sumTah;
}
It stops working!
In Inno Setup, I gives me the message:
"Runtime Error (at -1:0)"
And if I try rundll32.exe, I get:
Error in yyy.dll
Missing entry:sumT
I'm not going to paste the Delphi code here, because I virtually tried everything, same thing with the C code, I tried using __stdcall instead, declaring the argument as INT, UINT... and other things I forgot.
I also looked up the MSDN, but could not find anything pertaining to this particular problem.
So, can anyone help?
Thanks
EDIT: I am compiling in C, so no need for "extern".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
导出的名称可能与您想象的不符。在 DLL 上使用 dumpbin.exe /exports 查看实际导出的名称。使用 extern "C" 来抑制名称修改。
另外,你必须声明这个函数 __stdcall,这就是你的 Delphi 声明所说的。默认为 __cdecl。当您开始传递参数时,这将失败。
The exported name probably doesn't match what you think it looks like. Use dumpbin.exe /exports on the DLL to see the actual exported name. Use extern "C" to suppress name mangling.
Also, you have to declare this function __stdcall, that's what your Delphi declaration said. The default is __cdecl. That will fail when you start passing arguments.
如果使用 C++ 编译器进行编译,函数名称会被破坏,从而包含有关参数的数据。解决方案是执行以下操作:
extern "C" __declspec(dllexport) int myfn();
,然后将其引用为 _myfn。
extern "C"
告诉编译器不要破坏函数名称。这不适用于模板化函数、类或重载函数。If you compile with a C++ compiler, the function name gets mangled so that it contains data about the arguments. The solution is to do something like:
extern "C" __declspec(dllexport) int myfn();
and then reference it as _myfn. The
extern "C"
tells the compiler to not mangle the function name. This does not work with templated functions, classes or overloaded functions.__declspec(dllexport) int sumT(int sumThah){
返回sumTah;
看起来像是一个拼写错误或你的错误......
__declspec(dllexport) int sumT(int sumThah){
return sumTah;
looks like a typo or your error....