Something with callbacks and object instance and such...

From: Robert Diamond (aorevolt_at_hotmail.com)
Date: 03/26/04


Date: Fri, 26 Mar 2004 10:02:22 GMT


    Ok... I write uber cool oop code to get my app started with objects all
over the place and tweak a few things and it works... Then in my makeshift
main objects WndProc I try to read or write to a protected var and it
crashes the app "An Access Violation (Segmentation Fault) raised in your
program." (from the dev-c++ debuger). Change the var to public and it
crashes... remove that code and try to set-up a timer (timers are callback
functions that get run after a timeout) in the same way i set-up the
makeshift WndProc. The timer (which is basically a duplicate of my WndProc)
can read/write to private/protected/public vars :) ... but why not WndProc?
Anyways, If you managed to read all this and think you can help out... here
is some code snipets:

/******* Main.cpp *******/
// The object that does all the work
MainObj *mainObj= NULL;

LRESULT CALLBACK MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM
lParam) {
    return (*mainObj).WndProc(hWnd, uMsg, wParam, lParam);
}

VOID CALLBACK WndProcDuplicateTimer(HWND hWnd, UINT uMsg, UINT_PTR idEvent,
DWORD dwTime) {
    (*mainObj).WndProcDuplicateTimer(hWnd, uMsg, idEvent, dwTime);
}

...

int WINAPI WinMain(...) {

...

    // Create our base object that is basically a oop replacement for
winmain and such (constructor sets up window and callback and timer and
such)
    if ((mainObj = new MainObj()) == NULL) error = true;

    if (!error) {
        // Loop That Runs While we don't have a WM_QUIT
        while (GetMessage(&msg, NULL, 0, 0)) {
            TranslateMessage(&msg); // Translate The Message
            DispatchMessage(&msg); // Dispatch The Message (this get's sent
to MainWndProc, which then sends it to the (*mainObj).WndProc)
        }
    }

    // If our Main Object still exists, delete it (destructor handles app
cleanup)
    if (glMain != NULL)
        delete glMain;

    return error;
}

/*********** MainObj.cpp ************/

class MainObj {
    public:
        MainObj();
        LRESULT /*NOT Callback... it's not a function pointer, just a
regular function*/ WndProc(HWND hWndHdl, UINT uMsg, WPARAM wParam, LPARAM
lParam);
        void WndProcDuplicateTimer(HWND hWndHdl, UINT uMsg, UINT_PTR
idEvent, DWORD dwTime);
        ~GlMain();

    protected:
        ...
        bool active;
        ...
};

in WndProc and WndProcDuplicateTimer if put a simple single line "if
(active) active = true;" in WndProc I get Segmentation failure and in
WndProcDuplicateTimer I get no error. BTW MainObj::WinProc has stuff to
handle WM_QUIT can do stuff like "if (HIWORD(wParam)) return 0;" and calls
DefWindowProc if it get's to that... I think the reason things are failing
is because the callback function in Main.cpp is calling the
MainObj::WndProc. I tried to setup MainObj::WndProc as the callback, but
keeped getting "Can't convert ...(MainObj::*)WndProc... to ...(*)WndProc..."
so I came up with this solution. Something to note... I don't virtual,
inline, etc. any of the functions (at least not until I knw thier working).
Thanks in advance, and I know this may be a tuff one, but any help will help
alot, i'm completely lost ;(



Relevant Pages

  • Re: Quick question on object instantiation!
    ... I did include the object instantiation in my WndProc and made it static. ... int GetCounterconst {return iCounter;} ... WPARAM wParam, LPARAM lParam) ...
    (microsoft.public.vc.language)
  • Re: Renaming Buttons in Visual C++
    ... you should add following to your dialog callback ... > BOOL DialogCallback(HWND hWndDialog, UINT uMsg, WPARAM wParam, LPARAM ...
    (microsoft.public.win32.programmer.ui)
  • Re: In the need for some advice!
    ... LRESULT CALLBACK CW1_WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM ...
    (microsoft.public.vc.language)
  • Re: Query on dialog box
    ... BOOL CALLBACK AdPropPageProc(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM ... BOOL CALLBACK EditWmProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM ...
    (microsoft.public.win32.programmer.ui)
  • Re: Understanding callback
    ... I have seen the WndProc prototyped two ways, ... Are these functionally equivalent? ... Under 64 bit windows they are not since LRESULT ... CALLBACK is also defined as _stdcall. ...
    (comp.lang.c)