Problem creating a DLL due to conflicts using MFC, Visual C++ 2008
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

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

Hybrid View

  1. #1
    Join Date
    Jan 2013
    Posts
    8

    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.

    Dario

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,524

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

    1. Define "crashes".
    2. Did you debug your dll?
    3. Note that only Release version of your dll may be deployed.
    Victor Nijegorodov

  3. #3
    Join Date
    Jan 2013
    Posts
    8

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

    Quote Originally Posted by VictorN View Post
    1. Define "crashes".
    2. Did you debug your dll?
    3. Note that only Release version of your dll may be deployed.
    Hello,

    thank you for your answer. The DLL, compiled with the initial configuration (without Multi-threaded Debug (/MTd) and without clr) works fine.

    The external programm crashes when it uses the DLL. The error that I get when I debug this with Visual C++ is the next:

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


    And it crashes in the third line of the code of the function of the DLL, that is the next:

    extern "C" void WINAPI DllInfo(HWND hwndMain)
    //
    // entry point for display of about info dialog box for plug-in DLL
    //
    {
    // connect to main app window
    CWnd* pWnd = new CWnd;
    pWnd->Attach(hwndMain);

    AfxGetApp()->m_pMainWnd = pWnd;
    ->Here crashes

    I didn't changed any code for the moment, I just changed the configuration as I described. The size of the DLL with the initial configuration is 1795 Kb, and with the new is 218 Kb.

    In summary, the configuration now is:

    -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)
    -I added also _AFXDLL to the preprocessor


    I have tried in both debug and release modes, with the same results.

    Thank you for your help,

    Dario

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,524

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

    Quote Originally Posted by dariotojeiro View Post
    And it crashes in the third line of the code of the function of the DLL, that is the next:

    Code:
    extern "C" void WINAPI DllInfo(HWND hwndMain)
    //
    // entry point for display of about info dialog box for plug-in DLL
    // 
    {
    	// connect to main app window
    	CWnd* pWnd = new CWnd;
    	pWnd->Attach(hwndMain);
    
    	AfxGetApp()->m_pMainWnd = pWnd;[/I]   ->Here crashes
    What does AfxGetApp() return?
    Victor Nijegorodov

  5. #5
    Join Date
    Jan 2013
    Posts
    8

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

    I didn't programm the DLL actually, but it seems to be a function of MFC:

    http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,524

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

    Quote Originally Posted by dariotojeiro View Post
    I didn't programm the DLL actually, but it seems to be a function of MFC:

    http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx
    Yes, we do know that AfxGetApp() is "a function of MFC".
    And what does it return inside your dll
    Code:
    extern "C" void WINAPI DllInfo(HWND hwndMain)
    funcrion?
    Victor Nijegorodov

  7. #7
    Join Date
    Apr 1999
    Posts
    27,444

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

    Quote Originally Posted by dariotojeiro View Post
    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).
    And once you did that, you are taken into a different world.

    You are mixing in a different technology than what is expected in a normal DLL. Since you now added .NET code to your DLL, you basically are out of the realm of what this forum can help you with. Instead you now enter the world of .NET and Managed code, and there is a separate forum dedicated to that type of coding (the Managed Code) forum.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Jan 2013
    Posts
    8

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

    Quote Originally Posted by Paul McKenzie View Post
    And once you did that, you are taken into a different world.

    You are mixing in a different technology than what is expected in a normal DLL. Since you now added .NET code to your DLL, you basically are out of the realm of what this forum can help you with. Instead you now enter the world of .NET and Managed code, and there is a separate forum dedicated to that type of coding (the Managed Code) forum.

    Regards,

    Paul McKenzie
    Thank you for your answer. Should I post then my doubt in the corresponding Forum? or is it impossible to mixture the 2 technologies?

  9. #9
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,016

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

    Quote Originally Posted by dariotojeiro View Post
    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).
    How are you communicating between the application and your DLL? Do you only use a C API or do you export classes or use non-POD types in exported functions as well?
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center