Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: How to call a method while forcing different marshalling?

A method declared in one of Microsoft's Primary Interop assemblies is wrong.

It is declared as:

void Write(object[] psarray);

which is incorrect, and should actually be declared as:

void Write([In, MarshalAs(UnmanagedType.SafeArray)] object[] psarray);

I need to force the compiler to use [In, MarshalAs(UnmanagedType.SafeArray)], when calling the original method.

I'm not opposed to doing something like:

object[] parameters = new Object[1];
parameters[0] = theStringIWantedToPass;

thing.GetType().InvokeMethod(
      "write", 
      BindingFlags.InvokeMethod,
      null,
      thing, 
      parameters);

But I have to know how to override InvokeMethod to use UnmanagedType.SafeArray marshalling.

Note: I say "like", since I don't know if using reflection to invoke a method can even accomplish what I need. The point was I'm not opposed to calling methods the long way, or loading registers and issuing assembly JMP instructions, as long as .NET allows that sort of thing.


Note: I cannot re-declare the class with a different signature, since that's then a different class.

like image 862
Ian Boyd Avatar asked Jul 04 '26 15:07

Ian Boyd


1 Answers

One way to overcome this shortcoming would be to disassemble the PIA, fix the problem and then rebuild.

It's not the first time either me or a buddy of mine have had to do this in the past with interop assemblies.

like image 89
Kev Avatar answered Jul 06 '26 05:07

Kev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!