> David, thanks for the sugestion. I tried your suggestion and it worked
> out. Thing is I'm back to the original problem. I can't unload a
[quoted text clipped - 33 lines]
> Any idea as why the module doesn't unload. I guess I create another
> thread for this new problem..
Yes.. Here is the code:
public class CLocalLoader : MarshalByRefObject
{
private AppDomain m_dynamicAppDomain = null;
private CRemoteLoader m_RemoteLoad = null;
public String strErrorMsg = "";
public CLocalLoader()
{
LoadUp();
}
public void Unload()
{
if (m_dynamicAppDomain != null)
{
AppDomain.Unload(m_dynamicAppDomain);
m_dynamicAppDomain = null;
}
if (m_RemoteLoad != null)
m_RemoteLoad = null;
}
public void LoadUp()
{
try
{
// Set up the AppDomainSetup
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationName = "MyTestApp";
setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
setup.PrivateBinPath = AppDomain.CurrentDomain.BaseDirectory;
m_dynamicAppDomain = AppDomain.CreateDomain("MySecondDomain",
null, setup);
m_RemoteLoad = (CRemoteLoader)
m_dynamicAppDomain.CreateInstanceAndUnwrap("InvokeVadminHook",
"InvokeTest.CRemoteLoader");
}
catch (Exception pError)
{
strErrorMsg = pError.Message;
}
}
public object DynamicPInvoke(string dll, string entryPoint, Type
returnType, Type [] parameterTypes, object [] parameterValues)
{
if (m_RemoteLoad != null )
m_RemoteLoad.CallDynaMethod("tempAssembly", dll, entryPoint,
returnType, parameterTypes, parameterValues);
return null;
}
}
//=============================================================================
/// <summary>This class is used to </summary>
public class CRemoteLoader : MarshalByRefObject
{
String strErrorMsg = "";
public CRemoteLoader()
{
}
public void CallDynaMethod(String strAssemblyName, string dll,
string entryPoint, Type returnType, Type [] parameterTypes, object []
parameterValues)
{
try
{
AssemblyName asmName = new AssemblyName();
asmName.Name = strAssemblyName;
AssemblyBuilder dynamicAsm =
AppDomain.CurrentDomain.DefineDynamicAssembly(asmName,
AssemblyBuilderAccess.Run);
//Create a temp module in the assembly
ModuleBuilder dynamicMod = dynamicAsm.DefineDynamicModule(
"tempModule");
// Dynamically construct a global PInvoke signature using the
input information
MethodBuilder dynamicMethod =
dynamicMod.DefinePInvokeMethod(entryPoint,
dll,
MethodAttributes.Static | MethodAttributes.Public |
MethodAttributes.PinvokeImpl,
CallingConventions.Standard,
returnType,
parameterTypes,
CallingConvention.Winapi,
CharSet.Ansi);
// This global method is now complete
dynamicMod.CreateGlobalFunctions();
// Get a MethodInfo for the PInvoke method
MethodInfo mi = dynamicMod.GetMethod(entryPoint);
// Invoke the static method and return whatever it returns
Object pRetObj = mi.Invoke(null, parameterValues);
}
catch (Exception pError)
{
strErrorMsg = pError.Message;
throw pError;
}
}
}