表单显示对话框函数错误,并且不执行下面的语句

Form ShowDialog function error and does not execute the statements below of it

本文关键字:执行 语句 对话框 显示 函数 错误 表单      更新时间:2023-10-16

我有一个DLL,当dll注入进程时,它应该显示一个窗口窗体。下面的代码是在将 dll 注入进程时执行的。但是,ui->ShowDialog(( 代码似乎失败了,它下面的语句不会执行。而且错误也没有在 catch 语句中捕获。执行只是在 ui->ShowDialog 行上停止。我怎样才能做到这一点?

MainUI 是从 System::Windows::Forms::Form 类扩展而来的。

try {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
MainUI^ ui = gcnew MainUI();
ui->ShowDialog();
printOutput("ui->ShowDialog() has been executed");
Application::Run(ui);
}
catch (InvalidOperationException^ e) {
printOutput("Exception catched");
}

解决方案是添加一个 if 语句来检查 Application::MessageLoop。

if (Application::MessageLoop == false) {
printOutput("No message loop exists on this thread");
}
try {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Application::Run(gcnew MainUI());
}
catch (InvalidOperationException^ e) {
printOutput("Exception catched");
}