# Finding Process Name using Window Handle

**URL:** <https://www.ruby-forum.com/t/finding-process-name-using-window-handle/162422>\
**Category:** Ruby\
**Created:** [March 9, 2009, 9:06pm UTC](https://www.ruby-forum.com/t/finding-process-name-using-window-handle/162422 "2009-03-09T21:06:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anukulsinghal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@anukulsinghal](https://www.ruby-forum.com/u/anukulsinghal)\
**Post date:** [March 9, 2009, 9:06pm UTC](https://www.ruby-forum.com/t/finding-process-name-using-window-handle/162422/1 "2009-03-09T21:06:54Z")

</div>

Hi,

In my project, I have a requirement to work with any client application  
which I need to kill (if an instance exists) before invoking a fresh  
instance. Currently, I have hardcoded the name of the process to  
terminate a client before opening it. What I am looking for is a way to  
find the process name (using any Win32API or using a Window Handle). I  
found out how to get a Window Handle for the client application that is  
invoked, is that something I can use to get the process name, or any  
other way? Please see the code I am using:

```
 def killApp

```

wmi = WIN32OLE.connect(“winmgmts://”)  
processes = wmi.ExecQuery(“select \* from win32\_process”)  
for process in processes do  
if process.Name.include? “AppName.exe” then  
process.Terminate  
end  
end  
end

Would really appreciate your help.

Thanks,  
Anukul

---

<div class="post-metadata">

**Author:** ![anukulsinghal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@anukulsinghal](https://www.ruby-forum.com/u/anukulsinghal)\
**Post date:** [March 10, 2009, 4:26am UTC](https://www.ruby-forum.com/t/finding-process-name-using-window-handle/162422/2 "2009-03-10T04:26:05Z")

</div>

Ted Baker wrote:

> Hi all,
> 
> I have successfully managed to implement the following apis
> 
> findWindow=Win32API.new(“user32.dll”, “FindWindow”, [‘P’,‘P’], ‘N’)  
> sendMessage=Win32API.new(“user32.dll”,  
> “SendMessage”,[‘N’,‘N’,‘N’,‘P’],  
> ‘N’)  
> getMenu=Win32API.new(“user32.dll”, “GetMenu”, [‘N’], ‘N’)  
> getMenuItemCount=Win32API.new(“user32.dll”, “GetMenuItemCount”,[‘N’],  
> ‘N’)  
> getSubMenu=Win32API.new(“user32.dll”, “GetSubMenu”, [‘N’,‘N’], ‘N’)  
> deleteMenu=Win32API.new(“user32.dll”, “DeleteMenu”,  
> [‘N’,‘N’,‘N’,],‘N’)  
> drawMenuBar=Win32API.new(“user32.dll”, “DrawMenuBar”, [‘N’], ‘N’)  
> setWindowLong=Win32API.new(“user32.dll”,  
> “SetWindowLong”,[‘N’,‘N’,‘N’],  
> ‘N’)
> 
> but am unable to get getMenuItemInfo to work, this is because it  
> takes a  
> type structure and boolean as parameters and i can’t figure it out…
> 
> this is what i have
> 
> getMenuItemInfo =Win32API.new(“user32.dll”, “GetMenuItemInfo”,  
> [‘L’,‘L’,‘NEED’,‘NEED’], ‘L’)
> 
> the api is as follows…
> 
> BOOL GetMenuItemInfo(  
> HMENU hMenu,  
> UINT uItem,  
> BOOL fByPosition,  
> LPMENUITEMINFO lpmii  
> );
> 
> typedef struct tagMENUITEMINFO {  
> UINT cbSize;  
> UINT fMask;  
> UINT fType;  
> UINT fState;  
> UINT wID;  
> HMENU hSubMenu;  
> HBITMAP hbmpChecked;  
> HBITMAP hbmpUnchecked;  
> ULONG\_PTR dwItemData;  
> LPTSTR dwTypeData;  
> UINT cch;  
> HBITMAP hbmpItem;  
> } MENUITEMINFO, \*LPMENUITEMINFO;
> 
> any help greatly appreciated…
> 
> Thanks
> 
> Ted

Very late reply.

You should have been directed towards Ruby/DL in the  
standard library.

This is now preferred to Win32API which is in the process  
of being obsoleted.

[http://www.jbrowse.com/text/rdl\_en.html](http://www.jbrowse.com/text/rdl_en.html)

daz

---

<div class="post-metadata">

**Author:** ![anukulsinghal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@anukulsinghal](https://www.ruby-forum.com/u/anukulsinghal)\
**Post date:** [March 10, 2009, 4:31am UTC](https://www.ruby-forum.com/t/finding-process-name-using-window-handle/162422/3 "2009-03-10T04:31:56Z")

</div>

> > > Oops, that was to the wrong thread.

Anukul S. wrote:

> Hi,
> 
> In my project, I have a requirement to work with any client  
> application  
> which I need to kill (if an instance exists) before invoking a fresh  
> instance. Currently, I have hardcoded the name of the process to  
> terminate a client before opening it. What I am looking for is a way  
> to  
> find the process name (using any Win32API or using a Window Handle).  
> I  
> found out how to get a Window Handle for the client application that  
> is  
> invoked, is that something I can use to get the process name, or any  
> other way? Please see the code I am using:
> 
> ```
> def killApp
> 
> ```
> 
> wmi = WIN32OLE.connect(“winmgmts://”)  
> processes = wmi.ExecQuery(“select \* from win32\_process”)  
> for process in processes do  
> if process.Name.include? “AppName.exe” then  
> process.Terminate  
> end  
> end  
> end
> 
> Would really appreciate your help.
> 
> Thanks,  
> Anukul

Hi,

I only looked briefly at this but you might find some clues  
from solutions in other languages and be able to convert them  
yourself.

[http://forums.networkautomation.com/forum/messageview.cfm?FTVAR\_FORUMVIEWTMP=Threaded&catid=4&threadid=2515](http://forums.networkautomation.com/forum/messageview.cfm?FTVAR_FORUMVIEWTMP=Threaded&catid=4&threadid=2515)

[http://www.purebasic.fr/english/viewtopic.php?p=264755&highlight=&sid=d3f90592c990c78a0c1039efe8e1167e](http://www.purebasic.fr/english/viewtopic.php?p=264755&highlight=&sid=d3f90592c990c78a0c1039efe8e1167e)

I hope this is more helpful than getting no reply 🙂

daz
