# C++ Class wrapper on top of WinMain/DllMain

I've written some code to do the job for me. The final results seems to work fine, but I would like to know if this is how I should Handle classes. And where I can improve or things which are just plain stupid.

class CInit
{
public:
CInit();
~CInit();

void Check(bool);

virtual bool Init();
virtual bool ExInit();

private:
static CInit* _ThisInit;
};
CInit* CInit::_ThisInit;

CInit::CInit()
{
if(_ThisInit == 0)
_ThisInit = this;
}

CInit::~CInit()
{

}

void CInit::Check(bool bStart)
{
if(bStart)
_ThisInit->Init();
else
_ThisInit->ExInit();
}

bool CInit::Init()
{
return true;
}

bool CInit::ExInit()
{
return true;
}


This is my cInit class, as you may see this has a static value, because in when the program execetues it will first run the global class instance which will be used like this. It will overload Init() and there the program starts.

    CRaPE mycp;

bool CRaPE::Init()
{
return true;
}

bool CRaPE::ExInit()
{
return true;
}


The program runs from here:

CInit* _Init;

void HandleStartUp()
{
_Init = new CInit();
_Init->Check(true); // So Init gets called
}

void HandleExit()
{
_Init->Check(false); // so ExInit gets called
delete _Init;
}

BOOL WINAPI DllMain(
HINSTANCE hinstDLL,
DWORD fdwReason,
LPVOID lpReserved )
{
switch( fdwReason )
{
case DLL_PROCESS_ATTACH:
{
HANDLE hThread = CreateThread(0,0,(LPTHREAD_START_ROUTINE)HandleStartUp,0,0,0);
if(hThread == 0)
return false;
}
break;

case DLL_THREAD_ATTACH:
break;

case DLL_THREAD_DETACH:
break;

case DLL_PROCESS_DETACH:
HandleExit();
break;
}
return TRUE;
}


I hope I explained well, thanks for reading.

Gz

## 2 Answers

it's better to explicitly call init and exinit than pass a bool to "Check" to say which will one will get called. maybe call them "Start" and "Exit"

Also, I'd suggest not making them virtual, if you want to be able to extend it...use a protected virtual

public: bool Start();
protected: virtual bool StartImpl();

void CInit::Start()
{
StartImpl();
}


This gives you a bit more control over the calling in to your API. (Also a mostly recommended best practice for C++).

It's called "Non Virtual Interface" or NVI. It does have pros and cons. But in this case I think it would be quite useful.

I am sorry but at the moment it is not really clear what are you trying to achieve with this code. Why do you need such confusing initialization scheme? What is CRaPE class and what it's got to do with the rest of the code?

Also, according to C++ rules identifiers beginning with an underscore and an uppercase letter are reserved in any scope and therefore names like _ThisInit and _Init shouldn't be used.

• I'm trying to code a class on top of DllMain/WinMain, so I don't have to handle it everytime and the program starts inside my CRaPE class ( in this case) class CRape : public CInit That's how I use it, so CRape::Init() is the start of the program for me. – NLScotty Aug 22 '11 at 5:31
• The question was what are you trying to achieve, not what are you trying to code. Also CRaPE has no relation to CInit in the code you've posted. Also, if you want to execute CRaPE::Init() as your initialization, you should make new CRaPE object in HandleStartUp fuction instead of CInit. – Alexvn Aug 22 '11 at 6:17