0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Windows バージョン、ビルド番号、リビジョンの取得

Posted at

これら情報がWin32APIで書かれたアプリの起動時にちょいと必要になったのですが、
API調べるのが億劫だったので^^;

cmd.exe /c ver.exe

の出力をもらうことにしました(手抜き)。標準入出力のパイプ関連のソース(山本ワールドさん)を探して書いてみました。

bool ExecCMDCommand(char *cmd, char *outtxt, int outtxtlen)
{
  bool bret=true;

  HANDLE rPipe=NULL, wPipe=NULL;
  SECURITY_ATTRIBUTES sa{ sizeof(SECURITY_ATTRIBUTES) };
  sa.bInheritHandle=TRUE;
  if(CreatePipe(&rPipe, &wPipe, &sa, 0)==FALSE) return false;

  char cmdline[MAX_PATH]{};
  sprintf(cmdline, "cmd.exe /c %s", cmd);

  STARTUPINFO si{ sizeof(STARTUPINFO) };
  si.dwFlags=STARTF_USESTDHANDLES| STARTF_USESHOWWINDOW;
  si.hStdOutput=si.hStdError=wPipe;
  si.wShowWindow=SW_HIDE;
  PROCESS_INFORMATION pi{};
  CreateProcess(NULL, cmdline, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
  WaitForInputIdle(pi.hProcess, INFINITE);//SOFTWARETIMEOUT);
  WaitForSingleObject(pi.hProcess, INFINITE);//SOFTWARETIMEOUT);
  CloseHandle(pi.hThread); CloseHandle(pi.hProcess);
  
  DWORD tlen, len;
  PeekNamedPipe(rPipe, NULL, 0, NULL, &tlen, NULL);
  if(tlen>0){
    ReadFile(rPipe, outtxt, outtxtlen, &len, NULL);
  }else{
    bret=false;
  }
  
  CloseHandle(rPipe); CloseHandle(wPipe);
  return bret;
}

こんな感じか。あとは適当に文字列切り出して終わり。説明も、ver.exeの出力と同じフォーマットだよ、で済むし。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?