?
Solved

findwindow to exe file

Posted on 1998-04-01
14
Medium Priority
?
473 Views
Last Modified: 2013-12-03
If I use findwindow to get HWND, how can I get the
exe file that created the window, i.e. getmodulename ?
but that i COULD NOT not work it.
0
Comment
Question by:has
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
14 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 1412463
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1412464
Use GetWindowLong() with an index of GWL_HINSTANCE.  (I had a longer and more complex solution in mind (and mostly typed in) when I discovered this.)
0
 
LVL 22

Expert Comment

by:nietod
ID: 1412465
You would use it like

char FilNam[256[;
HINSTANCE Instance = (HINSTANCE) GetWindowLong(WndHnd,GWL_HINSTANCE);
GetModuleFileName(Instance,FilNam,255);
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

Author Comment

by:has
ID: 1412466
     HWND hWnd;
      HMODULE Instance;
      char name[MAX_PATH];
      hWnd = ::FindWindow(NULL, "mywindow");
      Instance = (HMODULE) GetWindowLong(hWnd,GWL_HINSTANCE);
      GetModuleFileName(Instance,name,MAX_PATH);
      MoveWindow(hWnd, 0, 0, 400, 200, TRUE);

above, mywindow moves(so hWnd if correct !) but
name comes as the name of current calling program, not
"mywindow" program ? any idea ?

0
 

Author Comment

by:has
ID: 1412467
     HWND hWnd;
      HMODULE Instance;
      char name[MAX_PATH];
      hWnd = ::FindWindow(NULL, "mywindow");
      Instance = (HMODULE) GetWindowLong(hWnd,GWL_HINSTANCE);
      GetModuleFileName(Instance,name,MAX_PATH);
      MoveWindow(hWnd, 0, 0, 400, 200, TRUE);

above, mywindow moves(so hWnd if correct !) but
name comes as the name of current calling program, not
"mywindow" program ? any idea ?

0
 

Author Comment

by:has
ID: 1412468
     hWnd = ::FindWindow(NULL, "mywindow");
      Instance = (HMODULE) GetWindowLong(hWnd,GWL_HINSTANCE);
      GetModuleFileName(Instance,name,MAX_PATH);
      MoveWindow(hWnd, 0, 0, 400, 200, TRUE);

above, mywindow moves(so hWnd if correct !) but
name comes as the name of current calling program, not
"mywindow" program ? any idea ?

0
 

Author Comment

by:has
ID: 1412469
instance value is 0x00400000, that yields the name for the
calling program.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1412470
You could try to get the instance handle from the Window's class inforamtion.  But I suspect that will fail to.  You wuld use GetClassLong() with GCL_HMODULE.  Unfortunately, I think that will usually return 00400000 too.

The problem is that instance handles are almost always 00400000.  They can be used within a process, but not between processes.  I need to think about this.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1412471
I suspect you can use GetWindowThreadProcessID() to do what you want.  But i can't figure out how to get a module handle from a process ID.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1412472
Got it.  Use GetWindowThreadProcessID() to get the process's ID.  Then use OpenProcess() to get the instance handle.  Give it a try and let me know if it works.
0
 

Author Comment

by:has
ID: 1412473
yes worked, thanks
0
 

Author Comment

by:has
ID: 1412474
I am sorry, I made a mistake, it fails also

      HWND hWnd;
      char name[MAX_PATH];
      hWnd = FindWindow(NULL, "mywindow");

      GetWindowText(hWnd, name, MAX_PATH); //yes verifies
      DWORD aa;
      aa = GetWindowThreadProcessId(hWnd, NULL);
      HMODULE module;
      module =  OpenProcess(PROCESS_ALL_ACCESS, FALSE, aa);
        GetModuleFileName(module,name,MAX_PATH);
      CloseHandle(module);
      MessageBox(NULL,name,"name:",MB_OK);

gives the path name of calling exe file, any more idea ?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1412475
I would have thought that was right.

Can you do a test?  Check to see if the process ID returned is different for different processes.  If so, check to see if the module handle is different for different processes.  Let me know what you find.

Unfortunately, I might not be able to help you for two reasons.  First, although this seemed easy at first, it seems to be pushing my knowledge.

Second, I'm leaving tommorrow morning and will be gone until Tusday the 7th.  

If we can't get this solved tonight, post a question in Customer Service for Linda saying you graded this question too early and ask her to reject it for you so other experts can help you.  Let her know that I (that is, the expert who got the points) suggested this and won't complain.  Be sure to give her the question ID.
0
 

Author Comment

by:has
ID: 1412476
dont worry about it, I may post a new one later again.
thanks for the try.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question