[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

findwindow to exe file

Posted on 1998-04-01
14
Medium Priority
?
476 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
  • 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
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

831 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