0

我下面的程序会将进程的名称连接到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));
4

1 回答 1

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...    
于 2014-03-19T06:02:30.590 回答