GetCommandLine()関数でコマンドライン文字列が取得できます。コマンドライン文字列は実行ファ イルパスに続いて引数もスペースで区切った形で取得できます。このままでは1行の文字列で扱い にくいので、CommandLineToArgvW()関数にて、引数リストに分割して利用します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #include <stdio.h> #include <tchar.h> #include <iostream> #include <string> #include <windows.h> int _tmain ( int argc , _TCHAR* argv[] ) { // 標準出力にユニコード出力する setlocale ( LC_ALL, "Japanese" ); // コマンドライン文字列の取得 TCHAR * lpCommandLine = ::GetCommandLine(); // コマンドライン文字列の表示 std::wcout << lpCommandLine << std::endl; // 引数リストの数 int nArgc = 0; // 引数リストの取得 TCHAR ** lppArgv = ::CommandLineToArgvW( lpCommandLine, &nArgc ); // 引数リストの表示 for ( int iI = 0; iI < nArgc; iI++ ) { std::wcout << L "[" << iI << L "]" << lppArgv[ iI ] << std::endl; } // 引数リストの解放 LocalFree( lppArgv ); // 正常終了 return ( 0 ); } |
"c:\TestFolder\Test.exe" param1 param2
[0]"c:\TestFolder\Test.exe"
[1]param1
[2]param2