Problem creating a DLL due to conflicts using MFC, Visual C++ 2008


  • Hello,

    I've already spent some hours trying to find a solution, but I couldn't, since I am not familiar with these settings, so I look for your help. This is the problem in detail:

    I have a VC++ project to create a DLL that will be used as a Plug-in in an external programm. This DLL uses some MFC functions. The initial configuration of the project was:

    -Use of MFC:  Use MFC in a Static Library

    -Common Language Runtime support:  No Common Language Runtime support

    -Runtime Library:  Multi-threaded Debug (/MTd)

    This configuration worked fine, there were no compilation errors and the resulting DLL worked correctly.

    The problem came when it was necessary to add to the DLL some functions of .NET, using the namespace System and similar. To do that, I had to change the Common Language Runtime support, to Common Language Runtime Support (/clr). Then, when I tried to compile, I got this message:

    '/MTd' and '/clr' command-line options are incompatible

    So I changed the Runtime Library to Multi-threaded Debug DLL (/MDd). Then I got this error message:

    Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version

    So I also changed the Use of MFC to Use MFC in a Shared DLL.

    After this, the compilation was correct. Then logically the size of the generated DLL is smaller, but this new DLL does not work correctly, the external programm in which this DLL is used crashes.

    I don't know what to do to fix the problem. Maybe I need to add some other DLLs or files to the directory where the DLL is located, where the external programm uses it. But I would prefer to include a single DLL file, but this seems to be incompatible with the use of .NET functionalities.

    Sorry for the long explanation. I would be very thankful is somedoby could help me.

    Thank you in advanced.


    Tuesday, January 29, 2013 5:04 PM

All replies

  • Hi,

    can you give details about the crash of the application that is using the DLL? Could it be that the MFC dlls required were missing?

    It would be nice if you could give us details about the crash of the external application.

    And the error message that I know gives more possibilities:
    fatal error C1189: #error :  Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d]

    If that is your message, you might want to go to project settings -> configuration settings -> C/C++ -> Preprocessor and add the definition _AFXDLL

    With kind regards,


    Wednesday, January 30, 2013 8:19 AM
  • Hello,

    thank you for your answer. I have tried you suggestion, but the external programm crashes the same as before. The configuration now is the following, and it compiles correctly:

    -Use of MFC:  Use MFC in a Static Library

    -Common Language Runtime support:  Common Language Runtime Support (/clr)

    -Runtime Library:  Multi-threaded Debug DLL (/MDd)

    The external programm crashes, and when I debug it with Visual C++, it gives this error message:

    An unhandled exception of type 'System.NullReferenceException' occurred in PI_Parameter.dll
    Additional information: Object reference not set to an instance of an object.

    It fails in the third line of the code of the DLL. This is the code:

        // connect to main app window
        CWnd* pWnd = new CWnd;

        AfxGetApp()->m_pMainWnd = pWnd;  -> This is where it fails

    As I said, the DLL works properly with the initial configuration. I haven't changed anything in the code, just the configuration of the project in order to use clr and be able to use .NET, but I haven't even included the .NET references for the moment.

    The size of the resulting DLL with the initial configuration (using Multi-threaded Debug (/MTd)) is 1795 Kb, and the size with the new configuration (Multi-threaded Debug DLL (/MDd)) is only 218 Kb. Am I missing something?

    Thank you for your help,

    Wednesday, January 30, 2013 10:44 AM
  • Hi,

    the AfxGetApp() seems to return null which causes the trouble. It seems that there was no CWinApp instance so far. Are you sure that the calling application created a CWinApp instance (or derived from that of course)?

    I would even suggest to ask this question inside the C++ general forum. I have to confess that I am mainly a C# developer - my C++ time ended when Visual Studio 2003 was published. The forum I have in mind can be found at

    With kind regards,


    Wednesday, January 30, 2013 12:36 PM
  • Hello,

    thank you for your answer, I will open a thread in the other forum. Regarding the CWinApp instance, I am not sure. But I guess that the problem is other, since I didn't modify the code for the moment, the DLL was given to me and it work fine.

    Best regards,


    Wednesday, January 30, 2013 1:52 PM