473,405 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Problems reloading library. Dynamic pinvoke

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);
}
}
}
Nov 17 '05 #1
0 2464

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Michael Own | last post by:
Hi. I've upgraded my RH 8.0 with some packages from rawhide : httpd-2.0.47-4.i386.rpm httpd-devel-2.0.47-4.i386.rpm php-4.3.2-7.i386.rpm php-devel-4.3.2-7.i386.rpm It works fine, but i...
8
by: Aspersion | last post by:
I'm building an ASP page that has a lot of text and graphics. There is a calculation facility on the page. The user enters several numbers in a form and presses a button to see the calculated...
4
by: Jacob | last post by:
I have a page Add_data.asp which accepts a number of user-entered variables and then inserts them into a database. I have another page, Loc_tree.asp which when accessed, returns a tree of...
0
by: Mike P. | last post by:
Hi all, I'm working on a simulation (can be considered a game) in Python where I want to be able to dump the simulation state to a file and be able to load it up later. I have used the standard...
5
by: Leonardo D'Ippolito | last post by:
Hello all. The code bellow prints (in the command line window) the MAC ADDRESSES of all NIC cards enabled in the computer. What I need is: a library based on this code, that will return a...
11
by: Steve Smith | last post by:
I have written a winforms application that launches approximately 150 threads with Thread.ThreadStart() Each thread uses CDO 1.21 to logon to a different Exchange mailbox and send/receive a...
2
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres...
14
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain...
1
by: Marcus.CM | last post by:
Hi, I use the following ctype to load a .so library in Linux. vr = ctypes.CDLL(sstr) And the following to release it so that i can reload the library without quiting the python script. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.