?
Solved

findwindow to exe file

Posted on 1998-04-01
14
Medium Priority
?
474 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
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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 …
If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

649 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