1
votes

I am building MFC Dialog Application.
I use Visual Studio 2010, MFC 2008/2010.
In my cpp file, i have some code to get info of the process. the process user name is SYSTEM.

HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
        PROCESS_VM_READ,
        FALSE, 0); // 0 is my process id
    DWORD testerror = GetLastError();
    // Get the process name.

    if (NULL != hProcess )
    {
        HMODULE hMod;
        DWORD cbNeeded;

        if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
            &cbNeeded) )
        {
            GetModuleBaseName( hProcess, hMod, namestr, 
                sizeof(namestr)/sizeof(TCHAR) );
        }
    }
    else 
    {
        MessageBox(L"", L"", MB_OK);
    }
    CloseHandle();

Result is displaying MessageBox, because handle is NULL.
I get GetLastError() and it return 5 ( Access is denied).
I dont know why? Thanks a lot!

1
FormatMessage() will map GetLastError()'s integer return into a more informative error description. That might help you.acraig5075
Have you read the documentation for OpenProcess? It looks like you can't access the System process in this fashion.The Forest And The Trees
In 0 is my process id, what does "my" mean? Is it the ID of another process or the current process? In the latter case, you don't need to open the process at all, you can just use GetCurrentProcess.Frerich Raabe
@acraig5075: i got result of func GetlastError() and i had got meaning of error code 5. Its Access denied. Now i just want to fix it :(lex_luther_9x
@TheForestAndTheTrees: yes yes. I can't Access the System process. Can u help me? Please! Thankslex_luther_9x

1 Answers

1
votes

The system idle process and some other system process is not allowed to get their info.

The system idle process (ID 0) is included in the snapshot under the name [System Process], you can't open a handle for it as the documentation for OpenProcess specifically says:

If the specified process is the System Process (0x00000000), the function fails and the last error code is ERROR_INVALID_PARAMETER. If the specified process is the Idle process or one of the CSRSS processes, this function fails and the last error code is ERROR_ACCESS_DENIED because their access restrictions prevent user-level code from opening them.