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
"CreateDllAssem bly" 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 CreateDllAssemb ly
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 CreateDllAssemb ly
function.
Any ideas what I may be doing wrong please?
Dll connection code
=============
class DllWrapperClass
{
private Type m_tDDDll = null;
string DllName = "3rdpartydll.dl l";
private void CreateDllAssemb ly()
{
const string fnName = "CreateDllAssem bly";
LogDebug(fnName , "Creating DDDDll assembly");
// Create dynamic assembly
AssemblyName an = new AssemblyName();
an.Name = "DDDDllAssembly " + Guid.NewGuid(). ToString("N");
AssemblyBuilder ab = AppDomain.Curre ntDomain.Define DynamicAssembly (an,
AssemblyBuilder Access.Run);
// Add module to assembly
modBuilder = ab.DefineDynami cModule("DDDDll Module");
// Add module to assembly
TypeBuilder tb = modBuilder.Defi neType("DDDDll" +
Guid.NewGuid(). ToString("N"));
// Define parameters to be sent to dll
Type[] myType = {typeof(byte[])};
// Add PInvoke methods to class
MethodBuilder meb = tb.DefinePInvok eMethod("Open",
DllName,
MethodAttribute s.Public | MethodAttribute s.Static |
MethodAttribute s.PinvokeImpl,
CallingConventi ons.Standard,
typeof(int),
myType,
CallingConventi on.Cdecl,
CharSet.Auto);
// Apply preservesig metadata attribute so we can handle return integer
ourselves
meb.SetImplemen tationFlags(Met hodImplAttribut es.PreserveSig
| meb.GetMethodIm plementationFla gs());
meb = tb.DefinePInvok eMethod("GetPoi nts",
DllName,
MethodAttribute s.Public | MethodAttribute s.Static |
MethodAttribute s.PinvokeImpl,
CallingConventi ons.Standard,
typeof(int),
myType,
CallingConventi on.Cdecl,
CharSet.Auto);
// Apply preservesig metadata attribute so we can handle return integer
ourselves
meb.SetImplemen tationFlags(Met hodImplAttribut es.PreserveSig
| meb.GetMethodIm plementationFla gs());
m_tDDDll = tb.CreateType() ;
}
Disposal of object and dll
=============== ===
[DllImport("kern el32.dll")]
internal static extern IntPtr GetModuleHandle (string modulename);
[DllImport("kern el32.dll")]
internal static extern int FreeLibrary(Int Ptr handle);
public void Dispose()
{
const string fnName = "Dispose";
LogDebug(fnName , string.Format(" Base Dispose called for object created at
{0}",FirstCreat ed));
LastException = null;
modBuilder = null;
m_tDDDll = null;
// Unload the library from memory
IntPtr i = GetModuleHandle (DllName);
if (i.ToInt64() != 0)
{
FreeLibrary(i);
}
}
}