• Which the release of FS2020 we see an explosition of activity on the forun and of course we are very happy to see this. But having all questions about FS2020 in one forum becomes a bit messy. So therefore we would like to ask you all to use the following guidelines when posting your questions:

    • Tag FS2020 specific questions with the MSFS2020 tag.
    • Questions about making 3D assets can be posted in the 3D asset design forum. Either post them in the subforum of the modelling tool you use or in the general forum if they are general.
    • Questions about aircraft design can be posted in the Aircraft design forum
    • Questions about airport design can be posted in the FS2020 airport design forum. Once airport development tools have been updated for FS2020 you can post tool speciifc questions in the subforums of those tools as well of course.
    • Questions about terrain design can be posted in the FS2020 terrain design forum.
    • Questions about SimConnect can be posted in the SimConnect forum.

    Any other question that is not specific to an aspect of development or tool can be posted in the General chat forum.

    By following these guidelines we make sure that the forums remain easy to read for everybody and also that the right people can find your post to answer it.

FSX Access violation on subclassing

Messages
31
I have a FSX gauge, and would like to subclass (replace the window procedure in order to process keyboard input).
After I wrote my code ("SetWindowLongPtr(GWL_WNDPROC)"), it works fine, but on exit I get an exception. Regardless I call SetWindowLongPtr() second time (to restore old windowproc), or not.

Cannot find what I did incorrectly. In my code I process 'my' keypress messages and return FALSE; for other messages I call old window procedure via CallWindowProc():

Code:
LRESULT CALLBACK KeysHandler(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
    //
    //    blah-blah skipped
    //
    if(GetKeyState(VK_CONTROL) & 0x8000)
        nKbFlags |= 32;
    //
    //    blah-blah skipped
    //
    int wmId = LOWORD(wparam); 
 
    if(bMsgForMe)
    {
        nKeyToSend = SOMEVALUE;
        if(nKeyToSend >= 0)
        {
            pPtr->key_handler(nKeyToSend);
            return FALSE;
        }
    }
 
    return CallWindowProc((WNDPROC)pFSX_WP, hwnd, msg, wparam, lparam);
} // KeysHandler

Code:
    //
    //    DLL main code, initialization
    //
    hFSWnd = FindWindow("FS98MAIN", NULL);
    if (hFSWnd)
    {
        //pFSX_WP = GetWindowLong(hFSWnd, GWL_WNDPROC);
        pFSX_WP = SetWindowLongPtr(hFSWnd, GWL_WNDPROC, (__int3264)(LONG_PTR)KeysHandler);
    } // if

Code:
    //
    //    DLL main code, deinitialization
    //
    SetWindowLongPtr(hFSWnd, GWL_WNDPROC, (__int3264)(LONG_PTR)pFSX_WP);

pFSX_WP receives values like 0x2012E413, which seems like valid. In debug mode I can see a lot of Win32 message handling functions in Call Stack, so the reason might be in unprocessed messages in message queue or something like that.

I tried a lot of ways: SetWindowLongPtr and SetWindowLongA; different places for calling (case PANEL_SERVICE_POST_INSTALL/PANEL_SERVICE_PRE_KILL vs DllMain:DLL_PROCESS_ATTACH/DLL_PROCESS_DETACH). Nothing helped.

Here is my log from SetWindowLongPtr() return:
Code:
FSX_WindowProc: [0x2012E413], changed to MyDLL_WindowProc: [0x9239FB9].
MyDLL_WindowProc: [0x9239FB9], changed back to FSX_WindowProc: [0x2012E413].
That is, second call of SetWindowLongPtr() returns address of my KeysHandler() correctly.

Any ideas?
 
I seem to recall messing around with this very thing, and getting nowhere. I now cannot find my code, which leads me to believe I deleted it in disgust. I'm pretty sure I never solved it. That said...
Looking at what you have posted here, I wonder what would happen if someone has subclassed the FS window after you have. You might want to use the GetWindowLong() function to determine whether you should be calling SetWindowLongPtr() on exit. If GetWindowLong() is not returning a pointer to your function, then best not call SetWindowLongPtr().
Alternatively, try not calling SetWindowLongPtr() until PANEL_SERVICE_PRE_DRAW has occurred at least once.

Doug
 
I seem to recall messing around with this very thing, and getting nowhere. I now cannot find my code, which leads me to believe I deleted it in disgust. I'm pretty sure I never solved it. That said...
Looking at what you have posted here, I wonder what would happen if someone has subclassed the FS window after you have. You might want to use the GetWindowLong() function to determine whether you should be calling SetWindowLongPtr() on exit. If GetWindowLong() is not returning a pointer to your function, then best not call SetWindowLongPtr().
Alternatively, try not calling SetWindowLongPtr() until PANEL_SERVICE_PRE_DRAW has occurred at least once.
Doug

Thank you Doug!
Now I make both calls of SetWindowLongPtr() in PANEL_SERVICE_PRE_DRAWs, which works much better.
No CTD now, when I close FSX after I deactivated subclassing by clicking some clickzone.
But still CTD happens, when I close FSX without deactivation of subclassing. For such case I keep old second call in "case PANEL_SERVICE_PRE_KILL" (with the verifying you advised). This second call works but after some time I get CTD.

So what the people use in their gauges instead of SetWindowLongPtr? I also thought about Simconnect, here I could use a function like SimConnect_MapInputEventToClientEvent(), but I guess that's not a solution, since it requires declaration of EACH key.
 
Fs98float

Another question:
In case the floating window (2D panel) is active (in foreground), I receive messages no more.
Apparently because I subclass using the main window HWND:
Code:
FindWindow("FS98MAIN"
and those floating windows have "FS98FLOAT" classes, not "FS98MAIN".

So, will I have to call SetWindowLongPtr() for both kinds of windows ("FS98MAIN" and "FS98FLOAT")? See the example below:
Code:
SetWindowLongPtr(hFSWnd, GWL_WNDPROC, (__int3264)(LONG_PTR)KeysHandler);
SetWindowLongPtr(hFSChildWnd, GWL_WNDPROC, (__int3264)(LONG_PTR)KeysHandler);

Or maybe are there some method to subclass once for main window including its child windows?
 
Back
Top