Home > Net >  How to release the Activator. The CreateInstance calling unmanaged DLL resources
How to release the Activator. The CreateInstance calling unmanaged DLL resources

Time:10-18

DLL for VB6 to write, I in the DLL's class defines a GetHwnd method returns the window handle, as follows, after the method returns a value, I already don't need the DLL's resources, but after I delete the DLL will be prompted to DLL file is occupied by a program, so could you please tell me you the great god, how can I release the Activator. The memory after CreateInstance? (GetType this DLL can also delete after execution of the sentence, after the CreateInstance before until the end of the program can delete)
The following code
 
Private IntPtr GetHwnd ()
{
IntPtr handle=0 (IntPtr);
Type obj=the GetTypeFromProgID (" CSharpDll. StartClass ");
The object ins=the Activator. CreateInstance (obj);

Try
{
The object result=obj. InvokeMember (" GetHwnd, "BindingFlags InvokeMethod, null, ins, new object [] {" DLL returned form", "000", ""});

Handle=(IntPtr) result;
}
The catch (Exception ex)
{
MessageBox. Show (" LoadDll () : "+ ex. Message);
}

Return (IntPtr) handle;
}

CodePudding user response:

The general method is unable to uninstall, and then delete the DLL
Only the AppDomain can be uninstalled
In which need to create a new AppDomain and then load the DLL, unloading, after using it to delete the DLL

CodePudding user response:

AppDomain. The CreateDomain create domain
Load the DLL
AppDomain. Unload discharge domain

CodePudding user response:

On the application domain loading and unloading application domain is still unable to delete, don't know that I write if you have any questions?
 class ProxyObject: MarshalByRefObject 
{
Int Handel=1;
Public void Loaddll ()
{
Type obj=the GetTypeFromProgID (" CSharpDll. StartClass ");
The object ins=the Activator. CreateInstance (obj);

Try
{
The object result=obj. InvokeMember (" GetHwnd, "BindingFlags InvokeMethod, null, ins, new object [] {" DLL returned form", "000", ""});
Handel=(Int32) result;
}
The catch (Exception ex)
{
MessageBox. Show (" LoadDll () : "+ ex. Message);
}

+ Handel MessageBox. Show (" ");
}
}

The following is called:
 AD=AppDomain. The CreateDomain (" DLL Unload test "); 
Obj=(ProxyObject) AD. CreateInstanceFromAndUnwrap (@ "WinFormDemo. Exe", "WinFormDemo. DllInvoke. ProxyObject");
Obj. Loaddll ();
AppDomain. Unload (AD);
Obj=null;

CodePudding user response:

reference 1st floor stherix response:
general method is to can't unload, and then delete the DLL
Only the AppDomain can be uninstalled
In which need to create a new AppDomain and then load the DLL, unloading, after using it to delete the DLL
thank eldest brother, I have used the application domain, but still not solve,

CodePudding user response:

refer to the second floor lazy lazy source reply:
AppDomain. The CreateDomain create domain
Load the DLL
AppDomain. Unload discharge domain
thank eldest brother, I have used the application domain, but still not solve,
  •  Tags:  
  • C #
  • Related