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

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

我有以下问题需要解决(VS2012,C++) 我必须从我的 exe 中找出特定的 HTA 应用程序是否是 运行。为此,我必须找到进程 mshta 并检查它是否有正确的参数(应该以 "mshta somehta.hta" 启动)。我的第一次尝试是迭代 processes/modules,我现在可以这样做。我看到列出了 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 %s\n"), szProcessName, processID );

   // Release the handle to the process.

   CloseHandle( hProcess );
}

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