2014-02-28 51 views
-1

我試圖從另一個.exe(Windows窗體)運行.exe(Win32)在visual c + +(VS 2012),使用Process::Start系統找不到指定的文件(Visual c + +)

因此,我存儲了Windows窗體所在的Win32。我們的想法是:

  • 得到模塊的完全限定路徑:GetModuleFileName

  • 中刪除文件名和反斜槓從路徑:PathRemoveFileSpec

  • 添加的Win32應用程序的名稱:sprintf

  • 傳遞字符串^至Process::Start

構建沒有錯誤,但在運行失敗時顯示如下圖所示的錯誤。我花了很多時間試圖解決它,但沒有結果。我怎樣才能解決這個問題?

enter image description here

#include "stdafx.h" 
#include <stdio.h> 
#include <Windows.h> 
#include <stdlib.h> 

#include <string> 
#include <cerrno> 
#include <Shlwapi.h> 

#include <msclr\marshal_cppstd.h> 

using namespace std; 
using namespace System; 
using namespace msclr::interop; 


//code... 


private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) 
{ 
    TCHAR path[1000]; 
    GetModuleFileName(NULL, path, 1000) ; // path: A pointer to a buffer that receives 
              //  the fully qualified path of the module 

     PathRemoveFileSpec(path); // path: holds location only (TCHAR) 

     CHAR mypath[1000]; 
     wcstombs(mypath, path, wcslen(path) + 1); // convert tchar to char (mypath) 

     // Formatting the string: constructing a string by substituting computed values at various 
     // places in a constant string 
     CHAR mypath2[1000]; 
     sprintf(mypath2, "%s\\JoypadCodesApplication.exe", mypath); 

     String^ result; 
     result = marshal_as<String^>(mypath2); 

     Process::Start(result); 
} 
+0

您是否正在運行64位機器?編輯:在你回答之前,你是否證實路徑實際上是正確的? – Brandon

回答

1

既然你調用.NET API反正啓動的過程中,你可以嘗試使用.NET API來構建你要調用的可執行文件的路徑。

using namespace System::Diagnostics; 
using namespace System::IO; 
using namespace System::Reflection; 

String^ assemblyLocation = Assembly::GetExecutingAssembly()->Location; 
String^ dir = Path::GetDirectoryName(assemblyLocation); 

String^ childProcessPath = Path::Combine(dir, "JoypadCodesApplication.exe"); 
Process::Start(childProcessPath); 

如果仍然不能正常工作,你有沒有驗證傳遞給Process::Start()的路徑是正確的exe文件是在該位置你認爲它是什麼?

+0

您可能想要記錄* using *指令,OP正在與基礎知識鬥爭。 –

+0

@Hans這是一個好點,編輯我的答案包括他們。 – Andy

+0

@Andy.exe不在我想象的位置。有時候,我們正在尋找複雜的,即使解決方案非常簡單。謝謝。我還會看看.NET API。 – dempap

相關問題