DLL全局变量问题
声明为局部变量时程序正常:程序代码:
extern "C" __declspec(dllexport) bool showDialog(DWORD address, LPDWORD bytes, HWND parent ) { DWORD wrote = 0; WriteProcessMemory(GetCurrentProcess(), UlongToPtr(address), bytes, 6, &wrote); UserOnInject(); QWinWidget win(nullptr); //声明为局部变量时程序正常 win.showCentered(); SroDlg dlg(&win); MyGlobalShortCut *shortcut = new MyGlobalShortCut("F5"); QObject::connect(shortcut, SIGNAL(activated()), &dlg, SLOT(activated())); dlg.exec(); return TRUE; }
声明为全局变量时程序崩溃:
程序代码:
QWinWidget win(nullptr); //声明为全局变量时程序崩溃 extern "C" __declspec(dllexport) bool showDialog(DWORD address, LPDWORD bytes, HWND parent ) { DWORD wrote = 0; WriteProcessMemory(GetCurrentProcess(), UlongToPtr(address), bytes, 6, &wrote); UserOnInject(); win.showCentered(); SroDlg dlg(&win); MyGlobalShortCut *shortcut = new MyGlobalShortCut("F5"); QObject::connect(shortcut, SIGNAL(activated()), &dlg, SLOT(activated())); dlg.exec(); return TRUE; }
请问 QWinWidget win(nullptr) 声明为全局时程序为什么会崩溃?
可编译通过,运行时错误。
这个问题同样适用于 SroDlg dlg(&win),但原始类型不会崩溃。
当showDialog()函数结束后,我仍然需要win和dlg对象,但声明为局部就销毁了
请问我该如何做? 蒙蒙的,请无视Qt的代码,我感觉这属于C++的问题,谢谢您!