473,386 Members | 1,598 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,386 software developers and data experts.

Mixed byVal and byRef parameters in DefinePInvokeMethod

Hi,
I have a little problem with supplying a mixed set of byVal and byRef
parameters to a dynamically created PInvoke method.
I have the following message signature which works perfectly when created
via DllImport:

int CT_data(ushort tn, ref byte sad, ref byte dad, ushort cl, byte[] c, ref
ushort rl, byte[] r);

For dynamic creation of used the following calls:

// ------------- CODE START------------------
ModuleBuilder module // pre-initialized with a dynamic module
object[] parameters;

parameters = new object[] {typeof(ushort), Type.GetType("System.Byte&"),
Type.GetType("System.Byte"), typeof(ushort), typeof(byte[]),
Type.GetType("System.Int16&"), typeof(byte[])};
MethodBuilder method = module.DefinePInvokeMethod("CT_data", "test.dll",
MethodAttributes.PinvokeImpl | MethoedAttributes.Public |
MethodAttributes.Static, CallingConventions.Standard, typeof(int),
parameters, CallingCOnvention.Winapi, CharSet.Auto);
// ------------- CODE END --------------------

Up to here everything is absolutely fine. The problems occur when I try to
invoke the method. I tried the following:

// --------------- CODE START ------------------
MethodInfo method; //initialized to my dynamic method declared above
object[] parameters;

// first try
// This produces an error "objecttype could not be casted to targettype"
(sorry, not the exact message, I translated from german)
parameters = new object[] {(ushort) 1, (byte) 2, (byte) 3, (ushort) 4, new
byte[4], (ushort) 5, new byte[5])};

// second try
// This produces an error "byte* can not be casted to object"
parameters = new object[] {(ushort) 0, (byte*) 2, (byte* 3), (ushort) 4, new
byte[4], (ushort*) 5, new byte[5])};

method.Invoke(null, parameters);
// ----------------- CODE END ----------------
Any ideas on how to fill the object-array with the correct method
attributes?

Greetings,
Matthias
Nov 15 '05 #1
1 3222
Sorry. I found a little typo in my code. 3rd Parameter with
DefinePInvokeMethod missed the "&". Here is the corrected version. Question
still is the same.
Hi,
I have a little problem with supplying a mixed set of byVal and byRef
parameters to a dynamically created PInvoke method.
I have the following message signature which works perfectly when created
via DllImport:

int CT_data(ushort tn, ref byte sad, ref byte dad, ushort cl, byte[] c, ref ushort rl, byte[] r);

For dynamic creation of used the following calls:

// ------------- CODE START------------------
ModuleBuilder module // pre-initialized with a dynamic module
object[] parameters;

parameters = new object[] {typeof(ushort), Type.GetType("System.Byte&"),
Type.GetType("System.Byte&"), typeof(ushort), typeof(byte[]),
Type.GetType("System.Int16&"), typeof(byte[])};
MethodBuilder method = module.DefinePInvokeMethod("CT_data", "test.dll",
MethodAttributes.PinvokeImpl | MethoedAttributes.Public |
MethodAttributes.Static, CallingConventions.Standard, typeof(int),
parameters, CallingCOnvention.Winapi, CharSet.Auto);
// ------------- CODE END --------------------

Up to here everything is absolutely fine. The problems occur when I try to
invoke the method. I tried the following:

// --------------- CODE START ------------------
MethodInfo method; //initialized to my dynamic method declared above
object[] parameters;

// first try
// This produces an error "objecttype could not be casted to targettype"
(sorry, not the exact message, I translated from german)
parameters = new object[] {(ushort) 1, (byte) 2, (byte) 3, (ushort) 4, new
byte[4], (ushort) 5, new byte[5])};

// second try
// This produces an error "byte* can not be casted to object"
parameters = new object[] {(ushort) 0, (byte*) 2, (byte* 3), (ushort) 4, new byte[4], (ushort*) 5, new byte[5])};

method.Invoke(null, parameters);
// ----------------- CODE END ----------------
Any ideas on how to fill the object-array with the correct method
attributes?

Greetings,
Matthias

Nov 15 '05 #2

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

Similar topics

8
by: Sandy | last post by:
Hello! Help!!!! I have ten zillion books that attempt to describe the difference between ByVal and ByRef and none of them are clear to me. I have gathered that ByVal makes a copy and ByRef...
3
by: william | last post by:
Hi everyone, I just started learning VB.NET, I found there are a lots methods passing object parameter by value. For example, Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As...
7
by: Hei | last post by:
Hi, i know the difference of ByRef and ByVal, in case if use byref or byval don't affect the result which one should prefer? (less memory use, better performance ....issue) thx
19
by: Rob Panosh | last post by:
Hello, Ok here is the senerio: ..... Dim myArrayList as New ArrayList(0) me.Test_A( myArrayList )
4
by: Carlos Gomez | last post by:
In VB6 the default for passing variables was ByRef. It was faster and used less memory. Why did MS changed that? Are there any advantages using ByVal over ByRef? (other than ByVal impeding you from...
16
by: Richard | last post by:
Hi, I am passing a structure to a subroutine where the passed parameter has been declared as ByVal. However, changes made to the passed variable inside the subroutine flow through to the...
14
by: Niklas | last post by:
Hi What I have learned is that a variable is just a reference when dealing with Objects. Are you supposed to use ByVal or ByRef in functions? They produce the same result or have I missed...
4
by: Warren Sirota | last post by:
Hi, Please let me know if I am interpreting this correctly. I've done a little testing of the difference between passing parameters byVal and byRef, and the results were slightly non-intuitive,...
3
by: JB | last post by:
Hi All, I've discovered a strange behaviour with Object parameters passed ByVal via remoting and I'm wondering if anybody could shed some light on this. In a non remoting function call, when...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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,...
0
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,...

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.