ProcessHandle is always NULL on 64 bit windows 7 for 32 bit application

Hello,

I have a 32 bit application that is required to close processes depending upon the situation. The application runs in my corporation and under certain cirmumstances I need to close certain applications on the desktop.

So I am using the following to try and obtain the process handle;

                  ProcessHandle =  OpenProcess( PROCESS_CREATE_THREAD	||
                                                PROCESS_VM_OPERATION	||
                                                PROCESS_VM_WRITE        ||
                                                PROCESS_VM_READ         ||
                                                PROCESS_TERMINATE,
                                                false,
                                                ProcessId );
Then we would execute the following code to actually terminate the process.

if ( ProcessHandle > 0 )
                  {

                       GetExitCodeProcess( ProcessHandle, &ExitCode )          ;
                       TerminateProcess( ProcessHandle, ExitCode )             ;
                       Application->ProcessMessages()                          ;
                       CloseHandle( ProcessHandle )                            ;

                  }


I know the process id, but every time I run this code the ProcessHandle is returned as NULL, even if I am running as an administrator. This has worked for many years and seems to work on the 32/64 bit windows but not on a a pure 64 bit windows machine.

Could someone tell me what I need to do to get this to work, as it is quite critical at the moment. 

Thank you 

Mike
0
Mike
10/6/2011 2:19:45 AM
📁 embarcadero.delphi.nativeapi
📃 1236 articles.
⭐ 0 followers.

💬 2 Replies
👁️‍🗨️ 593 Views

> {quote:title=Mike Browne wrote:}{quote}
> Hello,
> 
> I have a 32 bit application that is required to close processes depending upon the situation. The application runs in my corporation and under certain cirmumstances I need to close certain applications on the desktop.
> 
> So I am using the following to try and obtain the process handle;
> 
>                   ProcessHandle =  OpenProcess( PROCESS_CREATE_THREAD	||
>                                                 PROCESS_VM_OPERATION	||
>                                                 PROCESS_VM_WRITE        ||
>                                                 PROCESS_VM_READ         ||
>                                                 PROCESS_TERMINATE,
>                                                 false,
>                                                 ProcessId );
> Then we would execute the following code to actually terminate the process.
> 
> if ( ProcessHandle > 0 )
>                   {
> 
>                        GetExitCodeProcess( ProcessHandle, &ExitCode )          ;
>                        TerminateProcess( ProcessHandle, ExitCode )             ;
>                        Application->ProcessMessages()                          ;
>                        CloseHandle( ProcessHandle )                            ;
> 
>                   }
> 
> 
> I know the process id, but every time I run this code the ProcessHandle is returned as NULL, even if I am running as an administrator. This has worked for many years and seems to work on the 32/64 bit windows but not on a a pure 64 bit windows machine.
> 
> Could someone tell me what I need to do to get this to work, as it is quite critical at the moment. 
> 
> Thank you 
> 
> Mike

A couple of thoughts

On a pure64bit os (server2008 without the 32bit subsytem enabled?) how can there be a 32bit program to close in the first place?
You're not checking GetLastError after OpenProcess to see why it failed - that could give you a clue
Why so many PROCESS_* flags when all you're doing is terminating it
Is the ProcessId correct, can't tell from the snippet, as that's not included
GetExitCodeProcess is called before TerminateProcess, while the process is still running (so the exit code is invalid), and TerminateProcess gets the exit code anyway (so GetExitCodeProcess was redundant anyway) 
There's a general not checking if the calls have failed or worked in the whole snippet - it's hard to tell why something fails when you don't know where it started failing - eg is the code for getting the processid checking for errors, or just assuming it got the right id and all is well
0
karl
10/6/2011 6:21:27 AM
Thank you Karl,

We are now looking at this code closer. We have added the error checking to see what the error code is. This is actually running on a 64 bit windows 7 machine. 

Mike

> {quote:title=karl pritchett wrote:}{quote}
> > {quote:title=Mike Browne wrote:}{quote}
> > Hello,
> > 
> > I have a 32 bit application that is required to close processes depending upon the situation. The application runs in my corporation and under certain cirmumstances I need to close certain applications on the desktop.
> > 
> > So I am using the following to try and obtain the process handle;
> > 
> >                   ProcessHandle =  OpenProcess( PROCESS_CREATE_THREAD	||
> >                                                 PROCESS_VM_OPERATION	||
> >                                                 PROCESS_VM_WRITE        ||
> >                                                 PROCESS_VM_READ         ||
> >                                                 PROCESS_TERMINATE,
> >                                                 false,
> >                                                 ProcessId );
> > Then we would execute the following code to actually terminate the process.
> > 
> > if ( ProcessHandle > 0 )
> >                   {
> > 
> >                        GetExitCodeProcess( ProcessHandle, &ExitCode )          ;
> >                        TerminateProcess( ProcessHandle, ExitCode )             ;
> >                        Application->ProcessMessages()                          ;
> >                        CloseHandle( ProcessHandle )                            ;
> > 
> >                   }
> > 
> > 
> > I know the process id, but every time I run this code the ProcessHandle is returned as NULL, even if I am running as an administrator. This has worked for many years and seems to work on the 32/64 bit windows but not on a a pure 64 bit windows machine.
> > 
> > Could someone tell me what I need to do to get this to work, as it is quite critical at the moment. 
> > 
> > Thank you 
> > 
> > Mike
> 
> A couple of thoughts
> 
> On a pure64bit os (server2008 without the 32bit subsytem enabled?) how can there be a 32bit program to close in the first place?
> You're not checking GetLastError after OpenProcess to see why it failed - that could give you a clue
> Why so many PROCESS_* flags when all you're doing is terminating it
> Is the ProcessId correct, can't tell from the snippet, as that's not included
> GetExitCodeProcess is called before TerminateProcess, while the process is still running (so the exit code is invalid), and TerminateProcess gets the exit code anyway (so GetExitCodeProcess was redundant anyway) 
> There's a general not checking if the calls have failed or worked in the whole snippet - it's hard to tell why something fails when you don't know where it started failing - eg is the code for getting the processid checking for errors, or just assuming it got the right id and all is well
0
Mike
10/7/2011 1:00:38 PM
Reply: