Click here to Skip to main content
15,882,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created on COM interop DLL using the below mentioned code.

// CSharpServer.cs
// compile with: /target:library
// post-build command: regasm CSharpServer.dll /tlb:CSharpServer.tlb

using System;
using System.Runtime.InteropServices;
namespace CSharpServer
   // Since the .NET Framework interface and coclass have to behave as 
   // COM objects, we have to give them guids.
   public interface IManagedInterface
      int PrintHi(string name);

   public class InterfaceImplementation : IManagedInterface
      public int PrintHi(string name)
         Console.WriteLine("Hello, {0}!", name);
         return 33;

My C++ DLL is calling above said com DLL which uses below mentioned code to call

IManagedInterface* iFace = NULL

HRESULT hr = ::CoCreateInstance(GUID1,GUID2,reinterpret_cast<void**>(&iFace));


int n = iFace->Release();

   hr = DllCanUnloadNow();
now my requirement is to delete the COM Interop DLL 

BOOL b = DeleteFile("COM interop dll's path");

GetLastError() returned as 5. (File is already in use)

I am unable to delete this COM DLL. Please help me out.

Thanks in advance.
Updated 7-Sep-14 7:11am
Sergey Alexandrovich Kryukov 7-Sep-14 11:24am    
Why would you need to delete the DLL? You cannot delete it because it is loaded in memory as executable code. OS prevents deleting such modules, for apparent reasons.
Member 10780730 8-Sep-14 9:06am    
Hi Sergey thanks for your response, i want some few buy ins from you, kindly update me....
Is there no way i can unload it from memory space actually my requirement says its need to be deleted after you release. Think of reinstallation of any software where installer is using this com dll for a purpose it will dump this com dll again into the folder though installer is still alive, if it does not get unload then how come file will be replaced.

1 solution

MSDN states you should not explicitely call DllCanUnloadNow, it is called for you by CoFreeUnusedLibrariesEx. Also you have to make sure your implementation of DllCanUnloadNow returns S_OK.
Share this answer
Member 10780730 8-Sep-14 9:02am    
If i examine DllCanUnloadNow it gives S_OK, still some process is using this dll i guess which i unable to get,by the way is it true if some application is using COM dll for a purpose though it gets released by the application at some point of time still it is alive till the Applications lifetime ends.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900