Help Understanding hook MessageBoxA Positioning with API WindowsHook “User32” dll stuff
Hi
I would like to try and understand as much as possible a code which I may need to use and adapt
I have tried very hard to find some explanations that I can understand, but I am hitting a brick wall with this one every time. I either cannot find any information, or, the sparse info. that I have found is totally beyond my ability to understand it.
The code, ( here: https://pastebin.com/0Q2DKn5T
https://www.excelforum.com/developme...ml#post4830947 ) , I have Pieced together from various sources and/ or have made up from a lot of trial and error experiments. The section I call Rem 1 I understand after a bit of recent Forum participation. ( That is all about a “pseudo non modal MsgBox”. I have that sussed – No problem there )
Rem 2 and Rem 3 and Rem 4 parts I have been struggling on now for a week. My brain has become comfortably numb
I hate working blind and not having a clue what I am doing. I think the code and variations of it could be very useful to me, but I am very reluctant to use it until I can get some understanding of.
Can anyone put me put of my misery. I have a fairly good basic Excel VBA Knowledge but apart from that I know nothing about computers and programming. There are things going on in this code that are not to be seen by mortals and I need a real computer Expert to help spread some light
Could anyone explain what is going on in this code , or rather how it is working. If you could do that in as close to Layman’s terms that you can give then I would be very grateful..
( I have just started reading a 1500 page book on this that may get me somewhere on this in a few years.. , but any help now could probably save me a lot of time).
The code is here:
https://www.excelforum.com/developme...ml#post4830947
and here:
https://pastebin.com/0Q2DKn5T
and in code module “RapeAHook” in the uploaded file
It works brilliantly: ( I tried it on Excel 2003, 2007 and 2010 on three different computers )
This is what it does with my best up until now explanation of how..
The main point of the code is to get up a Pop up Message Box where I want it using the “Windows API” route. This Pop up box I can set to a position at will and also still select the spreadsheet whilst it is up. The Message Box is pseudo non modal, that is to say, unlike the conventional VBA MsgBox Function, it does not “wait” for me to hit OK. It works great just as I want, but I am bugged if I can work out how.
( Rem 1 This is the message Box stuff that I am happy with. I have the APIssinUserDLL_MsgBox , and the related code lines near that, well sussed, - no problem there ) .
Rem 2 Various Declare lines make the Windows API codes available to the main code.
( The main code is Sub AkaApiApplicationPromptToRangeInputBox and there is also a single VBA Function code, Private Function HoldRapeAHookPro )
Rem 3 This is where the main stuff starts , and at the outset globial variables are filled for the left and top position of/ for use in my pop up box. Those get used in some unconventional way and exactly when and how is part of the whole mystery to me… I expect they have to be Globial. I don’t know.
I am guessing that a code line I have in the main Sub AkaApiApplicationPromptToRangeInputBox of this form = SetWindowsHookExA(__ arguments, , is getting some identifying number for a position on a chain of events. ( I am guessing that when the chain is pulled it waggles and all hell sets loose internally). In its __ arguments, , it does some AddressOf thingy and I can only guess that it somehow makes a note of an address of my VBA Function code, Private Function HoldRapeAHookPro in such a way that when this chain of events is at the said noted reference point then that triggers off the VBA Function code, Private Function HoldRapeAHookPro
How on earth it then catches my fairly conventional API Message Box procedure call APIssinUserDLL_MsgBox before it brings up that message box and pauses it while it starts that VBA Function code, Private Function HoldRapeAHookPro is a mystery to me
But that does happen, that is to say the code Sub AkaApiApplicationPromptToRangeInputBox pauses in the code line APIssinUserDLL_MsgBox . Then at that point, seemingly magically , up springs the Function code, Private Function HoldRapeAHookPro
Function code, Private Function HoldRapeAHookPro then appears not to do much other than returning the value of _ 0 _ ( HoldRapeAHookPro = 0 ) . It does that 5 times. That is to say, after it has finished it starts magically again, 4 times, giving 5 of these runs where it don’t do much.
After those first 5 not very eventful runs…..It starts a 6th time. As previously it has started possibly due to the sprinkling of Fairy dust.
This time it appears to do a bit more by virtue of a specific condition being met. This condition is If on the first argument in the function, ( If lMsg = 5. God knows what and where that parameter magically comes from. There is no obvious way to be seen where that parameter is filled. In fact it is a mystery how any of those arguments in the signature line of Private Function HoldRapeAHookPro are filled.
Any road up, .. at this point we are on the 6th run of the Function. But it has never been called in the conventional way. Consequently no variables have been seen to be passed to it as far as mortal can see. But they are there anyway. Because the If condition was met on the 6th magically started run, a code line is done of the form
SetWindowPos wParam, 0, poX, pussY, 400, 150, 4
That code line almost looks sensible and understandable and is probably mainly “setting a windows position” based on some co ordinates and size dimensions in its arguments.
What however, is far from sensible is that that code line on running actually sets of the Function again. !!! As we almost programmers in the trade say, it “calls” the function again , or it “calls itself” or it does “recursion wonks”. Assuming that this is following the normal VBA recursion routine way of doing things then that means that a fresh copy of the Function is now started whilst the original “calling” Function copy ( the 6th run of the original first copy ) pauses .
That happens a further 29 times. ( That makes 30 copies in total of the Function running one after the other ).
Eventually then after the 30th Function copy run , that copy of the Function ends followed sequentially by the other 29 ending. Just before they end two code lines are done:_..
_.. another Windows API "user32" thingy , UnhookWindowsHookEx
and the same code line that ways also done by the initial 5 code runs of the function in not recurring runs
_.. HoldRapeAHookPro = 0
After that crazy set of operations are finished then the main code un pauses with the message box coming up.
It is doing exactly what I want… but how????
I put some code lines on to help see what is going on and have pseudo dumped a Log, obtained via Debug.Print in a few strategic places, ( Here a runny log dump copy: https://www.excelforum.com/developme...ml#post4829796 )
I don’t know what the stuff in that log means, but maybe it is of significance to someone who knows what is going on in this code.
_.....
So, I can’t really make head or tail of the damn thing. There are some really weird things going on when you step through it in Debug F8 mode.
The code works well. It looks a mess as I am not sure how to organise it in a way that well suits how it is working and what exactly it is doing as I don’t really know what and how it is working.- so, once again…
The main code bits that most concern me:
'_-===== 4 main Declaration things I do not fully understand.
'_-=====Weird thing with an AddressOf in it. I have read loads on this, but I do not understand a word and you get sent off on a never ending path of links trying to get to the bottom of what it all means.
I am fairly happy with the _ APIssinUserDLL_MsgBox _ code line on its own. That works similarly to a VBA MsgBox. But I do not understand how it works in conjunction with the crazy Function HoldRapeAHookPro(
'_-====
Function HoldRapeAHookPro(
That is weird:.. as you step through the code line _ APIssinUserDLL_MsgBox _ then _ Function HoldRapeAHookPro( _ starts somehow on its own. An If condition ( If lMsg = 5 Then ) is not met which results In the _ Function HoldRapeAHookPro _ being set to 0 ( HoldRapeAHookPro = 0 ) … The function starts again after it finishes … it does that 4 times in total doing the same, so then in effect a total of 5 times.
It then starts again a 6th time, but this time the If condition is met. As a result an Windows API "user32" thingy, SetWindowPos , starts … and this is weird… that somehow sets the _ Function HoldRapeAHookPro( _ off again, effectively in a recursion process starting another copy run of the _ Function HoldRapeAHookPro( _ . That happens another 29 times making in total 30 separate runs of separate copies of the Function with the If condition satisfied. After the 30th Function copy run , that copy of the Function ends followed sequentially by all the other 29 function copies ending. Just before they end, two code lines are done:_..
_.. another Windows API "user32" thingy , UnhookWindowsHookEx
and the same code line that was also done by the initial 5 code runs of the function runs when not recurring
_.. HoldRapeAHookPro = 0
Once that strange stuff is finished the Message box, ( APIssinUserDLL_MsgBox ), comes up as I want it.
Can anyone give me some easy to understand explanations of what is going on.
Many Thanks
Alan
refs:
Mystery to me is also.. http://listenonrepeat.com/watch/?v=iFZzN5qIiUs .. how she looks so young.
1500 page Book: “Visual Basic Programmer’s Guide to the Win32 API” by Dan Appleman
http://listenonrepeat.com/watch/?v=_...mfortably_numb
http://www.vbforums.com/showthread.p...3-but-not-2007
https://www.techrepublic.com/blog/10...-applications/
http://www.eileenslounge.com/viewtop...=28885#p223629
https://stackoverflow.com/questions/...excel-vba-why/
http://www.mrexcel.com/forum/excel-q...ox-method.html
http://www.excely.com/excel-vba/defi...ssagebox.shtml
Bookmarks