Wednesday, November 23, 2005

BindEvent() and Compile

Recently I have been working on a Fox IDE tool called FoxTabs. FoxTabs utilises my favourite feature of VFP 9.0, binding to Windows message events. The design approach is to hook into window events within the Fox IDE and manage multiple editing windows via tab controls on a toolbar. We, Craig and I, have just posted a beta version for all to check out. However there is one issue preventing me from getting some well earned sleep. (issue posted by Alan Stevens on the forum)

When running FoxTabs, if an error occurs during compiling your source code, VFP crashes. I admit, its only a minor issue. I mean, who writes code with bugs in it right? :-P

So what's happening? We have event bindings to all WM_CREATE events received by Fox. When a window is created, we interrogate its properties to see if it is an IDE window we want to manage via a toolbar tab. When the compile error occurs, Fox will create a dialog box to display the error. It is when we process the WM_CREATE event of this dialog box we get the fatal crash.

Here is some simplified code to demonstrate this:

* Window messaging constants

#define GWL_WNDPROC(-4)

#define WM_CREATE0x0001

#define WM_DESTROY0x0002

#define WM_SETTEXT0x000C

* Create an instance of the wnHandler class

Public wmHandler As wmHandler

wmHandler = NewObject("wmHandler")

* Bind to all window create notifications

BindEvent(0, WM_CREATE, wmHandler, "wmEventHandler")

Return

* Define Windows messages event handler class

Define Class wmHandler As Custom

PrevWndFunc= 0

IgnoreEvents= .F.

Function wmEventHandler(hWnd As Integer, Msg As Integer, wParam As Integer, lParam As Integer)

Copy the code into a PRG and run. Open some IDE editing windows and observe the text outputted to the desktop. To reproduce the crash, open a new PRG and enter some junk (anything that will generate a compilation error). Compile the PRG...Bugger!

Now run the test again, only this time before you compile the PRG, go the command window and type

wmHandler.IgnoreEvents = .T.

Compile the PRG and notice the sky does not fall in.

So it would seem that the solution may be to implement some sort of BeforeCompile() event hook and disable my Windows message event processing. But how can I determine whether Fox is in the middle of compiling some code? I have tried the call stack but no luck there.

I have no choice, I am going to have to use the emergency red phone (the Fox phone) and contact Calvin. After all, it was Calvin who coded all this great functionality. If he can not help me, I am truly Wallaby Ted's brother Roo!

2 Comments:

Hi Scott,

Over on my blog I have an FLL that will allow you to do this and much more using a CBT hook. In any event, it could be used to solve your problem if Calvin doesn't come through for you and you don't mind an FLL dependency. Here's the link...