none
Fail to create window in C++ DLL called from C# RRS feed

Answers

All replies

  • Hi all,

    Quite a question title, I know, but what I am trying to do is call a simple function (in an exported MFC extension DLL) that creates a frame window, from within my C# application.

    I have the following code in my MFC dll

    DLL_SPEC int CreateMyWindow()
    {
        CFrameWnd* pMainWnd = new CMainFrame();
        pMainWnd->Create ( NULL, _T("MyWnd") );
        pMainWnd->ShowWindow(SW_SHOW);
        pMainWnd->UpdateWindow();
        return 1;
    }

    where DLL_SPEC is __declspec(dllexport).  The code behind CMainFrame() is that which is generated from the Visual Studio AppWizard when creating a new SDI application.

    I try and call this from my C# app as follows:

    [DllImport("TestDLL.dll")]
    public static extern int CreateMyWindow();

    private void button1_Click(object sender, EventArgs e)
    {
           int n = CreateMyWindow();
    }

    When this happens, I get an assertion in AfxWin1.inl, line 21, which is

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

    So obviously, C# is having a hard time getting the resource handle.  The thing is, the same code works when called from an MFC dialog test app...

    Can anyone see what I am doing wrong, please?

    • Merged by Kira Qian Wednesday, August 25, 2010 6:33 AM duplicate
    Tuesday, August 17, 2010 3:36 PM
  • In your MFC test harness, MFC has already been initialized and the DLL can start creating MFC objects. Is the C# application also an MFC application? I am not positive, but...isn't MFC unmanaged code in C++?
    Tuesday, August 17, 2010 6:30 PM
  • Thanks for the reply.  The C# app is not MFC, it is just a simple Windows C# app created using the app wizard.  With this information, how can I achieve what I am after here?  Is it possible?
    Tuesday, August 17, 2010 7:34 PM
  • I am not a C++ developer, but after reading your post in the C# Forum pointing here, I read a little.

    MFC must be initialized. I found a KB article how to do it in a ATL project. Maybe it helps you a little:
    http://support.microsoft.com/kb/173974

    Maybe you can play around a little.

    With kind regards,

    Konrad

    Thursday, August 19, 2010 9:18 AM
  • Could you paste your question details here?
    Monday, August 23, 2010 1:34 AM
  • Thanks for the reply.  Here is the body of the original question:

    Hi all,

    Quite a question title, I know, but what I am trying to do is call a simple function (in an exported MFC extension DLL) that creates a frame window, from within my C# application.

    I have the following code in my MFC dll

    DLL_SPEC int CreateMyWindow()
    {
        CFrameWnd* pMainWnd = new CMainFrame();
        pMainWnd->Create ( NULL, _T("MyWnd") );
        pMainWnd->ShowWindow(SW_SHOW);
        pMainWnd->UpdateWindow();
        return 1;
    }

    where DLL_SPEC is __declspec(dllexport).  The code behind CMainFrame() is that which is generated from the Visual Studio AppWizard when creating a new SDI application.

    I try and call this from my C# app as follows:

    [DllImport("TestDLL.dll")]
    public static extern int CreateMyWindow();

    private void button1_Click(object sender, EventArgs e)
    {
           int n = CreateMyWindow();
    }

    When this happens, I get an assertion in AfxWin1.inl, line 21, which is

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

    So obviously, C# is having a hard time getting the resource handle.  The thing is, the same code works when called from an MFC dialog test app...

    Can anyone see what I am doing wrong, please?

    Monday, August 23, 2010 8:59 AM
  • Thanks, but as you say that is to do with ATL.  I don't think it is possible to do this in C#...or is it?
    Monday, August 23, 2010 9:01 AM
  • 1. This thread is more appropriate in VC++ discussion than CLR.

    2. MFC extension DLL's can only be consumed by MFC client. MFC extension dll relies on the calling processes to get the module state, instance, etc. Since C# is a managed process (Non-MFC) with no knowledge of MFC it would fail.

    3. To resolve this issue. use MFC Regular DLL's instead of Extension DLL.

    4. For more information on extension dll, see http://msdn.microsoft.com/en-us/library/h5f7ck28(VS.90).aspx

    5. For more information on regular dll, http://msdn.microsoft.com/en-us/library/30c674tx(VS.80).aspx

    • Marked as answer by dazMan_Wad Wednesday, September 8, 2010 2:38 PM
    Wednesday, September 1, 2010 7:15 PM
    Moderator
  • Thank you very much billb08.  Changing to a regular MFC DLL fixed the problem :D
    Wednesday, September 8, 2010 2:38 PM