Cool new VFP9 feature: the RegWinProc function (by John Koziol)

(Disclaimer:  the following is in process and subject to change. The author, nor Microsoft, makes no guarantees nor warrantys on the content of this post)

One of the features we intended to put into VFP 9 was AppState, a _SCREEN property that would allow you to trap when a VFP application got or lost focus in regards to other Windows apps. All you needed to do was setup some code and a BINDEVENT and you could trap it.

Now this was something asked for by quite a lot of customers and seemed like a great idea. However, we were having problems with it as there were double the number of Windows focus messages flying around under certain circumstances. This made testing difficult and since there was no way to further granularize what was actually happening, it would make for a problematic implementation in line-of-business customer applications.

So Calvin took to exploring the whole Windows messaging concept and has come up with something even better:  The RegWinProc function. 

What does it do? Well…here’s the early upshot direct from Calvin’s email:


hWnd is the hWnd value from _VFP or _SCREEN or a VFP Form. nWinMessage is the Windows message, a numeric value in a hexadecimal representation in MSDN documentation; ie, 0x0201 is WM_LBUTTONDOWN or left mouse button down. cProcedure is the UDF to call for the specified window and message.

So what is gained? Pretty much the entire Windows messaging world.  Here’s some mini-use cases:

  • You need to trap a shutdown or restart to guarantee that all tables and indexes are closed cleanly in your application. You can trap WM_POWERBROADCAST for the _VFP hWnd and send it to a UDF that performs the closings.

  • You want to know a disk has been inserted or ejected from a drive.  No problem; that message is trapable.  Boy, I remember the days when the application asked users to insert a disk into drive A: and press Enter.  You then prayed that they had and proceeded anyway with little ability to check.

  • You have a sophisticated Tablet PC application that uses an offline view to an enterprise data source. You can now add the capability to resynch the view when the application senses that the network is available using RegWinProc; Windows detects when network connections are gained and lost and now you can too.

  • You can now detect when Plug-and-Play media has been added or removed. Great for applications reading or writing to a USB drive.

  • Last (but not finally), you can stop an event dead in it’s tracks with the BROADCAST_DENY_QUERY nWinMessage.  Trap this baby when there are things you don’t want to happen while your application is running; for example, your app and data is on removable media and you do not want that media ejected while the application is running.

My understanding is that there are lots of .Net samples for this type of code and I’m sure we’ll come up with some and you guys will come up with a lot…which I’m hoping you upload copiously to web forums and other net watering holes to share your insights. I’m sure this will be fertile ground for article writers as well.

Final word:  There’s a movie quote rattling around in my head, “With great power comes great responsibility”.  Ken would know who said it and when <g>. My point is, with this new function you have a lot of control of what is going on in Windows external to your VFP application. It’s quite possible, if used poorly, that you could crash your application or other processes on your computer. This is not something new to RegWinProc – you can do it now with DECLARE – DLL and a misuse of the API.

Final (I mean it this time), final word: This new function was just approved and did not make it to the public beta build. However, if we refresh the beta later on you’ll likely see it. I’ll post a followup if and when that occurs.

Comments (14)

  1. wOOdy says:

    hey, it’s nice to see one of my longstanding ERs finally getting reinvented… Seems just another case of that magic ER-rule "If sender-name = "woody" then delete" 😉

  2. I’m impressed! Can’t thank you enough! In fact one of my new apps under development, to be delivered at the end of 2004, hopefully sometime after VFP9 goes live, will require an online/offline connectivity detection scheme as it will be used in laptops on the field. It will greatly benefit from a seamless, more "natural" connectivity check. That same app also needs to detect a diskette. Now I can get rid of my old-cluncky method to see if a disk was (maybe) inserted.

    Thank you MS. You guys in the VFP Team rock!

  3. Rick Strahl says:

    This is very cool. Is this going to be hooked to all top level windows or just the VFP desktop?

  4. John Koziol says:

    In the sample code Calvin worked up, _VFP and _SCREEN were referenced so I’m assuming that this will apply to all top level forms. Just an educated guess, though.

  5. Really cool!

    Another useful use cases:

    – Hooking ActiveSynch to get synchronize Mobile DBs and apps.

    – Launching processes during Screen-saver time (house-keeping stuff).

    – Sensing wireless/net connection to send/receive data.

    – Detecting arriving email?

    Who knows! We will have to search the docs to see all the things we can do with this.

    Thanks a lot!

  6. Spider-man’s uncle Ben, says to him: "With great power, comes great responsibility."

  7. Rob Spencer says:

    Now if only you could reconnect a broken file handle in your error handler, this would make for some really robust systems! You can currently trap a 1104 error (caused by pulling out your network cable for instance) and with RegWinProc I assume we could detect when the network came back online again but nothing that I can find will reset the connection. If you know what table it is, you should be able to close and reopen it, but you can’t do that in an error handler (what do you do if your in the middle of an SQL query for instance). "With great power, comes a whole new list of enhancement requests." 😉

  8. John Koziol says:

    Hey Rob, sorry but you’re right – can’t mend a broken file handle. But at least you’ll be able to catch the break and attempt to mitigate.

  9. Paul James says:

    "Make it so Number 1" this would be very very useful! Keep up the great work!

  10. Malcolm Greene says:

    Wow! This tops even the new reportwriter features! Absolutely awesome!

  11. Neil Tonkin says:

    Great feature, now we can use RegisterWindowMessage() to communicate between VFP applications or grab the MessageBox window and change the appearance of the child controls (prompts, icon etc).

  12. Nick Causton says:

    Excellent function but will it work with non-visual COM servers??

  13. John Koziol says:

    Well, now it appears that it will be enhanced functionality with the existing BINDEVENTS function. Stay tuned.

  14. Franz Müller says:

    Excellent new feature of VFP9. Does anybody know, if VFP9 supports callback-functions (e.g. RasDialFunc) too?