Solved

Problem unloading mixed C++ DLL

Posted on 2006-11-13
9
331 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
  • 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now