Background:
I have written the following function in C++:
extern "C" __declspec(dllexport) int test(const char*, ...);
And I am using P/Invoke to call it from C#:
[DllImport("foo/bar.dll", EntryPoint = "test")]
public static unsafe extern int __unmanaged__test(byte* buffer, __arglist);
Problem:
I need to initialize the
__arglist dynamically, meaning that I have an Array<object>, which I have to convert into the __arglist before calling the C++-function.
I have tried the following, but it returns an compiler error:
unsafe {
byte[] buffer = ....
object[] args = ....
// ....
fixed (byte* ptr = buffer)
return __unmanaged__test(ptr, __arglist(args)); // <--ERROR
}
Does anyone know a way to solve this problem?
for those who do not know what
__arlist is: http://bartdesmet.net/blogs/bart/archive/2006/09/28/4473.aspx
You need to use the C-style calling convention - the others (including the default Stdecl) don't support varargs:
Another tricky part is that
__arglist(...)is a compile-time feature - it simply generates a (virtual) stack push for each of its "arguments". This means that you can't use it with arguments not known at compile-time - in your sample code, you're simply attempting to make a single argument, typedobject[].Building the helper reflection methods isn't too hard - see Calling varargs method via DynamicMethod for an example. I cannot test this, but I'd guess
Expressioncould be used to build this code easily - if you didn't have to pass thatbyte*, that is (do you really have to? passingbyte[]could work just as well).