Jump to content
  • Advertisement
Sign in to follow this  
VanillaSnake21

Faking HINSTANCE

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

If you intended to correct an error in the post then please contact us.

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 this post


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


Why?

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by cow_in_the_well
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 :).


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 this post


Link to post
Share on other sites
Quote:
Original post by cow_in_the_well
Pure 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 file
class Foo : public IFoo
{
public:
//overwriting virtual with the actual (windows definition) of HINSTANCE
virtual DWORD Something(HINSTANCE hInst);
}

Share this post


Link to post
Share on other sites
How 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; }

}

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by cow_in_the_well
How 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].

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!