2010-12-10 56 views
0

如何以編程方式從Windows快捷方式(.lnk文件)啓動應用程序?如何從Windows快捷方式(.lnk文件)以編程方式啓動(C++)應用程序?

我試圖使用API​​ ShellExecute,它似乎工作。任何警告?

謝謝。

這是我當前的代碼片段:

#include <windows.h> 

#include <map> 
#include <string> 
#include <iostream> 

int main(int, char**) 
{ 
    std::map< int, std::wstring > errors; 
    errors[0]      = L"The operating system is out of memory or resources."; 
    errors[ERROR_FILE_NOT_FOUND] = L"The specified file was not found."; 
    errors[ERROR_PATH_NOT_FOUND] = L"The specified path was not found."; 
    errors[ERROR_BAD_FORMAT]  = L"The .exe file is invalid (non-Microsoft Win32 .exe or error in .exe image)."; 
    errors[SE_ERR_ACCESSDENIED] = L"The operating system denied access to the specified file."; 
    errors[SE_ERR_ASSOCINCOMPLETE] = L"The file name association is incomplete or invalid."; 
    errors[SE_ERR_DDEBUSY]   = L"The Dynamic Data Exchange (DDE) transaction could not be completed because other DDE transactions were being processed."; 
    errors[SE_ERR_DDEFAIL]   = L"The DDE transaction failed."; 
    errors[SE_ERR_DDETIMEOUT]  = L"The DDE transaction could not be completed because the request timed out."; 
    errors[SE_ERR_DLLNOTFOUND]  = L"The specified DLL was not found."; 
    errors[SE_ERR_FNF]    = L"The specified file was not found."; 
    errors[SE_ERR_NOASSOC]   = L"There is no application associated with the given file name extension. This error will also be returned if you attempt to print a file that is not printable."; 
    errors[SE_ERR_OOM]    = L"There was not enough memory to complete the operation."; 
    errors[SE_ERR_PNF]    = L"The specified path was not found."; 
    errors[SE_ERR_SHARE]   = L"A sharing violation occurred."; 

    int ret = reinterpret_cast<int>(::ShellExecute(0,L"open",L"\"C:\\Documents and Settings\\All Users\\Start Menu\\Programs\\Accessories\\Calculator.lnk\"",0,0,SW_SHOW)); 
    const int minimumRetOK = 33; 
    if (ret < minimumRetOK) { 
     if (errors.count(ret)) { 
     std::wcout << L"Error " << ret << L" " << errors[ ret ]; 
     } else { 
     std::wcout << L"Error " << ret << L" undocumented error"; 
     } 
    } 

    return 0; 
} 

回答

1

我不確定你不確定什麼,你觀察到的行爲是documented

ShellExecute「open」操作將在您打開文件參數引用的文件時執行shell執行的任何操作(可以右鍵單擊快捷方式並明確選擇「打開」,但這也是默認值操作爲.lnk,所以與雙擊相同)。

「打開」一個快捷方式文件會「打開」目標,如果目標是可執行文件,它將運行,如果它是文檔或數據文件,它將在相關程序中打開,或者提示一個程序如果沒有關聯。

+0

謝謝您的解釋。 – 2010-12-10 19:20:10

0

ShellExecuteCreateProcess應該可以打開鏈接文件。如果他們找不到關聯的文件和/或程序,則可以始終使用這些API並將艱苦工作委託給「cmd start」或「explorer」。例如。 ShellExecute(0, "open", "explorer", linkfile, 0, SW_SHOW);

1

ShellExecute應該工作。

但是,...

int main(int, wchar_t*) 

...沒有編譯器,我知道支持的這個簽名。只要寫:

int main() 

此外,對於dignostic的消息,只需要使用FormatMessage Windows API函數,或者,如果代碼是專爲VISUAL C++,使用與該編譯器捆綁相應的支持類。

Cheers & hth。,

+0

是的,我在主簽名中犯了一個錯誤。微軟編譯器允許我這樣做:-) 關於FormatMessage,我意識到它,但我認爲在這種情況下,它不會工作,因爲ShellExecute有其特定的錯誤代碼。 – 2010-12-10 19:10:40

+0

@uvts_cvs:好吧,我找不到shell32的任何消息資源DLL,所以'FormatMessage'只能用於三個'ERROR_' * xxx *代碼。 :-( – 2010-12-10 21:14:31

相關問題