Re: Acquiring process handle and convert it into PID

Michael C <[email protected]> Wed, 30 Nov 2016 08:14:42 -0800
Newsgroups gmane.comp.python.ctypes
Message-ID <CANyKM1jAAQTm12Er+1iKySwZoTXQ469Qn5bHUr_ZkcMxZCuv-A@mail.gmail.com>
Why is it a bad idea?

On Wed, Nov 30, 2016 at 7:55 AM, eryk sun <[email protected]> wrote:

> On Tue, Nov 29, 2016 at 10:07 PM, Michael C
> <[email protected]> wrote:
> > I ran this but the value returned was 0. I am trying to save time by
> > automating the PID acquisition process.
> >
> > import ctypes
> > import time
> >
> > time.sleep(2)
> > handle = ctypes.windll.user32.GetForegroundWindow()
> > print(handle)
> > PID = ctypes.windll.kernel32.GetProcessId(handle)
> > print (PID)
>
> GetProcessId is the wrong function.
>
> To begin with, before writing a single line of code, you need to
> familiarize yourself with the subject of WinAPI handles. Carefully
> read the following pages:
>
> Object categories
> https://msdn.microsoft.com/en-us/library/ms724515
> User
> https://msdn.microsoft.com/en-us/library/ms725486
> GDI
> https://msdn.microsoft.com/en-us/library/ms724291
> Kernel
> https://msdn.microsoft.com/en-us/library/ms724485
>
> Here's the function you'll need:
>
> GetWindowThreadProcessId
> https://msdn.microsoft.com/en-us/library/ms633522
>
> Also, please stop using ctypes.windll. The global library loaders were
> an extremely bad idea.
>

------------------------------------------------------------------------------

_______________________________________________
ctypes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ctypes-users