简体   繁体   中英

How to delete a C# dll from a C++ dll

I have created ac# dll which contains RSA algo and same dll is being loaded in my c++ dll. When i am trying to delete the c# dll after its usage system is throwing exception that c# dll is already in use, though i have release all the interface pointers. Below is my code:

in C++

ICryptInterfaceRSA *crpt = null;
coinitialize(null);
hresult hr = ::cocreateinstance(guid1,guid2,<reinterpret cast>(void**)&crypt);
//doing my encryption and decryption
crpt->Release();
coUninitialize();

//Problem occurs at below mentioned code 
BOOL b = DeleteFile("C# dll file path");
DWORD dw = GetLasterror();

dw is being given as 5(File is already in use)

How to overcome this problem. If it cannot be deleted what are the workarounds then. Please help me out. Thanks in advance.

A few possible checks/answers:

If it doesn't need to be deleted "right now" you could use MoveFileEx , to delete on next reboot (if destination is NULL, it deletes, not moves).

Use Process Explorer to figure out what handles are still open, and write code to explicitly close them. You can also close the handles using Process Explorer's UI and confirm you can delete the DLL after you force-close them (your app might crash of course).

Try GetModuleHandle("dllname.dll"), to see if DLL is still loaded in your process (returns NULL if not) and try FreeLibrary on the handle if still loaded.

If the C# code is yours, can you use C++/CLI and normal "export functions" instead of COM? You have more control over loading/releasing this way.

Your design is flawed in some ways, I'd do all I could to change it if possible. I've seen programs (mostly Anti-Virus) that will keep files open for a fair amount of time after they have been accessed. Depending on the ability to delete a file right after loading it and freeing it, doesn't sound robust.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM