Search code examples
c++visual-studiocommfc

AfxGetInstanceHandle() triggers an assertion failure


I am using MFC in my C++ program (using Visual Studio 2008). I have to call AfxGetInstanceHandle() at the begining of my program.

This function triggers a break point:

AFXWIN_INLINE HINSTANCE AFXAPI AfxGetInstanceHandle()
{ ASSERT(afxCurrentInstanceHandle != NULL);
return afxCurrentInstanceHandle; }

The ASSERT statement fails. Is there something special that needs to be done in order to initialize the afxCurrentInstanceHandle before we try to access it?

PS: I am using MFC in a shared dll.

EDIT

My code is like that:

int _tmain(int argc, _TCHAR* argv[])
{

  CoInitialize(NULL);
  AfxGetInstanceHandle();
  return 0;
}

I would like to use the InstanceHandle in order to initialize a CComModule and then use it to manipulate com object.


Solution

  • If you are using MFC you shouldn't be providing a main, wmain, _tmain, or WinMain -- MFC provides its own entry point. Put initialisation code in InitInstance of your CWinApp derived class. If you don't have a CWinApp derived class you haven't created the project correctly -- use the Visual Studio Wizards for creating an MFC application.