Solved

Problem unloading mixed C++ DLL

Posted on 2006-11-13
9
354 Views
Last Modified: 2010-04-24
I am currently writing a C++ mixed DLL containing both managed and unmanaged code with the purpose of implementing the extension interface of a legacy C++ application and using the advanced features of the .NET framework to exend the functionality of the application. I was able to load this DLL into the legacy application and use the .NET framework classes in my extension without problems except for unloading the extension. I noticed that when the application unloads the extension it is never unloaded cleanly. The DLL_PROCESS_DETACH is never called !!
While trying to precisly define the problem, I created a minimal extension without any managed functions, and it was loaded/unloaded without any problems, but when I add any managed function (even if it was an empty function) the extension does NOT unload. I tried to use the AppDomain.Unload() but without success. Is there a way to make such mixed application extension unload cleanly ?
0
Comment
Question by:vt96
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 17936164
What C++ version?
0
 

Author Comment

by:vt96
ID: 17937680
Visual Studio 2005
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17937731
Can you post some minimal code which allows to reproduce this problem?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:vt96
ID: 17940340
Here is the minimal code that shows the problem. It is commented to be illustrative.

#include "stdafx.h"
#using <mscorlib.dll>
using namespace AnvilCSharpExt;

static AFX_EXTENSION_MODULE TheDLL = { NULL, NULL };
// Switch to unmanaged mode for the DllMain
#pragma managed(push, off)

extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
    UNREFERENCED_PARAMETER(lpReserved);
    if (dwReason == DLL_PROCESS_ATTACH)
    {
        if (!AfxInitExtensionModule(TheDLL, hInstance))
            return 0;
        new CDynLinkLibrary(TheDLL);
    }
    else if (dwReason == DLL_PROCESS_DETACH)
    {
        AfxTermExtensionModule(StubDLLDLL);
    }
    return 1;
}
// Backto managed
#pragma managed(pop)

extern "C" {

void WINAPI AnExtensionInterfaceFunction()
{
    // Although this is an empty function that will be called by the application that loads this extension, making it a managed function prevents the DLL from unloading properly
}

} //extern "C"
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17940457
So, client should load Dll using LoadLibrary and call AnExtensionInterfaceFunction? And when FreeLibrary is called, DllMain with DLL_PROCESS_DETACH is not called?
0
 

Author Comment

by:vt96
ID: 17941599
Thank you for your help.
Yes, the client load the extension dynamically uysing LoadLibrary, and when freeing the Library using FreeLibrary the Dllmain() with DLL_PROCESS_DETACH is never called if any "managed" function was previously called in the DLL either directly or indirectly (through another unmanaged function). If the DLL does not have any managed function that was called between LoadLibrary and FreeLibrary, then it is freed normally and the DLL_PROCESS_DETACH of DllMain() is called as usual.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17944978
I will try to test this some time later. I added this question to my bookmarks, cannot promise immediate result.
My first guess is that once managed function is called, some managed instances are allocated. They are subject of garbage collecting, this prevents library to be unloaded immediately. Even if function call doesn't do anything, loading library requires .NET to read library metadata, this allocates a lot of managed instances. Possibly unloading managed library is unpredictable like releasing of garbage-collected objects.
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 17946042
The only thing I found is this:
In the .NET Framework, there's no way to directly unload an assembly ( from http://msdn.microsoft.com/msdnmag/issues/05/10/CatWork/default.aspx ) - with some stuff about domains.

Actually, I don't have answer to this question. It is interesting enough and I think you have good chance to get some details in MSDN forum:
http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=29&SiteID=1

It is free, you need only to register, and sometimes Mcrosoft developers provide information which cannot be found in any other place.
0
 

Author Comment

by:vt96
ID: 18038205
Thank you for your help
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question