参数传递有关DLL,主要进程崩溃? [英] parameter passing about DLL ,Main progrem crash?

查看:114
本文介绍了参数传递有关DLL,主要进程崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


在DLL文件中


In DLL File

extern "C" __declspec(dllexport) void Show(CString caption,CString mode)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	CTest test(NULL,caption,mode);
	test.DoModal ();
}



在测试文件中




In TEST FILE


void CTestDlg::OnOK() 
{
	// TODO: Add extra validation here
	typedef void (WINAPI * TESTDLL)(CString caption,CString model);
	HINSTANCE hmod;
	hmod = ::LoadLibrary ("mfcdll.dll");
	if(hmod==NULL)
	{
		AfxMessageBox("Fail");
	}
	TESTDLL lpproc;
	lpproc = (TESTDLL)GetProcAddress (hmod,"Show");
	if(lpproc!=(TESTDLL)NULL)
		(*lpproc)(_T("Alex-DLL"),_T(""));
	
	FreeLibrary(hmod);
	
//	CDialog::OnOK();
}



错误:

运行时检查失败#0-在整个函数调用中未正确保存ESP的值.
这通常是由于用一个调用约定声明的函数调用了带有不同调用约定声明的函数指针的函数所致.

我想知道为什么在dll中关闭对话框时会出现错误.
我不知道如何上传图片?
谁可以向我解释?
tks ~~~!

ps:如果没有参数,就没有错误.



bug:

Run-Time Check Failur#0-The value of ESP was not properly saved across a function call.
this is usually a result of calling a function decalared with one calling convention with a funciton pointer declared with a different calling convention.

I''d like to know why have a bug ,when close the dialog in dll.
I don''t know how to upload the picture?
who can explain to me ?
tks~~~!

ps: if no parameter ,no the bug.

推荐答案

为什么要使用DLL?重新考虑是否需要使用DLL并使用静态库或根本不需要库(如果不需要). DLL可能会带来很多问题,如果您没有使用DLL的经验,那么您将很头疼.

无论如何,问题似乎很明显,并且出现在错误消息中.由于您定义了函数指针,如下所示:
Why do you use a DLL? Rethink if you need to use a DLL and use a static lib or no lib at all if you don''t have to. DLLs can be quite problematic and you will have a lot of headaches if you don''t have experience with them.

Anyway the problem seems to be obvious and its in the error message. Since you defined the function pointer like this:
typedef void (WINAPI * TESTDLL)(CString caption,CString model);


您的exe使用__stdcall调用约定(因为您指定了WINAPI)来调用该函数,但是您尚未在DLL中为该函数指定调用约定,因此其代码是使用C ++编译器设置(__cdecl中设置的默认调用约定)生成的您尚未更改):


Your exe calls the function with __stdcall calling convention (becuase you specified WINAPI) but you haven''t specified a calling convention in the DLL for your function so its code is generated with the default calling convention set in your C++ compiler settings (__cdecl if you haven''t changed it):

extern "C" __declspec(dllexport) void Show(CString caption,CString mode)



更改此内容:



Change this:

extern "C" __declspec(dllexport) void Show(CString caption,CString mode)


对此:


to this:

extern "C" __declspec(dllexport) void WINAPI Show(CString caption,CString mode)



我还将使用const char *代替CString,因为通过不同的编译器设置以及exe和dll中单独的CRT库/堆,通过DLL接口边界传递对象可能很危险.在DLL接口中使用primitve类型和纯虚方法是一个好习惯.



I would also use const char* instead of CString because passing objects through DLL interface boundaries can be dangerous because of different compiler settings and the separate CRT libraries/heaps in your exe and dll. Using primitve types and pure virtual methods in the DLL interface is a good practice.


这篇关于参数传递有关DLL,主要进程崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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