473,402 Members | 2,072 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,402 software developers and data experts.

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.DefinePInvokeMethod until I started implementing calls to
pointer (byref) parameters. The switch to TypeBuilder.DefinePInvokeMethod
(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.Name = "tempDll";
AssemblyBuilder assemblyBuilder =
AppDomain.CurrentDomain.DefineDynamicAssembly(asse mblyName,
AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder =
assemblyBuilder.DefineDynamicModule("tempModule");

// Dynamically construct a global PInvoke signature using the
input information
MethodBuilder methodBuilder =
moduleBuilder.DefinePInvokeMethod(entryPointName,

dllName,

MethodAttributes.Static | MethodAttributes.Public |
MethodAttributes.PinvokeImpl,

CallingConventions.Standard,

returnType,

parameterTypes,

CallingConvention.Winapi,

CharSet.Ansi);

// This global method is now complete

methodBuilder.SetImplementationFlags(MethodImplAtt ributes.PreserveSig);
moduleBuilder.CreateGlobalFunctions();

// Get a MethodInfo for the PInvoke method
MethodInfo methodInfo = moduleBuilder.GetMethod(entryPointName);

Object returnValue = new Object();

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

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

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

AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "tempDll";
AssemblyBuilder assemblyBuilder =
AppDomain.CurrentDomain.DefineDynamicAssembly(asse mblyName,
AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder =
assemblyBuilder.DefineDynamicModule("tempModule");

// Dynamically construct a global PInvoke signature using the
input information
TypeBuilder typeBuilder = moduleBuilder.DefineType("TempType");
MethodBuilder methodBuilder =
typeBuilder.DefinePInvokeMethod(entryPointName,

dllName,

MethodAttributes.Static | MethodAttributes.Public |
MethodAttributes.PinvokeImpl,

CallingConventions.Standard,

returnType,

parameterTypes,

CallingConvention.Winapi,

CharSet.Ansi);

// This global method is now complete

methodBuilder.SetImplementationFlags(MethodImplAtt ributes.PreserveSig);
moduleBuilder.CreateGlobalFunctions();

//...GetMethod returns nothing
MethodInfo methodInfo = moduleBuilder.GetMethod(entryPointName);

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.Invoke(null, parameterValues);

Jan 12 '06 #1
0 1671

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

Similar topics

0
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...
2
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...
2
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 ...
2
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...
9
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...
3
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...
1
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....
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.