IT Community - Software Programming, Web Development and Technical Support

How to unload both Managed and Unmanaged DLL?

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...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 07-21-2007, 12:27 AM
theone theone is offline
D-Web Sr.Programmer
 
Join Date: Jun 2007
Posts: 129
theone is on a distinguished road
Question How to unload both Managed and Unmanaged DLL?

How to unload both Managed and Unmanaged DLL
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-21-2007, 01:34 AM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 633
oxygen is on a distinguished road
Default Re: How to unload both Managed and Unmanaged DLL?

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);
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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


All times are GMT -7. The time now is 10:39 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0