Public Group

Faking HINSTANCE

This topic is 3963 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi, This is the problem: I have an interface for a class in one file and the class itself in another. I want to keep the interface file free from any includes such as windows.h or others, but I need to use HINSTANCE as one of the params of the function in that interface. Since no extra files are included, the compiler marks it as undefined. Is there any way I can emulate HINSTANCE? I looked up its definition: if STRICT is not defined its simply "typedef void* HINSTANCE" but if it is ( and this seems to be the case) then its "struct HINSTANCE__ { int unused; }; typedef HINSTANCE__ *HINSTANCE;" I tried in many ways to emulate the above struct but nothing seems to work. Anyone has any ideas? P.S If you're not clear as to what I mean by emulate, for example LRESULT can be simply declared as long, WRESULT as long* etc.

Share on other sites
Quote:
 Original post by VanillaSnake21I want to keep the interface file free from any includes such as windows.h or others

Why?

Share on other sites
It simplifies debugging by a lot. When I compile a file I know that I'm compiling this single module, not 20 other includes with it. Also, I'm building an OS independent engine, by separating the includes this way I can make the compiler check for any OS/STD functions that shouldn't be there.

Share on other sites
I've always used void* for "faking" any windows handle (HINSTANCE, HWND, etc) without any problems. Since both definitions of HINSTANCE are pointers, void* should work in both cases :).

Share on other sites
Quote:
 Original post by cow_in_the_wellI've always used void* for "faking" any windows handle (HINSTANCE, HWND, etc) without any problems. Since both definitions of HINSTANCE are pointers, void* should work in both cases :).

void* was the first thing I tried. It simply tells me that the class cannot be instantiated because one of the pure virtuals in not defined. I think that the cast from HINSTNCE* to void* has a later bind time, so at this point the compiler doesn't know that they're equal.

Share on other sites
Pure virtuals? What pure virtuals?

Are you explicitly casting between the two?

void* foo = (void*)hInstance;
HINSTANCE hInstFoo = (HINSTANCE)foo;

Share on other sites
Quote:
 Original post by cow_in_the_wellPure virtuals? What pure virtuals?Are you explicitly casting between the two?void* foo = (void*)hInstance;HINSTANCE hInstFoo = (HINSTANCE)foo;

It's in my OP, I have an interface ( a class with pure virtual functions) and I'm making a class using that interface. So for example
class IFoo{public:virtual DWORD Something(void* hInst /*hinstance*/ ) = 0;}// different fileclass Foo : public IFoo{public: //overwriting virtual with the actual (windows definition) of HINSTANCE virtual DWORD Something(HINSTANCE hInst);}

Share on other sites

class Foo : public IFoo{public: // override base with same type as based virtual DWORD Something(void* hInst)  { return SomethingInternal( (HINSTANCE)hInst ); } void DWORD SomethingInternal(HINSTANCE hInst) { stuff; }}

Share on other sites
Your implementation should not be OS independent. That's the whole point of an implementation. Look here:

// --------------// IFoo.h// --------------class SomethingArgs{  // Empty baseclass for   // implementation specific data};class IFoo{public:  virtual long Something(SomethingArgs *args) = 0;};// --------------// Win32Foo.h// --------------#include "IFoo.h"#include <windows.h>class Win32SomethingArgs : public SomethingArgs{  HINSTANCE hInst;};class Win32Foo : public IFoo{public: virtual long Something(SomethingArgs *args) {   Win32SomethingArgs *win32args = dynamic_cast<Win32SomethingArgs*>(args);   doSomethingElse(win32args->hInst); }};// ----------------------------------// in main.cpp or where ever// ----------------------------------IFoo *foo = new Win32Foo();Win32SomethingArgs *args = new Win32SomethingArgs();args->hInst = hInstance;foo->Something(args);

Share on other sites
Quote:
 Original post by cow_in_the_wellHow about something like:class Foo : public IFoo{public: // override base with same type as based virtual DWORD Something(void* hInst) { return SomethingInternal( (HINSTANCE)hInst ); } void DWORD SomethingInternal(HINSTANCE hInst) { stuff; }}

Thanks cow_in_the_well, didn't think of doing that, it's working fine now [smile].

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5

• 26
• 11
• 9
• 9
• 11
• Forum Statistics

• Total Topics
633700
• Total Posts
3013428
×