Hi
Ive been using Mattias Sjögren's example at
http://www.msjogren.net/dotnet/eng/s...dynpinvoke.asp to load an
unmanaged 3rd party dll dynamically when my object is created. Calling the
"CreateDllAssembly" function below does this.
When my wrapper is disposed of, I want the dll to be unloaded from memory.
See the "dispose" function below.
All seems to work ok on the first instance of my wrapper class. (I only
create a single instance of the wrapper at any one time)
I destroy the wrapper, then I create another one. This is where something
goes wrong. There appears to be no problems in the CreateDllAssembly
function, but I receive exceptions when I attempt to call the Dll's
functions. At disposal, calling GetModuleHandle(), i is returned as
zero/null.
It looks as though the library is not being loaded by the CreateDllAssembly
function.
Any ideas what I may be doing wrong please?
Dll connection code
=============
class DllWrapperClass
{
private Type m_tDDDll = null;
string DllName = "3rdpartydll.dll";
private void CreateDllAssembly()
{
const string fnName = "CreateDllAssembly";
LogDebug(fnName, "Creating DDDDll assembly");
// Create dynamic assembly
AssemblyName an = new AssemblyName();
an.Name = "DDDDllAssembly" + Guid.NewGuid().ToString("N");
AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(an,
AssemblyBuilderAccess.Run);
// Add module to assembly
modBuilder = ab.DefineDynamicModule("DDDDllModule");
// Add module to assembly
TypeBuilder tb = modBuilder.DefineType("DDDDll" +
Guid.NewGuid().ToString("N"));
// Define parameters to be sent to dll
Type[] myType = {typeof(byte[])};
// Add PInvoke methods to class
MethodBuilder meb = tb.DefinePInvokeMethod("Open",
DllName,
MethodAttributes.Public | MethodAttributes.Static |
MethodAttributes.PinvokeImpl,
CallingConventions.Standard,
typeof(int),
myType,
CallingConvention.Cdecl,
CharSet.Auto);
// Apply preservesig metadata attribute so we can handle return integer
ourselves
meb.SetImplementationFlags(MethodImplAttributes.Pr eserveSig
| meb.GetMethodImplementationFlags());
meb = tb.DefinePInvokeMethod("GetPoints",
DllName,
MethodAttributes.Public | MethodAttributes.Static |
MethodAttributes.PinvokeImpl,
CallingConventions.Standard,
typeof(int),
myType,
CallingConvention.Cdecl,
CharSet.Auto);
// Apply preservesig metadata attribute so we can handle return integer
ourselves
meb.SetImplementationFlags(MethodImplAttributes.Pr eserveSig
| meb.GetMethodImplementationFlags());
m_tDDDll = tb.CreateType();
}
Disposal of object and dll
==================
[DllImport("kernel32.dll")]
internal static extern IntPtr GetModuleHandle(string modulename);
[DllImport("kernel32.dll")]
internal static extern int FreeLibrary(IntPtr handle);
public void Dispose()
{
const string fnName = "Dispose";
LogDebug(fnName, string.Format("Base Dispose called for object created at
{0}",FirstCreated));
LastException = null;
modBuilder = null;
m_tDDDll = null;
// Unload the library from memory
IntPtr i = GetModuleHandle(DllName);
if (i.ToInt64() != 0)
{
FreeLibrary(i);
}
}
}