Click here to Skip to main content
15,880,725 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello, I have a dll made in Delphi that is working perfectly, I use its functions within C# at runtime, but I am not able to clean it from memory.

What I have tried:

C#
[DllImport("kernel32.dll", SetLastError = true)]
      [return: MarshalAs(UnmanagedType.Bool)]
      static extern bool FreeLibrary(IntPtr hModule);



C#
IntPtr iTeste = TesteEnviaXml();
string sRetTeste = Marshal.PtrToStringAnsi(iTeste);
FreeLibrary(iTeste);
Posted
Updated 20-Jan-23 6:20am
v3
Comments
Richard MacCutchan 18-Jan-23 6:46am    
What happens when you call FreeLibrary?
Cristiano Rogoy 2023 18-Jan-23 8:57am    
[DllImport("DllTeste.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static extern IntPtr TesteEnviaXml();
Richard MacCutchan 18-Jan-23 9:17am    
Sorry I do not understand, what does that mean?
Cristiano Rogoy 2023 18-Jan-23 9:40am    
Nothing happens, when I run it and try to rename the dll it says it is in use.
I want to use the dll function and release it.
Richard MacCutchan 18-Jan-23 9:43am    
Sorry, that is far from clear. Please use the Improve question link above, and add complete details of the code you are using and what is not working.

FWIW, you cannot rename something that is locked for use.

you neeed to release the dll running under task manager
 
Share this answer
 
Comments
Graeme_Grant 18-Jan-23 18:33pm    
Vote 3: You want to expand with sample code to show the OP how to do this? eg: After identifying the dll name running under task manager, you would code it as follows: [add code after this].
If you use the DllImport mechanism

[DllImport("DllTeste.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static extern IntPtr TesteEnviaXml();


to create your access to the functions in DllTeste.dll, you cannot use FreeLibrary to unload that DLL. FreeLibrary requires the handle returned when the DLL is loaded, but you don't have that available to you. The .NET infrastructure has loaded the DLL for you when the DllImport code is executed at program start.

If you want to use FreeLibrary so that you can manipulate the DLL while the program is still running, you have to load it manually using LoadLibrary and then use GetProcAddress to get access to your function. After using it, you can then call FreeLibrary and rename the DLL.

(error checking removed for brevity)
// load the library
IntPtr dllHandle = LoadLibrary ("DllTeste.dll");
// get the function address
IntPtr funcPtr = GetProcAddress (dllHandle, "TesteEnviaXml");
// create the callable delegate (must match your DLL function's prototype)
delegate IntPtr dlgMethod ();
dlgMethod dlg = (dlgMethod) Marshal.GetDelegateForFunctionPointer (funcPtr, typeof (IntPtr));
// call the function
IntPtr ret = dlg ();
// release the library
FreeLibrary (dllHandle);
 
Share this answer
 
v2

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