LoginSignup
0
1

More than 5 years have passed since last update.

C++ Builder XE4 > ProcessLogger > 指定の.exeファイルのプロセスIDをロギングする v0.1

Posted at
動作環境
C++ Builder XE4

処理概要

  • 指定の.exeファイルのプロセスIDを調べる (例: notepad.exe)
  • 調べたプロセスIDをファイル出力する (ロギング)

関連

v0.1

実装

Unit1.h
//---------------------------------------------------------------------------

#ifndef MainH
#define MainH
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE で管理されるコンポーネント
    TMemo *Memo1;
    TButton *Button1;
    TEdit *E_exeName;
    TTimer *TMR_logging;
    TCheckBox *CHK_log;
    void __fastcall Button1Click(TObject *Sender);
    void __fastcall FormDestroy(TObject *Sender);
    void __fastcall TMR_loggingTimer(TObject *Sender);
private:    // ユーザー宣言
    int __fastcall ListUpProcess_NameOf(String exename);
    void __fastcall checkProcessID(void);
    TStringList *m_logSL; // ロギング用
public:     // ユーザー宣言
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include <Tlhelp32.hpp>
#include "Main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------

static const String kFilename_log = L"ProcLogger.csv"; // ログ用

__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    Memo1->Lines->Clear();

    //
    m_logSL = new TStringList();

    // ログタイマー設定
    TMR_logging->Enabled = false;
    TMR_logging->Interval = 10000; // msec
    TMR_logging->Enabled = true;
}

void __fastcall TForm1::FormDestroy(TObject *Sender)
{
    delete m_logSL;
}

//---------------------------------------------------------------------------

int __fastcall TForm1::ListUpProcess_NameOf(String exename)
{
    HANDLE ListHandle;  // タスクマネージャ > プロセスリスト 用ハンドル
    TProcessEntry32 Pentry; // 個々のプロセス閲覧用

    ListHandle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

    if (ListHandle == INVALID_HANDLE_VALUE) {
        return 0;
    }

    Pentry.dwSize = sizeof(TProcessEntry32);
    Process32First(ListHandle, &Pentry);
    while(1) {
        String pnam = Pentry.szExeFile;
        int procId = Pentry.th32ProcessID;
        if (pnam.Pos(exename) > 0) {
            return procId;
        }

        if (Process32Next(ListHandle, &Pentry) == false) {
            break;
        }
    }
    return 0;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    checkProcessID();
}

void __fastcall TForm1::checkProcessID(void)
{
    int procId = ListUpProcess_NameOf(E_exeName->Text);

    String msg;
    msg = Now().FormatString(L"yyyy/mm/dd,hh:nn:ss");
    msg += L",procID," + IntToStr(procId);
    Memo1->Lines->Add(msg);

    m_logSL->Add(msg);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TMR_loggingTimer(TObject *Sender)
{
    if (CHK_log->Checked) {
        checkProcessID();
        m_logSL->SaveToFile(kFilename_log);
    }
}
//---------------------------------------------------------------------------

動作例

  1. メモ帳(notepad.exe)を起動する
  2. ProcessLoggerでロギング開始 (CHK_logをチェックする)

2018-11-13_20h06_11.png

上記の例では
A. 5816->3344 : メモ帳を再起動
B. 3344->0 : メモ帳を終了

0
1
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
1