Click here to Skip to main content
13,863,412 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
No matter what I do. OpenProcess keeps returning null.

#include <windows.h>
#include <iostream>
#include <psapi.h>

using namespace std;
void loop(){
DWORD pid;
HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, false, pid);

if (hProc == NULL){ cout << "Cannot open process.";

LPWSTR path = new WCHAR[MAX_PATH];
DWORD charsCarried = MAX_PATH;
}}
int main(){loop();}

What I have tried:

No matter what I do. OpenProcess keeps returning null.

#include <windows.h>
#include <iostream>
#include <psapi.h>

using namespace std;
void loop(){
DWORD pid;
	HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, false, pid);

	if (hProc == NULL){ cout << "Cannot open process.";

	LPWSTR path = new WCHAR[MAX_PATH];
	DWORD charsCarried = MAX_PATH; 
}}
int main(){loop();}
Posted
Updated 11-Feb-19 6:01am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Most likely because you have not given a value to the pid parameter, so there is nothing to open. See OpenProcess function | Microsoft Docs[^].
   
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Start with the documentation: OpenProcess function | Microsoft Docs[^]
Instead of just printing a "cannot open" message, use the GetLastError function (Windows)[^] to fetch information on why it failed. Without that, you are just guessing as to why!
   
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

You might also want to read the documentation on PROCESS_ALL_ACCESS, here[^].
   
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Mr. Griff has very good advice. To expand on it just a little, here is a function I use to help with this kind of thing :
///////////////////////////////////////////////////////////////////////////////

bool GetErrorMsg( DWORD lastError, PTSTR buffer, UINT bufferSize )
{
    PTSTR pmsgbuf = nullptr;
    DWORD msgsize = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
                                   FORMAT_MESSAGE_FROM_SYSTEM,
                                   nullptr, lastError, LANG_NEUTRAL,
                                   (PTSTR)&pmsgbuf, bufferSize, nullptr );
    if( ! msgsize )
    {
        _stprintf( buffer, _T( "FormatMessage() failed for error code %d" ), lastError );
        return false;
    }

    _tcsncpy( buffer, pmsgbuf, bufferSize-1 );
    LocalFree( (HLOCAL) pmsgbuf );
    PTSTR p = _tcschr( buffer, ch::Return );
    if( *p )
        *p = 0;
    return true;
}

///////////////////////////////////////////////////////////////////////////////

bool GetLastErrorMsg( PTSTR buffer, UINT bufferSize )
{
    DWORD lastError = GetLastError();
    return GetErrorMsg( lastError, buffer, bufferSize );
}
This will get a textual representation of the error so you can print it or display it or what ever else you want to do with it. The point is you should find out what the error condition so you can fix it.
   
Comments
0x01AA 11-Feb-19 11:16am
   
Yes yes of course, that is a very good hint, but Mr. MacCutchan pointed the most likely cause of the problem :-)
Rick York 11-Feb-19 12:50pm
   
Yes, he did. The OP could likely have found it himself had he displayed what the error condition was.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web05 | 2.8.190214.1 | Last Updated 11 Feb 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100