Revenge
Revenge

Reputation: 51

.NET COM Library is not unloaded from C++ host process

We have a plugin system here, written in c++. Now that this has to have an ability to upgrade plugins(which are COM) we need to unload the plugin, install the plugin and then load it again. Now the problem is that this has to happen without closing an app. c++ COM dlls get unloaded pretty good but .NET ones not. Here's the sample code I'm using to load/unload the COM.

#include "stdafx.h"
#import "C:\Projects\MyTLBWithInterface.tlb" raw_interfaces_only, raw_native_types, no_namespace, named_guids


int _tmain(int argc, _TCHAR* argv[])
{
    CoInitialize(0);

    try
    {
        CLSID rclsid;


        HRESULT  hr = CLSIDFromProgID(_T("MY_NET_COM"), &rclsid);

        if (hr != S_OK)
        {
            return false;
        }

        IMYInterfaceForCom *cpi =NULL;

        hr = CoCreateInstance(rclsid, 0, CLSCTX_ALL, __uuidof(IUnknown),reinterpret_cast<void**>(&cpi));

        if (SUCCEEDED(hr)) 
        {

            BSTR  name;
            cpi->GetName(&pluginName);
                MessageBox(0,pluginName, L"MyApp", MB_OK|MB_ICONERROR);

            ULONG CC = cpi->Release();

            CoFreeUnusedLibraries();
        }

    }
    catch(_com_error & e)
    {
        _bstr_t bstrSource(e.Source());
        _bstr_t bstrDescription(e.Description());
        printf("\nException:\n\tSource : %s \n\tDescription : %s \n",(LPCSTR)bstrSource,(LPCSTR)bstrDescription);
    }
    catch(...)
    {
        printf("\nException");
    }

    CoUninitialize();
    return 0;

}

Upvotes: 2

Views: 197

Answers (2)

Revenge
Revenge

Reputation: 51

Resolved via this thread

Upvotes: 0

DrPizza
DrPizza

Reputation: 18360

I would not support .NET plugins for the reasons I describe in this post

Upvotes: 1

Related Questions