Solved

findwindow to exe file

Posted on 1998-04-01
14
463 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 50 total points
Comment Utility
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
You would use it like

char FilNam[256[;
HINSTANCE Instance = (HINSTANCE) GetWindowLong(WndHnd,GWL_HINSTANCE);
GetModuleFileName(Instance,FilNam,255);
0
 

Author Comment

by:has
Comment Utility
     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
Comment Utility
     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
Comment Utility
     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
Comment Utility
instance value is 0x00400000, that yields the name for the
calling program.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
yes worked, thanks
0
 

Author Comment

by:has
Comment Utility
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
Comment Utility
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
Comment Utility
dont worry about it, I may post a new one later again.
thanks for the try.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now