This is a discussion on How to unload both Managed and Unmanaged DLL? within the C# Programming forums, part of the Software Development category; How to unload both Managed and Unmanaged DLL...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| I had to create a Totally new DLL like thise and Reference that dll into my Own Code. This call contains only two methods one to CallFunctionforManagedDLL and CallFunctionForUnmanagedDLL public class DLL : MarshalByRefObject { } 2) In my own Code now i have to first check whether the dll user has given as path is Managed or Unmanaged using (FileStream fs = File.Open(tbDLLPath.Text, FileMode.Open)) { using (MemoryStream ms = new MemoryStream()) { byte[] buffer = new byte[1024]; int read = 0; while ((read = fs.Read(buffer, 0, 1024)) > 0) ms.Write(buffer, 0, read); Assembly assembly = Assembly.Load(ms.ToArray()); } } Above code will help you to find that. If Assembly.Load throws an exception that means It is Unmanaged DLL. 3) Now Create a totally New Appdomain AppDomain Temporary = AppDomain.CreateDomain("Temporary"); try { //Creates new Type in Temp Domain DLL Proxy = (DLL)Temporary.CreateInstanceAndUnwrap("DLLAppDoma in", "DLLAppDomain.DLL"); if (managed) { MessageBox.Show("Executing Managed DLL"); Object retobj = Proxy.CallManagedDLLFunction(dllFilePath, methodeName, arg, types); } else { MessageBox.Show("Executing Unmanaged DLL"); Object retobj = Proxy.CallUnmanagedDLLFunction(dllFilePath, methodeName, arg, types,returnType, UnloadDll); } 4) In the CallUnamagedDLLFunction..one thing you should make sure that you unload the DLL file from the module. after you have executed the Function Dynamically. // For HtmlHelpA function the code was acting weird // When i execute the code the Help file is opened but after that the // MyApplication crashes..If i go in debug mode and close the help file after it is opened // everything seems to work. New idead to put CheckBox that will ask user // if they want to Free or Unload a DLL after its execution is done. if (UnloadDLL) { IntPtr ptr = GetModuleHandle(filename); FreeLibrary(ptr); } This will help you unlock the DLL file your application was using during execution. 5) Finally Unload the Appdomain.. finally { AppDomain.Unload(Temporary); } |
![]() |
| Thread Tools | |
| Display Modes | |
| |
LinkBacks (?)
LinkBack to this Thread: http://www.discussweb.com/c-programming/2391-how-unload-both-managed-unmanaged-dll.html | |||
| Posted By | For | Type | Date |
| How to unload both Managed and Unmanaged DLL? | Web Hosting | This thread | Refback | 01-04-2008 06:25 PM |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| What is managed code and managed data? | H2o | ASP and ASP.NET Programming | 1 | 07-24-2007 03:34 AM |