Why AfxGetResourceHandle causes Asserts?

My scenario is somehow complicated but I try to simplify it:

I have a CLR class libraray that wrapps some functionalities of a MFC dll. The MFC dll has a class with a method that calls AfxGetResourceHandle() method, when I use the corresponding method on the wrapper the Asserts are generated.

The method is trying to get a handle of a resource that is defined inside the MFC dll, why I got asserts

..\Microsoft Visual Studio 8\VC\atlmfc\include\afxwin1.inl

_AFXWIN_INLINE HINSTANCE AFXAPI AfxGetInstanceHandle()

{ ASSERT(afxCurrentInstanceHandle != NULL);

return afxCurrentInstanceHandle; }



Answer this question

Why AfxGetResourceHandle causes Asserts?

  • Soeren D.

    You probably need this :

    AFX_MANAGE_STATE(AfxGetStaticModuleState( ));

    before you invoke the MFC DLL method.

    Shadi_05 wrote:

    My scenario is somehow complicated but I try to simplify it:

    I have a CLR class libraray that wrapps some functionalities of a MFC dll. The MFC dll has a class with a method that calls AfxGetResourceHandle() method, when I use the corresponding method on the wrapper the Asserts are generated.

    The method is trying to get a handle of a resource that is defined inside the MFC dll, why I got asserts

    ..\Microsoft Visual Studio 8\VC\atlmfc\include\afxwin1.inl

    _AFXWIN_INLINE HINSTANCE AFXAPI AfxGetInstanceHandle()

    { ASSERT(afxCurrentInstanceHandle != NULL);

    return afxCurrentInstanceHandle; }



  • cosmopoet

    Shadi_05 wrote:
    I created a CLass Library project with the wizards, it is "using MFC in a shared DLL" and it uses /CLR switch. Not enough isn't it

    Ah, then it looks like you just need some #include's in your stdafx.h to be able to use AFX_MANAGE_STATE.

    When you enable "using MFC in a shared DLL", the header files are not automatically inserted for you. Create a fresh MFC app, and copy/paste the MFC header files, and you should be okay.



  • yorengoy

    I created a CLass Library project with the wizards, it is "using MFC in a shared DLL" and it uses /CLR switch. Not enough isn't it
  • bmm22

    Shadi_05 wrote:
    I created a CLass Library project with the wizards, it is "using MFC in a shared DLL" and it uses /CLR switch. Not enough isn't it

    The managed method that is calling the MFC dll is a method of a class that inherits

    MarshalByRefObject. Could I have both of these in a MFC dll with /clr enabled


  • zagolin

    Shadi_05 wrote:

    Shadi_05 wrote:
    I created a CLass Library project with the wizards, it is "using MFC in a shared DLL" and it uses /CLR switch. Not enough isn't it

    The managed method that is calling the MFC dll is a method of a class that inherits

    MarshalByRefObject. Could I have both of these in a MFC dll with /clr enabled

    That shouldn't cause any problems as far as I can tell.



  • Paolo Hutchison

    I'm invoking the method from a mixed assembly, and I'm a C# programmer not familiar with c++ includes. When I add this line, it complains that these are undeclared. Do I need to include something before calling this
  • KrustyDeKlown

    Shadi_05 wrote:
    I'm invoking the method from a mixed assembly, and I'm a C# programmer not familiar with c++ includes. When I add this line, it complains that these are undeclared. Do I need to include something before calling this

    If the DLL is an MFC extension DLL, then you cannot use it from a non-MFC caller. Which means your mixed mode DLL must be an MFC regular DLL with /clr enabled. Is that the case here



  • Why AfxGetResourceHandle causes Asserts?