我的程序将把进程名称连接成names
字符串。如何更改它以包含进程ID而不是名称? names
应该是什么类型,如何初始化以及如何连接其中的每个proc32.th32ProcessID
?
PROCESSENTRY32 proc32;
TCHAR names[MAX_PATH]=L"";
if(hSnap == INVALID_HANDLE_VALUE)
{
cout<<"invalid handle value error!\n";
return;
}
proc32.dwSize = sizeof(PROCESSENTRY32);
if(!Process32First(hSnap, &proc32))
{
cout<<"Tread32First() error!\n";
CloseHandle(hSnap);
return ;
}
do
{
//cout<<"Current process id: "<<GetCurrentProcessId()<<"\n";
wcout<<L"Process Name: "<<proc32.szExeFile<<"\n";
cout<<"Process ID: " <<proc32.th32ProcessID<<"\n";
cout<<"Thread Count: "<<proc32.cntThreads<<"\n"<<endl;
lstrcat(names, proc32.szExeFile);
lstrcat(names, L"\n");
}while(Process32Next(hSnap, &proc32));
答案 0 :(得分:0)
因为你还在使用C ++,所以你应该使用它。使用std::vector
,std::wstring
等:
PROCESSENTRY32W proc32;
vector<wstring> names;
vector<DWORD> ids;
if (hSnap == INVALID_HANDLE_VALUE)
{
cout << "invalid handle value error!" << endl;
return;
}
proc32.dwSize = sizeof(PROCESSENTRY32W);
if (!Process32FirstW(hSnap, &proc32))
{
cout << "Tread32First() error!" << endl;
CloseHandle(hSnap);
return ;
}
do
{
//cout << "Current process id: " << GetCurrentProcessId() << endl;
wcout << L"Process Name: " << proc32.szExeFile << endl;
cout << "Process ID: " << proc32.th32ProcessID << endl;
cout << "Thread Count: " << proc32.cntThreads << endl << endl;
names.push_back(proc32.szExeFile);
ids.push_back(proc32.th32ProcessID);
}
while (Process32Next(hSnap, &proc32));
// use names and ids as needed...
或者:
PROCESSENTRY32W proc32;
vector<PROCESSENTRY32W> procs;
if (hSnap == INVALID_HANDLE_VALUE)
{
cout << "invalid handle value error!" << endl;
return;
}
proc32.dwSize = sizeof(PROCESSENTRY32W);
if (!Process32FirstW(hSnap, &proc32))
{
cout << "Tread32First() error!" << endl;
CloseHandle(hSnap);
return ;
}
do
{
//cout << "Current process id: " << GetCurrentProcessId() << endl;
wcout << L"Process Name: " << proc32.szExeFile << endl;
cout << "Process ID: " << proc32.th32ProcessID << endl;
cout << "Thread Count: " << proc32.cntThreads << endl << endl;
procs.push_back(proc32);
}
while (Process32Next(hSnap, &proc32));
// use procs as needed...