プロセス一覧を取得します。プロセスIdの列挙には、EnumProcesses()関数を使用します。 プロセスのパスを取得するには、GetProcessImageFileName()関数を利用しますが、GetProcessImageFileName()関数で取得できるパスは、デバイスのパス ("\device\HarddiskVolume3\"など)になります。デバイスパスから通常の"c:\"などのパスへの変換には、各ドライブのデバイスパスをあらかじめQueryDosDevice()関数 で取得しておいて、マッチングさせて変換します。
#include <tchar.h>
#include <iostream>
#include <string>
#include <vector>
#include <windows.h>
#include <Shlwapi.h>
#include <psapi.h>
// lib
#pragma comment( lib, "psapi.lib" )
/*
プロセスパスを列挙して取得する(64bit対応)
*/
void GetEnumProcessExeName
(
std::vector<std::wstring>& vecProcessPaths
)
{
DWORD dwaProcessID[ 8192 ];
DWORD dwRetSize = 0;
// ベクタ初期化
vecProcessPaths.clear();
/*
ドライブレターのデバイスパスを取得する
*/
std::vector<std::wstring> vecDriveDevicePath;
{
// L"a~zのデバイスパスを取得する"
for ( int nDrive = L'a'; nDrive <= 'z'; nDrive++ ) {
// ドライブレター
TCHAR waDrive[ 4 ] = { nDrive, L':', 0, 0 };
// デバイス文字列
TCHAR szDevicePath[ MAX_PATH ];
// デバイスパスの取得
if ( 0 == ::QueryDosDevice( waDrive, szDevicePath, _countof( szDevicePath ) ) ) {
szDevicePath[ 0 ] = L'¥0';
}
// デバイスパスの格納
vecDriveDevicePath.push_back( szDevicePath );
}
}
// プロセスの列挙
if ( 0 != ::EnumProcesses( dwaProcessID, sizeof( dwaProcessID ), &dwRetSize ) ) {
// 取得したプロセス情報を巡回
for ( UINT nI = 0; nI < ( dwRetSize / sizeof( DWORD ) ); nI++ ) {
HANDLE hProcess = NULL;
// プロセスをオープン
hProcess = ::OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwaProcessID[ nI ] );
if( NULL != hProcess ) {
// プロセス名の受け取り場所
TCHAR waFilePath[ MAX_PATH * 4 ] = { 0 };
// プロセスのファイルパスの取得
::GetProcessImageFileName( hProcess, waFilePath, _countof( waFilePath ) );
// ファイルパス(¥device¥HarddiskVolume3¥)
std::wstring strProcessFilePath = waFilePath;
// デバイスパスの変換
std::vector<std::wstring>::iterator ite = vecDriveDevicePath.begin();
std::vector<std::wstring>::iterator iteEnd = vecDriveDevicePath.end();
while( ite != iteEnd ) {
// ドライブ名
TCHAR waDrive[ 4 ] = { L'a' + (int)( ite - vecDriveDevicePath.begin() ), L':', 0, 0 };
// アイテム取得
std::wstring& strDevicePath = *ite++;
if ( 0 == strDevicePath.size() ) {
continue;
}
// デバイスパスの比較
if ( 0 == ::_wcsnicmp( waFilePath, strDevicePath.c_str(), strDevicePath.size() ) ) {
// ドライブパスに変換
strProcessFilePath = std::wstring( waDrive ) + std::wstring( &waFilePath[ strDevicePath.size() ] );
break;
}
}
/*
重複チェック
*/
{
std::vector<std::wstring>::iterator ite = vecProcessPaths.begin();
std::vector<std::wstring>::iterator iteEnd = vecProcessPaths.end();
while( ite != iteEnd ) {
if ( 0 == ::_wcsicmp( strProcessFilePath.c_str(), ite->c_str() ) ) {
// 重複していました。
break;
}
ite++;
}
// 重複していないものだけ追加で登録する
if ( ite == iteEnd ) {
// ベクタへ追加
vecProcessPaths.push_back( strProcessFilePath );
}
}
// プロセスハンドルのクローズ
::CloseHandle( hProcess );
}
}
}
}
/*
プロセス一覧の取得(64bit対応)
*/
int _tmain
(
int argc
, _TCHAR* argv[]
)
{
// 標準出力にユニコードを表示できるようにする
setlocale( LC_ALL, "Japanese" );
// プロセス一覧の取得用
std::vector<std::wstring> vecProcessPaths;
// プロセスの一覧を取得する(64bit対応)
GetEnumProcessExeName( vecProcessPaths );
/*
プロセス一覧の表示
*/
{
std::vector<std::wstring>::iterator ite = vecProcessPaths.begin();
std::vector<std::wstring>::iterator iteEnd = vecProcessPaths.end();
while( ite != iteEnd ) {
// パスを取得
std::wstring& strProcessPath = *ite++;
// 標準出力へ
std::wcout << strProcessPath.c_str() << std::endl;
}
}
return 0;
}
c:\Windows\System32\sihost.exe c:\Windows\System32\svchost.exe c:\Windows\System32\taskhostw.exe c:\Windows\System32\RuntimeBroker.exe c:\Windows\System32\igfxEM.exe c:\Windows\System32\igfxHK.exe c:\Windows\System32\igfxTray.exe c:\Windows\SystemApps\Microsoft.Windows.Cortana_cw5n1h2txyewy\RemindersServer.exe c:\Program Files\TortoiseSVN\bin\TSVNCache.exe c:\Windows\System32\SettingSyncHost.exe c:\Program Files (x86)\CyberLink\PowerDVD12\PDVD12Serv.exe c:\Program Files\Realtek\Audio\HDA\RtkNGUI64.exe c:\Program Files\CSR\CSR Harmony Wireless Software Stack\CsrHCRPServer.exe c:\Program Files\CSR\CSR Harmony Wireless Software Stack\CsrAudioguiCtrl.exe c:\Program Files\CSR\CSR Harmony Wireless Software Stack\CsrSyncMLServer.exe c:\Program Files\CSR\CSR Harmony Wireless Software Stack\vksts.exe c:\Program Files\CSR\CSR Harmony Wireless Software Stack\HarmonyUserStartup.exe c:\Program Files\CSR\CSR Harmony Wireless Software Stack\TrayApplication.exe c:\Program Files\iTunes\iTunesHelper.exe c:\Program Files\Windows Defender\MSASCuiL.exe c:\Users\dummy\AppData\Local\Microsoft\OneDrive\OneDrive.exe c:\org\tools\TvRock09u2\tvrock.exe c:\Program Files (x86)\DAEMON Tools Lite\DTLite.exe c:\Program Files (x86)\Common Files\Apple\Internet Services\iCloudServices.exe c:\Program Files (x86)\Common Files\Apple\Internet Services\iCloudDrive.exe c:\Program Files (x86)\Common Files\Apple\Internet Services\iCloudPhotos.exe c:\Program Files (x86)\FavGCalScheduler\FavGCalScheduler.exe c:\Program Files (x86)\Canon\Canon IJ Network Scan Utility\CNMNSUT.EXE c:\Program Files (x86)\Common Files\Freemake Shared\ProductUpdater\ProductUpdater.exe c:\Program Files (x86)\Sknet\MonsterTVHD\MonsterTVD.exe c:\Program Files (x86)\Common Files\Apple\Apple Application Support\APSDaemon.exe c:\Program Files (x86)\プロ生ちゃんSleep抑止\PronamaSleepStop.exe c:\Program Files (x86)\FavPopupCalendar\FavPopupCalendar.exe c:\Program Files (x86)\Common Files\Apple\Apple Application Support\secd.exe c:\Program Files\WindowsApps\Microsoft.SkypeApp_11.10.145.0_x64__kzf8qxf38zg5c\SkypeHost.exe c:\Program Files\Intel\Intel(R) Rapid Storage Technology\IAStorIcon.exe c:\Program Files\Intel\SUR\WILLAMETTE\ESRV\esrv.exe c:\Windows\System32\conhost.exe c:\Program Files (x86)\Mozilla Firefox\firefox.exe c:\Windows\System32\IME\SHARED\ImeBroker.exe c:\Program Files (x86)\sakura\sakura.exe c:\Windows\explorer.exe c:\Windows\SystemApps\ShellExperienceHost_cw5n1h2txyewy\ShellExperienceHost.exe c:\Windows\SystemApps\Microsoft.Windows.Cortana_cw5n1h2txyewy\SearchUI.exe c:\Windows\System32\SystemSettingsBroker.exe c:\Windows\System32\dllhost.exe c:\Windows\System32\ApplicationFrameHost.exe c:\Program Files (x86)\Common Files\Apple\Internet Services\ApplePhotoStreams.exe c:\Windows\System32\DataExchangeHost.exe c:\Program Files (x86)\FavMoviePlayer\FavMoviePlayer.exe c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\VCExpress.exe c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcpackages\vcpkgsrv.exe c:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\mspdbsrv.exe c:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\VCExpress.exe c:\Windows\System32\smartscreen.exe c:\Windows\System32\GamePanel.exe