473,734 Members | 2,824 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Moving from Module Builder to Type Builder

Hi
I need to call various functions in a native C++ DLL (FORTRAN eventually
too) - passing integers, doubles, (pointers and arrays to both of these as
well) and ultimately structures too. I was quite happily proceeding using
ModuleBuilder.D efinePInvokeMet hod until I started implementing calls to
pointer (byref) parameters. The switch to TypeBuilder.Def inePInvokeMetho d
(the one with the required custom modifiers) has resulted in the message
"the invoked member is not supported in a dynamic module".

Please could someone step me through the required calls in order to create a
totally dynamic call to such a DLL using the above approach. I don't really
want to save temporary assemblies to disk, but will if that is what it
required.

The sample below shows the module builder approach (which works) and then
the switch to the type builder approach which I am having problems with
(note: uses simple form of the method). The C++ function being called is
declared as follows:

extern "C"
{
__declspec( dllexport ) double WINAPI AddTwoDoubles2( double a, double b)
{
return a+b;

}
}
Thanks for any help anyone can provide.

Marek

C# code using module builder follows (this works
fine)********** *************** *************** ***
Type returnType = typeof(double);

//...Set up the parameters:
Type[] parameterTypes = new Type[2];
object[] parameterValues = new object[2];

parameterTypes[0] = typeof(double);
parameterValues[0] = 2;

parameterTypes[1] = typeof(double);
parameterValues[1] = 2;

// Create a dynamic assembly and a dynamic module
AssemblyName assemblyName = new AssemblyName();
assemblyName.Na me = "tempDll";
AssemblyBuilder assemblyBuilder =
AppDomain.Curre ntDomain.Define DynamicAssembly (assemblyName,
AssemblyBuilder Access.Run);
ModuleBuilder moduleBuilder =
assemblyBuilder .DefineDynamicM odule("tempModu le");

// Dynamically construct a global PInvoke signature using the
input information
MethodBuilder methodBuilder =
moduleBuilder.D efinePInvokeMet hod(entryPointN ame,

dllName,

MethodAttribute s.Static | MethodAttribute s.Public |
MethodAttribute s.PinvokeImpl,

CallingConventi ons.Standard,

returnType,

parameterTypes,

CallingConventi on.Winapi,

CharSet.Ansi);

// This global method is now complete

methodBuilder.S etImplementatio nFlags(MethodIm plAttributes.Pr eserveSig);
moduleBuilder.C reateGlobalFunc tions();

// Get a MethodInfo for the PInvoke method
MethodInfo methodInfo = moduleBuilder.G etMethod(entryP ointName);

Object returnValue = new Object();

try
{
returnValue = methodInfo.Invo ke(null, parameterValues );
MessageBox.Show (String.Format( "Value calculated: {0}",
Convert.ToDoubl e(returnValue). ToString()), entryPointName) ;
}
catch (Exception ex)
{
MessageBox.Show (ex.Message, entryPointName) ;

if (ex.InnerExcept ion != null)
MessageBox.Show (ex.InnerExcept ion.Message);
}

C# code using type builder follows (this doesn't
work)********** *************** *************** ***

AssemblyName assemblyName = new AssemblyName();
assemblyName.Na me = "tempDll";
AssemblyBuilder assemblyBuilder =
AppDomain.Curre ntDomain.Define DynamicAssembly (assemblyName,
AssemblyBuilder Access.Run);
ModuleBuilder moduleBuilder =
assemblyBuilder .DefineDynamicM odule("tempModu le");

// Dynamically construct a global PInvoke signature using the
input information
TypeBuilder typeBuilder = moduleBuilder.D efineType("Temp Type");
MethodBuilder methodBuilder =
typeBuilder.Def inePInvokeMetho d(entryPointNam e,

dllName,

MethodAttribute s.Static | MethodAttribute s.Public |
MethodAttribute s.PinvokeImpl,

CallingConventi ons.Standard,

returnType,

parameterTypes,

CallingConventi on.Winapi,

CharSet.Ansi);

// This global method is now complete

methodBuilder.S etImplementatio nFlags(MethodIm plAttributes.Pr eserveSig);
moduleBuilder.C reateGlobalFunc tions();

//...GetMethod returns nothing
MethodInfo methodInfo = moduleBuilder.G etMethod(entryP ointName);

Object returnValue = new Object();

try
{
// Calling method builder directly results in the exception
"The invoked member is not supported in a dynamic module"
returnValue = methodBuilder.I nvoke(null, parameterValues );

Jan 12 '06 #1
0 1706

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

Similar topics

0
1703
by: jenniferyiu | last post by:
Hi. I downloaded the bean builder and followed the steps told on http://java.sun.com/products/javabeans/beanbuilder/1.0/docs/guide/tutorial.html and the sample tools was created as the above link told. I guess the bean builder can let the developers do something without writing
2
3487
by: qazmlp | last post by:
Builder pattern seem to suit my requirements perfectly except in one case as described below. I have used the same example diagram that is used in GOF Design pattern book, to explain the problem that I am facing. aClient aDirector aConcreteBuilder =============================================================================
2
3240
by: 63q2o4i02 | last post by:
Hi, I'm using python 2.4 and windows XP. I have two packages in the windows version of python in site-packages. They are PyVisa and ctypes, and both live in c:\python24\lib\site-packages I'd like to move these to the cygwin version of python on the same system. I tried copying the PyVisa and ctypes directorices (including
2
2299
by: Mike | last post by:
Hello NG, i am just learning various Design Patterns and now i am not sure, if this design is correct (Builder) or if i should use an other pattern. I have various classes (here ChildA and ChildB) derived from class Base. Now i want to create an object, but i don't want to know which class to instantiate.
9
3101
by: axs221 | last post by:
I am trying to move some of our large VBA Access front-end file into ActiveX DLL files. I created two DLL files so far, one was a module that contains code to integrate into the QuickBooks accounting software. Another has general utilities. I tried referencing the utilities dll, and it shows up in the object explorer. I instantiated an instance of the class and now it shows up all okay in the Intellisense. Whenever I try to run a simple...
3
3758
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and thawed it out. I built a console app using Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the header file had to be commented out. I built a console app using Borland C++ Builder 5. The linker complained of references to...
1
1530
by: Shaolin | last post by:
Hi guys I;m trying to move all my Listview content into a module because I have to call it several times and it becomes annoying having to type it a number of times. I'm a little stuck though. I've managed to move most of the listview code onto a module but I keep getting errors. See the code below: Public Sub AddItem(lvwListView1 as listview) ConnectDB Dim lstNew As ListItem Set rs = New ADODB.Recordset rs.Open "SELECT * From...
0
4639
by: SuzK | last post by:
I am trying to calculate in VBA in Access 2002 a moving average and update a table with the calculations. Fields in my WeeklyData table are Week Ending (date) ItemNbr (double) Sales Dollars (double) Sales Units (double) Promo (Text) -- is null or "X" AvgWklyDollars (double) AvgWklyUnits (double) I have a vba module which I thought would work, but it doesn't. I think the problem is an embedded SQL Totals Top 8 query, which doesn't...
0
8946
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
8777
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9184
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8187
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
6737
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
4813
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3262
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
2
2729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.