使用dll时ESP无法正确保存 [英] ESP not properly saved when using dlls

查看:100
本文介绍了使用dll时ESP无法正确保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!我只想问一个问题,因为google无法提供答案.....

您每次看到一个.dll,然后让一个.exe从中调用函数时,您都会看到此错误.

ESP的值未在函数调用中正确保存.

现在我用谷歌搜索了很多结果.主要是告诉我更改函数调用内容....

只有一个函数不被声明为普通函数,而是被声明为普通函数.

Hello everyone! I just wanted to ask a question since google hasnt been able to provide an answer.....

You see every time I make a .dll and then have a .exe call a function from it, I get this error.

The value of the ESP was not properly saved across a function call.

Now I googled this and got alot of results. Mainly telling me to change my function call things....

There is only one function which is not declared as just a plain function and it is declared as such.

<br />
extern "C" __declspec(dllexport) int Compile(int argc, char* argv[])<br />
{<br />
     // function definition<br />
}



现在我读到我应该将函数更改为这样.

int _cdecl Compile(int argc, char* argv[])

或使用_stdcall.我已经尝试了带或不带extern"C"的情况,甚至尝试仅使用普通

int Compile(int argc, char* argv[])

但是我在运行时仍然遇到相同的错误.即使程序执行完毕.该错误实际上在返回.exe的主要功能之后出现.我使用int main,并且总是从cstdlib库返回EXIT_SUCCESS或EXIT_FAILURE.如果有人可以帮助我解决此问题,那对我来说将是极大的帮助.
我正在使用Windows 7 Service Pack 1,并且我的编译器是Microsoft Visual C ++ Express 2010,并且每次都尝试使用Visual C ++ 6.0并具有相同的结果.



Now I read that i should change the function to be like this.

int _cdecl Compile(int argc, char* argv[])

or by using _stdcall. I have tried both, with and without extern "C", and even tried using just plain

int Compile(int argc, char* argv[])

but i still get the same error at runtime. Even though the program is done executing. The error actually appears AFTER the main function of the .exe has returned. I use int main and always return EXIT_SUCCESS or EXIT_FAILURE from the cstdlib library. If anyone can help me fix this it would be GREAT help for me.
I am using windows 7 service pack 1 and my compiler is Microsoft Visual C++ Express 2010 and I have also tried Visual C++ 6.0 with the same results each time.

推荐答案

如果您使用Visual Studio向导生成DLL骨架(指定导出符号"),然后在生成的代码中可能会发现以下注释:

If you use the Visual Studio wizard to generate DLL skeleton (specifying ''export symbols'') then you may find, in the generated code, the following remark:

//以下ifdef块是创建用于导出宏的标准方法
//从更简单的DLL中获取.此DLL中的所有文件都是使用MYDLL_EXPORTS
编译的 //在命令行上定义的符号.此符号不应在任何项目中定义
//使用此DLL.这样,其源文件包含此文件的任何其他项目,请参见
//MYDLL_API的功能是从DLL导入,而此DLL会看到符号
//与此宏一起定义为要导出.
// The following ifdef block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the MYDLL_EXPORTS
// symbol defined on the command line. This symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// MYDLL_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.
#ifdef MYDLL_EXPORTS
#define MYDLL_API __declspec(dllexport)
#else
#define MYDLL_API __declspec(dllimport)
#endif 




我猜想它解释了您的问题:虽然DLL构建必须使用 __declspec(dllexport),但是应用程序之一应该使用__declspec(dllimport)(或者不要在应用程序构建中使用 ,完全__declspec,请参见此处 [ ^ ]).



I suppose it explains your problem: while the DLL build must use __declspec(dllexport), the application one should use __declspec(dllimport) (or don''t use, in the application build, __declspec at all, see here[^]).


您尝试使用此功能吗?
extern "C" __declspec(dllexport) int _cdecl Compile(int argc, char* argv[])
Did you try to use this?
extern "C" __declspec(dllexport) int _cdecl Compile(int argc, char* argv[])


这篇关于使用dll时ESP无法正确保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆