Mixed byVal and byRef parameters in DefinePInvokeMethod

M

Matthias Klöpper

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
 
M

Matthias Klöpper

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top