Re: win32 process name

Discussion in 'Python' started by Fredrik Lundh, Dec 22, 2004.

  1. "phil" <> wrote:

    > from win32all
    > EnumProcesses gives me the pids, then
    > OpenProcess(pid) gives me a handle.
    > Then what?
    > GetModuleFileNameEX? It requires two handles as args
    > and I can't figure out which one is the handle from OpenProcess
    > and what it wants for the other one and I can't find any
    > Win32 SDK docs that help.


    http://msdn.microsoft.com/library/en-us/perfmon/base/getmodulefilenameex.asp

    describes a function with two [in] arguments, and one [out] argument.
    the first argument is the process handle, the second a module handle;
    the second argument can be NULL.

    > This ought to be a nobrainer. But Nooo. Its Windows.


    it can be pretty tricky on other platforms too; that's why Unix programs usually
    solve this by writing their PID to a file in a known location, so that other programs
    can find them without having to resort to more or less stupid tricks.

    </F>
    Fredrik Lundh, Dec 22, 2004
    #1
    1. Advertising

  2. Fredrik Lundh

    Keith Dart Guest

    Fredrik Lundh wrote:
    > "phil" <> wrote:
    >
    >
    >>from win32all
    >>EnumProcesses gives me the pids, then
    >>OpenProcess(pid) gives me a handle.
    >>Then what?
    >>GetModuleFileNameEX? It requires two handles as args
    >>and I can't figure out which one is the handle from OpenProcess
    >>and what it wants for the other one and I can't find any
    >>Win32 SDK docs that help.

    >
    >
    > http://msdn.microsoft.com/library/en-us/perfmon/base/getmodulefilenameex.asp
    >
    > describes a function with two [in] arguments, and one [out] argument.
    > the first argument is the process handle, the second a module handle;
    > the second argument can be NULL.
    >
    >
    >>This ought to be a nobrainer. But Nooo. Its Windows.

    >
    >
    > it can be pretty tricky on other platforms too; that's why Unix programs usually
    > solve this by writing their PID to a file in a known location, so that other programs
    > can find them without having to resort to more or less stupid tricks.
    >
    > </F>


    On Linux, you can just scan the /proc directory. This is what the procps
    package does. From the command line:

    310 $ ps -o cmd --no-heading 867
    metalog [MASTER]

    In the pyNMS package on sourceforge
    (http://sourceforge.net/projects/pynms) there is a module called
    Linux.procfs, and in it there is an object called "ProcStat" that lets
    you get the process info easily (if running on Linux).

    Python> import procfs
    Python> procfs.ProcStat(1)
    Python> pid1 = procfs.ProcStat(1)
    Python> print pid1
    cmdline: init [3]
    cmaj_flt: 63690958
    cmin_flt: 186761321
    cnswap: 0
    command: (init)
    eip: 35
    esp: 43
    exit_signal: 0
    flags: 256
    it_real_value: 0
    maj_flt: 224
    min_flt: 1963
    mm_end_code: 134538444
    mm_start_code: 134512640
    mm_start_stack: 3221225232
    nice: 0
    nswap: 0
    pgrp: 0
    pid: 1
    ppid: 0
    priority: 15
    processor: 0
    rlim_cur: 4294967295
    rss: 117
    session: 0
    sig_blocked: 0
    sig_catch: 671819267
    sig_ignore: 1475401980
    sig_pending: 0
    start_time: 42
    state: S
    tms_cstime: 731277
    tms_cutime: 9593767
    tms_stime: 237
    tms_utime: 75
    tty_nr: 0
    tty_pgrp: -1
    vsize: 1429504
    wchan: 3222957162


    --
    -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Keith Dart <>
    public key: ID: F3D288E4
    =====================================================================
    Keith Dart, Dec 22, 2004
    #2
    1. Advertising

  3. Fredrik Lundh

    Keith Dart Guest

    Fredrik Lundh wrote:
    > "phil" <> wrote:
    >
    >
    >>from win32all
    >>EnumProcesses gives me the pids, then
    >>OpenProcess(pid) gives me a handle.
    >>Then what?
    >>GetModuleFileNameEX? It requires two handles as args
    >>and I can't figure out which one is the handle from OpenProcess
    >>and what it wants for the other one and I can't find any
    >>Win32 SDK docs that help.

    >
    >
    > http://msdn.microsoft.com/library/en-us/perfmon/base/getmodulefilenameex.asp
    >
    > describes a function with two [in] arguments, and one [out] argument.
    > the first argument is the process handle, the second a module handle;
    > the second argument can be NULL.
    >
    >
    >>This ought to be a nobrainer. But Nooo. Its Windows.

    >
    >
    > it can be pretty tricky on other platforms too; that's why Unix programs usually
    > solve this by writing their PID to a file in a known location, so that other programs
    > can find them without having to resort to more or less stupid tricks.
    >
    > </F>


    On Linux, you can just scan the /proc directory. This is what the procps
    package does. From the command line:

    310 $ ps -o cmd --no-heading 867
    metalog [MASTER]

    In the pyNMS package on sourceforge
    (http://sourceforge.net/projects/pynms) there is a module called
    Linux.procfs, and in it there is an object called "ProcStat" that lets
    you get the process info easily (if running on Linux).

    Python> import procfs
    Python> procfs.ProcStat(1)
    Python> pid1 = procfs.ProcStat(1)
    Python> print pid1
    cmdline: init [3]
    cmaj_flt: 63690958
    cmin_flt: 186761321
    cnswap: 0
    command: (init)
    eip: 35
    esp: 43
    exit_signal: 0
    flags: 256
    it_real_value: 0
    maj_flt: 224
    min_flt: 1963
    mm_end_code: 134538444
    mm_start_code: 134512640
    mm_start_stack: 3221225232
    nice: 0
    nswap: 0
    pgrp: 0
    pid: 1
    ppid: 0
    priority: 15
    processor: 0
    rlim_cur: 4294967295
    rss: 117
    session: 0
    sig_blocked: 0
    sig_catch: 671819267
    sig_ignore: 1475401980
    sig_pending: 0
    start_time: 42
    state: S
    tms_cstime: 731277
    tms_cutime: 9593767
    tms_stime: 237
    tms_utime: 75
    tty_nr: 0
    tty_pgrp: -1
    vsize: 1429504
    wchan: 3222957162


    --
    -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Keith Dart <>
    public key: ID: F3D288E4
    =====================================================================
    Keith Dart, Dec 22, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rtm
    Replies:
    0
    Views:
    799
  2. Java and Swing
    Replies:
    1
    Views:
    674
    Chris Lambacher
    Oct 24, 2005
  3. Tim Golden
    Replies:
    0
    Views:
    441
    Tim Golden
    Oct 21, 2005
  4. Qi Lu
    Replies:
    0
    Views:
    403
    Qi Lu
    Jul 14, 2008
  5. Mike Stead
    Replies:
    3
    Views:
    269
    Roger Pack
    Mar 25, 2010
Loading...

Share This Page