Вопрос

My program below will concatenate the names of the processes into the names string. How can I change it to include the process ID's instead of names? What type should names be, how to initialise it and how to concatenate every proc32.th32ProcessID in it ?

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));
Это было полезно?

Решение

Since you are using C++ anyway, you should make use of it. Use std::vector, std::wstring, etc:

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...

Or:

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...    
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top