如何从 Windows 快捷方式(.lnk 文件)以编程方式 (C++) 启动应用程序?

发布于 2024-10-06 17:21:06 字数 2297 浏览 0 评论 0原文

如何以编程方式从 Windows 快捷方式(.lnk 文件)启动应用程序?

我尝试使用 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;
}

how can I launch programmatically an application from a Windows shortcut (.lnk file)?

I tried to use the API ShellExecute and it seems to work. Any caveat?

Thank you.

Here it is a snippet of my current code:

#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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最笨的告白 2024-10-13 17:21:06

我不确定您不确定什么,您观察到的行为是

ShellExecute“打开”操作,当您“打开”文件参数引用的文件时,将执行 shell 执行的任何操作(您可以右键单击快捷方式并显式选择“打开”,但这也是 .lnk 的默认操作,因此与双击相同)。

“打开”快捷方式文件它将“打开”目标,如果目标是可执行文件,它将运行,如果它是文档或数据文件,它将在关联的程序中打开,如果没有,则提示输入程序联系。

I am not sure what you are uncertain about, the behaviour you observe is documented.

The ShellExecute "open" operation, will do whatever the shell does when you "open" the file referenced by the file argument (you can right-click a shortcut and select "Open" explicitly, but that is also the default operation for .lnk, so is the same as a double-click).

"Opening" a short-cut file it will "open" the target, if the target is an executable it will run, if it is a document or data file, it will open in the associated program, or prompt for a program if none associated.

镜花水月 2024-10-13 17:21:06

ShellExecuteCreateProcess 应该能够打开链接文件。如果他们找不到关联的文件和/或程序,您始终可以使用这些 API 并将艰苦的工作委托给“cmd start”或“explorer”。例如 ShellExecute(0, "open", "explorer", linkfile, 0, SW_SHOW);

ShellExecute or CreateProcess should be able to open link file. If they can't find the associated file and/or the program, you can always use those API and delegate the hard work to "cmd start" or "explorer". E.g. ShellExecute(0, "open", "explorer", linkfile, 0, SW_SHOW);

命比纸薄 2024-10-13 17:21:06

ShellExecute 应该可以工作。

但是,……

int main( int, wchar_t* )

据我所知,没有编译器支持这个签名。只需编写:

int main()

此外,对于诊断消息,只需使用 FormatMessage Windows API 函数,或者,如果代码专用于 Visual C++,则使用与该编译器捆绑在一起的适当支持类。

干杯&呵呵,

ShellExecute should work.

But, ...

int main( int, wchar_t* )

... no compiler I know of supports this signature. Just write:

int main()

Also, for the dignostic messages, just use the FormatMessage Windows API function, or, if the code is exclusively for Visual C++, use the appropriate support class that's bundled with that compiler.

Cheers & hth.,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文