わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。

指定されたプロセスの終了ステータスを取得する

環境変数の取得は、GetExitCodeProcess()関数で取得できます。

#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <windows.h>



/*
	指定されたプロセスの終了ステータスを取得する
*/
int _tmain
(
	  int argc
	, _TCHAR* argv[]
)
{
	// 標準出力にユニコード出力する
	setlocale( LC_ALL, "Japanese" );

	HRESULT hResult = E_FAIL;


	STARTUPINFO         tStartupInfo = { 0 };
	PROCESS_INFORMATION tProcessInfomation = { 0 };

	/*
		プロセスの起動(pingを起動する)
	*/
	BOOL bResult = CreateProcess(
			  L"c:¥¥Windows¥¥System32¥¥PING.exe"
			, L"-6 127.0.0.1"
			, NULL
			, NULL
			, FALSE
			, 0
			, NULL
			, NULL
			, &tStartupInfo
			, &tProcessInfomation
		);
	if ( 0  == bResult ) {
		return( HRESULT_FROM_WIN32( ::GetLastError() ) );
	}


	/*
		プロセスの終了を待つ
	*/
	DWORD dwResult = ::WaitForSingleObject(
			  tProcessInfomation.hProcess
			, INFINITE
		);
	if ( WAIT_FAILED  == dwResult ) {
		hResult = HRESULT_FROM_WIN32( ::GetLastError() );
		goto err;
	}


	/*
		プロセスの終了コードを取得する
	*/
	DWORD dwExitCode;
	bResult = ::GetExitCodeProcess(
			  tProcessInfomation.hProcess
			, &dwExitCode
		);
	if ( 0 == bResult ) {
		hResult = HRESULT_FROM_WIN32( ::GetLastError() );
		goto err;
	}

	// 終了コードの表示
	wprintf( L"dwExitCode = %d¥n", dwExitCode );


err:
	// ハンドルの解放
	::CloseHandle( tProcessInfomation.hProcess );
	::CloseHandle( tProcessInfomation.hThread  );

	// 正常終了
	return( 0 );
}



実行結果


127.0.0.1 に ping を送信しています 32 バイトのデータ:
127.0.0.1 からの応答: バイト数 =32 時間 <1ms TTL=128
127.0.0.1 からの応答: バイト数 =32 時間 <1ms TTL=128
127.0.0.1 からの応答: バイト数 =32 時間 <1ms TTL=128
127.0.0.1 からの応答: バイト数 =32 時間 <1ms TTL=128

127.0.0.1 の ping 統計:
    パケット数: 送信 = 4、受信 = 4、損失 = 0 (0% の損失)、
ラウンド トリップの概算時間 (ミリ秒):
    最小 = 0ms、最大 = 0ms、平均 = 0ms
dwExitCode = 0






わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。