Solved

Problem unloading mixed C++ DLL

Posted on 2006-11-13
9
337 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

816 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

11 Experts available now in Live!

Get 1:1 Help Now