2010-12-10 59 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

相关问题