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
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