I have created a c# 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:
我创建了一个包含RSA算法的c#dll,并在我的c ++ dll中加载了相同的dll。当我试图删除c#dll后,它的使用系统抛出了c#dll已经在使用的异常,尽管我已经释放了所有的接口指针。以下是我的代码:
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)
dw被赋予5(文件已被使用)
How to overcome this problem. If it cannot be deleted what are the workarounds then. Please help me out. Thanks in advance.
如何克服这个问题。如果无法删除那么解决方法是什么。请帮帮我。提前致谢。
1 个解决方案
#1
0
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).
如果它不需要“立即删除”,你可以使用MoveFileEx,在下次重启时删除(如果destination为NULL,则删除,而不是移动)。
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).
使用Process Explorer确定哪些句柄仍处于打开状态,并编写代码以显式关闭它们。您还可以使用Process Explorer的UI关闭句柄,并确认在强制关闭DLL后可以删除DLL(当然,您的应用可能会崩溃)。
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.
试试GetModuleHandle(“dllname.dll”),查看你的进程中是否仍然加载了DLL(如果没有,则返回NULL),如果仍然加载,则尝试使用句柄上的FreeLibrary。
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.
如果C#代码是你的,你可以使用C ++ / CLI和普通的“导出函数”代替COM吗?您可以通过这种方式更好地控制加载/释放。
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.
你的设计在某些方面存在缺陷,如果可能的话我尽我所能改变它。我见过的程序(主要是反病毒程序)会在文件被访问后保持打开相当长的时间。根据在加载文件并释放文件后立即删除文件的能力,听起来不健全。
#1
0
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).
如果它不需要“立即删除”,你可以使用MoveFileEx,在下次重启时删除(如果destination为NULL,则删除,而不是移动)。
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).
使用Process Explorer确定哪些句柄仍处于打开状态,并编写代码以显式关闭它们。您还可以使用Process Explorer的UI关闭句柄,并确认在强制关闭DLL后可以删除DLL(当然,您的应用可能会崩溃)。
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.
试试GetModuleHandle(“dllname.dll”),查看你的进程中是否仍然加载了DLL(如果没有,则返回NULL),如果仍然加载,则尝试使用句柄上的FreeLibrary。
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.
如果C#代码是你的,你可以使用C ++ / CLI和普通的“导出函数”代替COM吗?您可以通过这种方式更好地控制加载/释放。
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.
你的设计在某些方面存在缺陷,如果可能的话我尽我所能改变它。我见过的程序(主要是反病毒程序)会在文件被访问后保持打开相当长的时间。根据在加载文件并释放文件后立即删除文件的能力,听起来不健全。