0
1

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 5 years have passed since last update.

C++ Builder XE4 > プロセス > 指定名のプロセスを起動している数だけTMemoに表示

Last updated at Posted at 2018-11-05
動作環境
C++ Builder XE4
動作環境1: Windows 7 Pro (32bit)
動作環境2: Windows 10 Pro (64bit) バージョン 1803 (April 2018 Update)

処理概要

  • タスクマネージャのプロセスタブに表示されているプロセス
  • 指定の名前がある場合にTMemoに表示
    • 例: notepad.exe

参考ページ

情報感謝です。

実装

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

# ifndef Unit1H
# define Unit1H
//---------------------------------------------------------------------------
# include <System.Classes.hpp>
# include <Vcl.Controls.hpp>
# include <Vcl.StdCtrls.hpp>
# include <Vcl.Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE で管理されるコンポーネント
	TButton *Button1;
	TMemo *Memo1;
	void __fastcall Button1Click(TObject *Sender);
private:	// ユーザー宣言
	void __fastcall TForm1::ListUpProcess_NameOf(String exename);
public:		// ユーザー宣言
	__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
# endif
Unit1.cpp
//---------------------------------------------------------------------------

# include <vcl.h>
# pragma hdrstop

# include <Tlhelp32.hpp>
# include "Unit1.h"
//---------------------------------------------------------------------------
# pragma package(smart_init)
# pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListUpProcess_NameOf(String exename)
{
	HANDLE ListHandle;	// タスクマネージャ > プロセスリスト 用ハンドル
	TProcessEntry32 Pentry; // 個々のプロセス閲覧用

	ListHandle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

	if (ListHandle == INVALID_HANDLE_VALUE) {
		return;
	}

	Memo1->Clear(); // UI: 表示用

	Pentry.dwSize = sizeof(TProcessEntry32);
	Process32First(ListHandle, &Pentry);
	while(1) {
		String pnam = Pentry.szExeFile;
		if (pnam.Pos(exename) > 0) {
			Memo1->Lines->Add(pnam); // UI: 表示用
		}

		if (Process32Next(ListHandle, &Pentry) == false) {
			break;
		}
	}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
	ListUpProcess_NameOf(L"notepad.exe");
}
//---------------------------------------------------------------------------

動作例

  1. メモ帳(notepad.exe)を2つ起動する
  2. ボタンを押す

2018-11-05_18h24_34.png

用途

  • 同じソフトを開ける数を4つに制限したい、など
    • プロセスタブ上のプロセス数に基づく制限
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?