process Id and HINSTANCE...what is the relation betweeen them? - Programmer

This is a discussion on process Id and HINSTANCE...what is the relation betweeen them? - Programmer ; Hello Alll, I am learning windows programming and getting confused more and more.. here is my latest confusion... What is the relation between processId (which we come to know from the task manager) and hInstance (which is one of the ...

+ Reply to Thread
Results 1 to 12 of 12

Thread: process Id and HINSTANCE...what is the relation betweeen them?

  1. process Id and HINSTANCE...what is the relation betweeen them?

    Hello Alll,
    I am learning windows programming and getting confused more and more..
    here is my latest confusion...
    What is the relation between processId (which we come to know from the
    task manager) and hInstance (which is one of the parameter passed in
    the winmain function) of a program?

    Or there isn't any relation at all? if yes..why? as both give us the
    unique number related to a running program..
    Thanks and Regards,
    Yogesh Joshi


  2. Re: process Id and HINSTANCE...what is the relation betweeen them?

    Hi yogpjosh!


    > What is the relation between processId (which we come to know from the
    > task manager)


    It does not come from the task-manager... it comes from the OS
    (process-manager)

    > and hInstance (which is one of the parameter passed in
    > the winmain function) of a program?


    There is no relation.


    > Or there isn't any relation at all? if yes..why? as both give us the
    > unique number related to a running program..


    hInstance is not a unique number, it is just a "handle" to the EXE...

    --
    Greetings
    Jochen

    My blog about Win32 and .NET
    http://blog.kalmbachnet.de/

  3. Re: process Id and HINSTANCE...what is the relation betweeen them?

    H is a handle; kind of like an internal pointer to special data in
    the system that you cannot access directly.

    It can be used via OpenProcess, and get the process ID.


    wrote in message
    news:1139159482.468622.38340@g44g2000cwa.googlegro ups.com...
    > Hello Alll,
    > I am learning windows programming and getting confused more and more..
    > here is my latest confusion...
    > What is the relation between processId (which we come to know from the
    > task manager) and hInstance (which is one of the parameter passed in
    > the winmain function) of a program?
    >
    > Or there isn't any relation at all? if yes..why? as both give us the
    > unique number related to a running program..
    > Thanks and Regards,
    > Yogesh Joshi
    >




  4. Re: process Id and HINSTANCE...what is the relation betweeen them?

    "Jochen Kalmbach [MVP]" wrote in message
    news:Ow5jsfnKGHA.140@TK2MSFTNGP12.phx.gbl...
    > Hi yogpjosh!
    >> What is the relation between processId (which we come to know from the
    >> task manager)

    >
    > It does not come from the task-manager... it comes from the OS
    > (process-manager)
    >
    >> and hInstance (which is one of the parameter passed in
    >> the winmain function) of a program?

    >
    > There is no relation.
    >
    >
    >> Or there isn't any relation at all? if yes..why? as both give us the
    >> unique number related to a running program..

    >
    > hInstance is not a unique number, it is just a "handle" to the EXE...


    In other words it's just the address where process is mapped in OS address
    space.

    >
    > --
    > Greetings
    > Jochen
    >
    > My blog about Win32 and .NET
    > http://blog.kalmbachnet.de/


    --
    Vladimir



  5. Re: process Id and HINSTANCE...what is the relation betweeen them?

    Btw, just noticed that my MSDN (April 2003) states that GetProcessId has the
    following prototype:

    void GetProcessId(
    HANDLE Process
    );



    --
    Vladimir

    "Mark Randall" wrote in message
    news:u1RENhnKGHA.3492@TK2MSFTNGP09.phx.gbl...
    > H is a handle; kind of like an internal pointer to special data
    > in the system that you cannot access directly.
    >
    > It can be used via OpenProcess, and get the process ID.
    >
    >
    > wrote in message
    > news:1139159482.468622.38340@g44g2000cwa.googlegro ups.com...
    >> Hello Alll,
    >> I am learning windows programming and getting confused more and more..
    >> here is my latest confusion...
    >> What is the relation between processId (which we come to know from the
    >> task manager) and hInstance (which is one of the parameter passed in
    >> the winmain function) of a program?
    >>
    >> Or there isn't any relation at all? if yes..why? as both give us the
    >> unique number related to a running program..
    >> Thanks and Regards,
    >> Yogesh Joshi
    >>

    >
    >




  6. Re: process Id and HINSTANCE...what is the relation betweeen them?

    This must be a new API because it has not existed up til now...
    also note the distinction between a HANDLE to a process - which
    is a user-mode HANDLE to a kernel PROCESS object, and
    a HINSTANCE which is not a handle at all - rather it is a pointer
    in thin disguise which points to the base address of the process/DLL -
    synonymous with a HMODULE. These last two "handles" are really
    just a hang-up from win16 days.

    James

    --
    Microsoft MVP - Windows SDK
    www.catch22.net
    Free Win32 Source and Tutorials


    "Scherbina Vladimir" wrote in message
    news:OfV6iyoKGHA.2696@TK2MSFTNGP14.phx.gbl...
    > Btw, just noticed that my MSDN (April 2003) states that GetProcessId has
    > the following prototype:
    >
    > void GetProcessId(
    > HANDLE Process
    > );
    >
    >
    >
    > --
    > Vladimir
    >
    > "Mark Randall" wrote in message
    > news:u1RENhnKGHA.3492@TK2MSFTNGP09.phx.gbl...
    >> H is a handle; kind of like an internal pointer to special data
    >> in the system that you cannot access directly.
    >>
    >> It can be used via OpenProcess, and get the process ID.
    >>
    >>
    >> wrote in message
    >> news:1139159482.468622.38340@g44g2000cwa.googlegro ups.com...
    >>> Hello Alll,
    >>> I am learning windows programming and getting confused more and more..
    >>> here is my latest confusion...
    >>> What is the relation between processId (which we come to know from the
    >>> task manager) and hInstance (which is one of the parameter passed in
    >>> the winmain function) of a program?
    >>>
    >>> Or there isn't any relation at all? if yes..why? as both give us the
    >>> unique number related to a running program..
    >>> Thanks and Regards,
    >>> Yogesh Joshi
    >>>

    >>
    >>

    >
    >




  7. Re: process Id and HINSTANCE...what is the relation betweeen them?

    Each Process has an internal data structure in the system space
    known as the executive process block which contains a field for
    the Process ID. The Executive process block also has a pointer
    to the handle table. So HANDLE you see for apis are nothing but
    indices into the handle table maintained in the system space and
    they increments in the order of 4,8,12,... like that. Try opening a
    couple of handles in a program to get the feel on the increments.

    So when someone does a CreateProcess(...), the handle is in fact
    a index into the local processes handle table which internally would
    point to the relevant system data structures.

    So the conculsion is that HANDLE is process specific while process id
    of a given process will always remain the same thoughout. You can
    different handles to a process using OpenProcess(...) twice or thrice.

    HMODULE you get from GetModuleHandle(NULL,..) is in fact the load address
    of the
    image. If I remember right, the hInstance that comes in the winmain willbe
    the same as
    the GetModuleHandle(NULL,...)

    --
    Vipin Aravind

    "Scherbina Vladimir" wrote in message
    news:OfV6iyoKGHA.2696@TK2MSFTNGP14.phx.gbl...
    > Btw, just noticed that my MSDN (April 2003) states that GetProcessId has
    > the following prototype:
    >
    > void GetProcessId(
    > HANDLE Process
    > );
    >
    >
    >
    > --
    > Vladimir
    >
    > "Mark Randall" wrote in message
    > news:u1RENhnKGHA.3492@TK2MSFTNGP09.phx.gbl...
    >> H is a handle; kind of like an internal pointer to special data
    >> in the system that you cannot access directly.
    >>
    >> It can be used via OpenProcess, and get the process ID.
    >>
    >>
    >> wrote in message
    >> news:1139159482.468622.38340@g44g2000cwa.googlegro ups.com...
    >>> Hello Alll,
    >>> I am learning windows programming and getting confused more and more..
    >>> here is my latest confusion...
    >>> What is the relation between processId (which we come to know from the
    >>> task manager) and hInstance (which is one of the parameter passed in
    >>> the winmain function) of a program?
    >>>
    >>> Or there isn't any relation at all? if yes..why? as both give us the
    >>> unique number related to a running program..
    >>> Thanks and Regards,
    >>> Yogesh Joshi
    >>>

    >>
    >>

    >
    >




  8. Re: process Id and HINSTANCE...what is the relation betweeen them?

    GetProcessId was issued as part of XP SP1, so it has been round for quite a
    long time.

    It works by calling NtQueryInformation querying ProcessBasicInformation. The
    fifth element of the PROCESS_BASIC_INFORMATION structure is the unique
    process id.

    Carly

    "James Brown" wrote in message
    news:gZKdneVMJbtt_nveRVnyiA@pipex.net...
    > This must be a new API because it has not existed up til now...
    > also note the distinction between a HANDLE to a process - which
    > is a user-mode HANDLE to a kernel PROCESS object, and
    > a HINSTANCE which is not a handle at all - rather it is a pointer
    > in thin disguise which points to the base address of the process/DLL -
    > synonymous with a HMODULE. These last two "handles" are really
    > just a hang-up from win16 days.
    >
    > James
    >
    > --
    > Microsoft MVP - Windows SDK
    > www.catch22.net
    > Free Win32 Source and Tutorials
    >
    >
    > "Scherbina Vladimir" wrote in message
    > news:OfV6iyoKGHA.2696@TK2MSFTNGP14.phx.gbl...
    >> Btw, just noticed that my MSDN (April 2003) states that GetProcessId has
    >> the following prototype:
    >>
    >> void GetProcessId(
    >> HANDLE Process
    >> );
    >>
    >>
    >>
    >> --
    >> Vladimir
    >>
    >> "Mark Randall" wrote in message
    >> news:u1RENhnKGHA.3492@TK2MSFTNGP09.phx.gbl...
    >>> H is a handle; kind of like an internal pointer to special
    >>> data in the system that you cannot access directly.
    >>>
    >>> It can be used via OpenProcess, and get the process ID.
    >>>
    >>>
    >>> wrote in message
    >>> news:1139159482.468622.38340@g44g2000cwa.googlegro ups.com...
    >>>> Hello Alll,
    >>>> I am learning windows programming and getting confused more and more..
    >>>> here is my latest confusion...
    >>>> What is the relation between processId (which we come to know from the
    >>>> task manager) and hInstance (which is one of the parameter passed in
    >>>> the winmain function) of a program?
    >>>>
    >>>> Or there isn't any relation at all? if yes..why? as both give us the
    >>>> unique number related to a running program..
    >>>> Thanks and Regards,
    >>>> Yogesh Joshi
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >




  9. Re: process Id and HINSTANCE...what is the relation betweeen them?

    yogpjosh@gmail.com wrote:
    | Hello Alll,
    | I am learning windows programming and getting confused more and more..
    | here is my latest confusion...
    | What is the relation between processId (which we come to know from the
    | task manager) and hInstance (which is one of the parameter passed in
    | the winmain function) of a program?
    |
    | Or there isn't any relation at all? if yes..why? as both give us the
    | unique number related to a running program..

    To summarize:
    HINSTANCE = HMODULE is the "module handle". Although a handle, generally,
    is an opaque thingo, in this case it physically represents the address
    in process's address space where a module (.exe or .dll) is loaded. (Not
    that you should rely on that piece of data). An .exe always has a
    hinstance of 0x00400000 in Win32, and dll's have different ones. It is
    useful in e.g. resource-handling functions -- basically, e.g.

    LoadBitmap(hInstance, IDB_MYBITMAP)

    says: "Take a look in the module(image) starting at hInstance, and find
    me the bitmap embedded somewhere in there".
    HINSTANCEs/HMODULEs are, thus, process private, and you can't "peek"
    into another's process's virtual address space and find one (nor it
    would be useful even if you could) -- both e.g. explorer.exe and
    winword.exe are loaded at the same 0x00400000 *from their point of view*,
    but they can't "peek" into each other (unless with "heavy artilery" like
    ReadProcessMemory).

    DWORD processid is an "unique ID" of a process on the system. You can
    do little with that piece of data per se, except...

    ....HANDLE hProcess is a handle to a process which gives you control over
    (in general, another) a process. You obtain it by OpenProcess(...ProcessId)
    and with that handle you can TerminateProcess, GetExitCodeProcess or
    even ReadProcessMemory or WriteProcessMemory, provided you have sufficient
    access rights. [Disclaimer: Don't do that while you're still a beginner].

    --
    Jugoslav
    ___________
    www.xeffort.com

    Please reply to the newsgroup.
    You can find my real e-mail on my home page above.

  10. Re: process Id and HINSTANCE...what is the relation betweeen them?

    Jugoslav Dujic wrote:
    > yogpjosh@gmail.com wrote:
    > | Hello Alll,
    > | I am learning windows programming and getting confused more and more..
    > | here is my latest confusion...
    > | What is the relation between processId (which we come to know from the
    > | task manager) and hInstance (which is one of the parameter passed in
    > | the winmain function) of a program?
    > |
    > | Or there isn't any relation at all? if yes..why? as both give us the
    > | unique number related to a running program..
    >
    > To summarize:
    > HINSTANCE = HMODULE is the "module handle". Although a handle, generally,
    > is an opaque thingo, in this case it physically represents the address
    > in process's address space where a module (.exe or .dll) is loaded. (Not
    > that you should rely on that piece of data).


    According to this article by Raymond Chen you can rely on it:

    http://blogs.msdn.com/oldnewthing/ar...25/247180.aspx

    Matt

  11. Re: process Id and HINSTANCE...what is the relation betweeen them?

    wrote in message
    news:1139159482.468622.38340@g44g2000cwa.googlegro ups.com...
    > Hello Alll,
    > I am learning windows programming and getting confused more and more..
    > here is my latest confusion...
    > What is the relation between processId (which we come to know from the
    > task manager) and hInstance (which is one of the parameter passed in
    > the winmain function) of a program?
    >
    > Or there isn't any relation at all? if yes..why? as both give us the
    > unique number related to a running program..


    The relation is: the HINSTANCE, like all handles, is an indirection to the
    process, valid within the running application. The process ID is a globally
    unique identifier. You can switch between them using:
    DWORD GetProcessId(HANDLE Process);
    HANDLE OpenProcess(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD
    dwProcessId);


    > Thanks and Regards,
    > Yogesh Joshi
    >




  12. Re: process Id and HINSTANCE...what is the relation betweeen them?

    > The relation is: the HINSTANCE, like all handles, is an indirection to the
    > process, valid within the running application.


    The HINSTANCE is the load address of module, be it .exe, .dll and so on. It
    has nothing to do with the process apart from the fact that the module is
    loaded in the process address space. Processes do need to have modules
    loaded in to the address space, the section object for the executable
    (Notepad.exe for example) is an argument to
    NtCreateProcess/NtCreateProcessEx and both it and Ntdll.dll are mapped into
    the process address space during process creation.

    The HINSTANCE is not a handle either, not strictly speaking anyway. It is
    the base address for the mapped view of a section object. Handles to section
    objects do exist in the handle table for the process and for executable
    images. The load address and section object are linked in that the section
    object describes executable mapping options, the preferred load address
    being just one of them.

    Carly

    The process ID is a globally
    > unique identifier. You can switch between them using:
    > DWORD GetProcessId(HANDLE Process);
    > HANDLE OpenProcess(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD
    > dwProcessId);
    >
    >
    >> Thanks and Regards,
    >> Yogesh Joshi
    >>

    >
    >




+ Reply to Thread