Reconciling C / C++ function pointers in windows

Started by
5 comments, last by templewulf 18 years, 9 months ago
Conceptually, I've got everything worked out, but the execution is a little disappointing. I'm just trying to write a basic DirectDraw RPG in MSVC .NET and right now I'm writing a generic "gengame" class to base all of my other classes on. gengame manages the window and gamestate, so the trouble comes when I try to use gengame::InitWindow(HINSTANCE & hinstance), because that function assigns a function pointer to the WNDCLASSEX::lpfnWndProc member, but now that I have that in a class (as opposed to global), it won't work because of __stdcall and __thiscall differences that I don't really know how to fix. The code is at home.earthlink.net/~foreverbonded/genesis.zip Can anyone show me the appropriate way to fix this or at least a moderately elegant shortcut? Thanks
XBox 360 gamertag: templewulf feel free to add me!
Advertisement
I've tried to look at your code but it's telling me the page cannot be found. However, if the wndproc function is in a class, I believe you need to make it static, but I'm not sure as you've said you're assigning it a function pointer?

Be easier to see if the code worked, but if the wndproc is now in your class, then try making it static.

Edit: Bit rusty in this field. Haven't coded for a while because of tendinitis, so my apologies if that doesn't work.
whoops!

There's a capital G in that filename, so it should be

home.earthlink.net/~foreverbonded/Genesis.zip

the static part actually did resolve my problem, but now it gives me errors that say static member functions cannot contain this pointers! That kinda defeats the purpose of having it in the class, doesn't it? lol

Well, at least I'm a step closer.

Thanks
XBox 360 gamertag: templewulf feel free to add me!
maybe this will help you.
OK I've got the code now.

Quote:from an article
Every C++ class has an implicit pointer called the this pointer. The this pointer points to the instantiation of the object. The this pointer can be dereferenced to members of the class like any other pointer. Inside of a class declaration the use of the this pointer is implicit...


That means that when you call a function from a class, the 'this' pointer is implicitly used in the parameter list. Example:

class CFoo{   public:      void foo();};


When you call foo(); it's really being translated as:

foo(CFoo *this);


Being as static members have file scope, there is no 'this' pointer, as you can call the static member without having instantiated a class. That's where your problem lies. [wink]
Quote:Original post by templewulf
but now it gives me errors that say static member functions cannot contain this pointers! That kinda defeats the purpose of having it in the class, doesn't it? lol


Ok, so you do seem to have some idea of what is going on. However, consider: suppose you have a function pointer to some non-static member function. What do you expect the this pointer to *be*? I.e. how are you going to specify which object to call the function on?

The C++ solution is that pointers to member functions have their own type (er, set of types, since the type depends on the function signature too), which basically explains the __stdcall/__thiscall difference. The "pointer to member function" is actually a sort of structure in memory (this is very implementation dependant!) that somehow represents both a pointer to the necessary code, and the this-pointer that will be used for invoking the code. Thus when you create that "bound" pointer-to-member-function, there is special syntax, and then IIRC special syntax again for invoking the thing. Unfortunately I can't remember the details; [google] "bound pointer to member function".
I'm still not sure I understand the difference between stdcall and thiscall, but this certainly solves my problem! Thanks for all the support, guys.
XBox 360 gamertag: templewulf feel free to add me!

This topic is closed to new replies.

Advertisement