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.
I made a Console App with MFC and got the Message too. I found the solution, that you need a "prologue" at the beginning of your main (_tmain, etc).
int main(int args, char* argv[]) //, char *envp[])
{
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
return 1;
}
AfxGetInstanceHandle();
// TODO: code your application's behavior here.
...
Use:
AFX_MANAGE_STATE(AfxGetStaticModuleState());
Before you call:
AfxGetInstanceHandle();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With