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

标签 c++ windows shortcut lnk

如何从 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;
}

最佳答案

我不确定你不确定什么,你观察到的行为是documented .

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

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

关于c++ - 如何从 Windows 快捷方式(.lnk 文件)以编程方式 (C++) 启动应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4410829/

相关文章:

c++ - 扩展 typedef struct(兼容 VC++11)

windows - 我可以在 Node.js 中设置 Windows 控制台宽度吗?

c++ - std::condition_variable::wait_for 和 std::condition_variable::wait_until 有什么区别?

c++ - 如何追踪导致段错误的原因?

c++ - Lua、元表和全局变量

windows - 如何检查输入是否已在列表中-批处理

windows - 使用 VS2017 增强系统 DPI 缩放

windows - 如何在 Windows 7 上创建 Rscript 的快捷方式

.net - 网络浏览器控制窃取快捷方式

windows - 在 vim 中使用 Windows 快捷方式