c++迭代进程并找出每个进程的命令行参数

C++ iterate processes and find out command line args of each process

本文关键字:进程 命令行 参数 迭代 c++      更新时间:2023-10-16

我有以下问题要解决(VS2012, c++)我必须从我的exe中找出特定的HTA应用程序是否正在运行。为此,我必须找到进程mshta并检查它是否具有正确的参数(应该以"mshta someta .hta"开始)。我的第一次尝试是迭代流程/模块,我现在可以这样做。我看到列出的mshta及其PID。但是,我没有找到获得信息的方法,它是如何开始的。有办法吗?

ProcessExists(wchar_t* processName)
{
    DWORD aProcesses[1024], cbNeeded, cProcesses;
    unsigned int i;
    if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
    {
        return false;
    }

    // Calculate how many process identifiers were returned.
    cProcesses = cbNeeded / sizeof(DWORD);
    // Print the name and process identifier for each process.
    for ( i = 0; i < cProcesses; i++ )
    {
        if( aProcesses[i] != 0 )
        {
            PrintProcessNameAndID( aProcesses[i] );
        }
    }
    return false;
 }
 void PrintProcessNameAndID( DWORD processID )
{
    TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
    // Get a handle to the process.
    HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                               PROCESS_VM_READ,
                               FALSE, processID );
    // Get the process name.
    if (NULL != hProcess )
    {
        HMODULE hMod;
        DWORD cbNeeded;
        if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
         &cbNeeded) )
        {
            GetModuleBaseName( hProcess, hMod, szProcessName, 
                           sizeof(szProcessName)/sizeof(TCHAR) );

        }
    }
    // Print the process name and identifier.
    dprintf( TEXT("%s  (PID: %u) %s %sn"), szProcessName, processID );
   // Release the handle to the process.
   CloseHandle( hProcess );
}

我最终使用了这里提出的解决方案:http://www.codeproject.com/Articles/19685/Get-Process-Info-with-NtQueryInformationProcess