2012-04-25 82 views
1

我有一個非常簡單的窗體表單程序,我想在按下按鈕時啓動notepad.exe。我收到了一些預期的錯誤。請幫忙。如何在VC++中正確啓動進程?

在我的代碼開始時,我有

#pragma once 
#include <windows.h> 
#include <Shellapi.h> 

在事件處理程序,我有

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { 


       //memset(&ExecuteInfo, 0, sizeof(ExecuteInfo)); 

       ExecuteInfo.cbSize  = sizeof(ExecuteInfo); 
       ExecuteInfo.fMask  = NULL;    
       ExecuteInfo.hwnd   = NULL;    
       ExecuteInfo.lpVerb  = "open";      // Operation to perform 
       ExecuteInfo.lpFile  = "C:\\Windows\\notepad.exe"; // Application name 
       ExecuteInfo.lpParameters = NULL;   // Additional parameters 
       ExecuteInfo.lpDirectory = NULL;       // Default directory 
       ExecuteInfo.nShow  = SW_SHOW; 
       ExecuteInfo.hInstApp  = NULL; 

       ShellExecuteEx(&ExecuteInfo); 

     } 

注:我收到以下錯誤消息,如果我設置爲「使用Unicode字符集」下的屬性頁>配置屬性>常規(ALT-F7)

1>c:\users\marco\desktop\new folder (2)\test000\test000\Form1.h(140): error C2440: '=' : cannot convert from 'const char [5]' to 'LPCWSTR' 
1>   Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast 
1>c:\users\marco\desktop\new folder (2)\test000\test000\Form1.h(141): error C2440: '=' : cannot convert from 'const char [23]' to 'LPCWSTR' 
1>   Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast 

注:我得到的FO llowing錯誤消息,如果我設置爲「使用多字節字符集」下的屬性頁>配置屬性>常規(ALT-F7)

1>test000.obj : error LNK2028: unresolved token (0A000012) "extern "C" int __stdcall ShellExecuteExA(struct _SHELLEXECUTEINFOA *)" ([email protected]@[email protected]@@Z) referenced in function "private: void __clrcall test000::Form1::button1_Click(class System::Object ^,class System::EventArgs ^)" ([email protected]@[email protected]@[email protected]@@[email protected]@@Z) 
1>test000.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall ShellExecuteExA(struct _SHELLEXECUTEINFOA *)" ([email protected]@[email protected]@@Z) referenced in function "private: void __clrcall test000::Form1::button1_Click(class System::Object ^,class System::EventArgs ^)" ([email protected]@[email protected]@[email protected]@@[email protected]@@Z) 
1>C:\Users\Marco\Desktop\New folder (2)\test000\Debug\test000.exe : fatal error LNK1120: 2 unresolved externals 

回答

1

另一種解決方案:如果您使用的是C++/CLI中,你可以和使用管理方法來啓動一個進程:

System::Diagnostics::Process::Start("C:\\Windows\\notepad.exe"); 

這也應該避免字符集問題。
這並不意味着你應該忽略它們,因爲它很好地瞭解潛在的問題。 Roee Shenberg在他的回答中強調了這一點。

+0

This works。Thanks。 – user765168 2012-04-25 12:11:49

+1

我非常喜歡這個,我可以打開Firefox並轉到只有一行代碼的網站。 – user765168 2012-04-25 18:17:05

1

您需要使用TEXT()宏在你的字符串(例如TEXT(「open」)而不是「open」)或在運行時將ANSI字符串轉換爲UTF-16(例如,使用mbstowcs_s()函數)。

發生這種情況的原因是TCHAR是一個Microsoft字符類型,可以是char或wchar_t,具體取決於項目是否配置爲unicode。請注意,lpFile和朋友的類型是LPCTCHAR(長指針指向const TCHAR),這意味着如果您使用(默認)unicode配置,它最終會成爲常量wchar_t *,並且char []不能成爲隱含地投給它。

+0

感謝您的回答。我遵循你所說的將「打開」改爲TEXT(「打開」),還將記事本字符串更改爲TEXT(「c:\\ windows \\ notepad.exe」)。我再次訪問了屬性頁面,並將其更改回「使用Unicode字符集」**。之後,我試圖重建我的程序,但VS顯示我1> test000.obj:錯誤LNK2028:無法解析的標記(0A000012)「extern」C「int __stdcall ShellExecuteExA(struct _SHELLEXECUTEINFOA *)」(?ShellExecuteExA ..... ........ **(與「使用多字節字符集」設置相同的錯誤)**任何建議? – user765168 2012-04-25 02:53:47