Solved

findwindow to exe file

Posted on 1998-04-01
14
470 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 50 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

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.  …
This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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