473,657 Members | 2,586 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

Similar topics

0
1903
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 cannot load any PHP dynamic library. I get errors
8
4005
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 answer. The problem is this: when the user presses the Calculate button, the whole page is reloaded and, on a large page, this is very noticeable. Is there any way that I can get the calculation done and the result displayed without reloading...
4
2897
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 locations which is dynamically generated by the server. The Loc_tree.asp creates a hyperlink for each object in the tree. I need to show the Location Tree in a separate window, and then when a location is clicked, I need to have some javascript code in...
0
1776
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 Python pickle module and it works fine pickling/unpickling from files. However, I want to be able to use a third party tool like an XML editor (or other custom tool) to setup the initial state of the simulation, so I have been playing around...
5
3414
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 string with the output of this command line tool. I need to use this library in my C# (.NET) program. In other words, convert this native .EXE in a library that I could use in my managed C# application.
11
2191
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 number of mail messages, reporting back to the UI thread through the use of a Queue object. When all messages that are expected have been received, each thread sends a final update to the UI and the method should exit, which should terminate the...
2
3269
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 and arrays of pointers to them. I have gotten the program to compile with gcc on WinXP. If the file i read doesnt have alot of records, it runs thru. But once i add more, it dies. In this program i have 4 files setup to read. The
14
3776
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 more why C++/CLI would be better to PInvoke than doing the PInvoke in C#? Because, usually in C# as you already know we use DLLImport and extern
1
2732
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. _ctypes.dlclose(vr._handle)
0
8392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7321
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.