在我們的系統中,我們正在監視一個我們幾乎無法控制的第三方可執行文件,我們稱之爲TheServer.exe。我相信它是用C++ Builder構建的。有時TheServer會出現故障,我們需要停止並重新啓動它。我正在編寫一些C#代碼來自動執行此操作,但是當我使用Process.Start()
啓動TheServer.exe時,出現一個來自TheServer的錯誤對話框,指出「External exception E0434F4D」。僅當使用Process.Start時出現應用程序錯誤
從資源管理器或命令行啓動TheServer時,沒有錯誤。我也嘗試在Visual Studio 2010中以調試模式啓動進程,也沒有錯誤。除了Process.Start
,我已經嘗試了P/Invoke調用ShellExecute
和CreateProcess
,結果相同。有沒有其他方法可以從.Net開始這個過程?
我使用現在的代碼:
const string path = @"C:\Program files\TheServer\TheServer.exe";
ProcessStartInfo psi = new ProcessStartInfo()
{
FileName = path,
WorkingDirectory = Path.GetDirectoryName(path),
UseShellExecute = true, // Tried false as well
};
Process.Start(psi);
編輯:當由Hans帕桑特找到this answer,我創建了一個非常小的C++程序作爲中間體。
#include <windows.h>
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
ShellExecuteA(NULL, "open", lpCmdLine, "", "", SW_NORMAL);
return 0;
}
使用Run.exe TheServer.exe
在命令行中運行這個,程序啓動時沒有任何錯誤。從.Net運行相同的命令行會導致與之前相同的錯誤對話框。
編輯:This question頗爲類似地雷,但我不使用Xenocode Postbuild,我相信這是問題的一部分。但正如我建議我嘗試通過我的代碼,該exe然後開始沒有錯誤。確實很奇怪。
聽起來像一個權限問題。 – leppie
我試着以管理員身份運行我的應用程序,結果相同。有什麼我可以嘗試嗎? – Anlo
大概你已經試過運行notepad.exe作爲測試?你確定你的EXE可以找到它的所有依賴項(DLL)嗎?您也可以嘗試在WinDBG下運行您的應用程序,並將其獲取到http://stackoverflow.com/questions/771039/can-visual-studio-be-made-to-debug-child-processes-like-windbg – cirrus