View Full Version : How to get hWnd?
4oh4
June 26th, 2001, 11:52
I'm trying to insert some additional code into a program, but need to pass the hWnd to one of my api calls. The problem is that the app is written in vc++ and is so overbloated.
What's the quickest way to get the hWnd so I can finish inserting my code?
Kayaker
June 26th, 2001, 13:08
How about GetActiveWindow?
The GetActiveWindow function retrieves the window handle to the active window associated with the thread that calls the function.
Or tap into the main CreateWindow call, which returns the hWnd, and store it for later use.
If it's a particular hWnd you need maybe you can retrieve it with some other API such as GetTopWindow or GetNextWindow once you've got the parent hWnd.
Kayaker
4oh4
June 26th, 2001, 15:59
Great!
That worked like a charm on the small test program I wrote. Now I'll just cross my fingers and hope it's that easy on the real app.
Thanks Kayaker
4oh4
June 28th, 2001, 12:43
Well this is irritating...
The app starts out by programmatically disabling several menu items and enabling them at specific times. For example the "copy" and "paste" menu items are enabled only when the clipboard has text on it. So before I can even add my functionality code, I've got to programmatically enable my added menu items.
This code (for one menu item) is as follows:
jmp 00502830 ;jmps around my added code
pushad ;for some reason my popad at the end seems to cause a gpf so I'm just pop'ing edi,esi,ebp, and ebx at the end
call GetActiveWindow
push eax ;hWnd of active window
call GetMenu
push 0 ;MF_ENABLE
push 00000FC9 ;resource id
push eax ;hMenu
call EnableMenuItem
pop edi
pop esi
pop ebp
pop ebx
jmp 0041262a ;back to the original code
The return values from each of the calls are currect and the return from EnableMenuItem is 03 which is the previous state of the menu item (MF_GRAYED | MF_DISABLED). The api help states that if the return value is 0xffffffff that the menu item didn't exist so I know that I've got it right. As a side note, the api help also states that MF_GRAYED disables AND grays the menu so I don't know why someone would pass both flags.
Anyways, generically speaking.... Can anyone think of why my menu item isn't enabled after this apparently successful code is run? This code technique is working perfectly in my test programs.
Kayaker
June 28th, 2001, 20:50
Hi 4oh4
Is it possible there's a call to some other menu function after your code is called which updates the menu to its default, such as LoadMenu, SetMenu or possibly several others? You might still be placing your patch too early in the programs execution. Try running APISpy32 on it with all of the 'menu' API's selected, then click on your menus, copy something to clipboard, whatever else affects the menus, and see if there are any other API calls that you have to deal with first.
Kayaker
4oh4
June 28th, 2001, 22:26
I'm pretty sure not, as I inserted my code well into the app. ....after the app is good 'n loaded my stuff runs. A quick check shows that SetMenu (which I didn't even know about...sheesh!) is called 4 times. That's could be my problem. I'll check it, as I don't know what else to try. At least that makes sense though.
Damn I hate vc++. The code looks like garbled crap even. You know what I saw? An "xor eax, eax" followed a few lines(which didn't alter eax at all) with a "push eax". Wouldn't a "push 0" be better?
God, if you're up there, I promise if I make it through this rce project alright I'll go to church every sunday and quit smoking and drinking....and all the prostitutes......and the crack too......
Powered by vBulletin® Version 4.2.2 Copyright © 2018 vBulletin Solutions, Inc. All rights reserved.