Solved

Problem unloading mixed C++ DLL

Posted on 2006-11-13
9
350 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

740 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