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

3 に答える 3

2

ShellExecuteまたはCreateProcessリンクファイルを開くことができるはずです。関連するファイルやプログラムが見つからない場合は、いつでもそれらの API を使用して、面倒な作業を "cmd start" または "explorer" に委任できます。例えばShellExecute(0, "open", "explorer", linkfile, 0, SW_SHOW);

于 2010-12-10T16:34:21.787 に答える
1

ShellExecute動作するはずです。

しかし、 ...

int main( int, wchar_t* )

...私が知っているコンパイラは、この署名をサポートしていません。書くだけ:

int main()

また、診断メッセージについては、FormatMessageWindows API 関数を使用するか、コードが Visual C++ 専用の場合は、そのコンパイラにバンドルされている適切なサポート クラスを使用します。

乾杯 & hth.,

于 2010-12-10T16:54:03.920 に答える
1

あなたが何について不確かなのかわかりません。あなたが観察した行動は文書化されています。

ShellExecute の「開く」操作は、file 引数によって参照されるファイルを「開く」ときにシェルが行うことは何でも実行します (ショートカットを右クリックして明示的に「開く」を選択できますが、これは .lnk の既定の操作でもあります)。 、ダブルクリックと同じです)。

ショートカットファイルを「開く」ターゲットを「開く」、ターゲットが実行可能ファイルの場合は実行する、ドキュメントまたはデータファイルの場合は関連するプログラムで開く、またはプログラムがない場合はプロンプトを表示する関連する。

于 2010-12-10T16:45:50.213 に答える