1

I'm working with the game Arma 3 where you can write your own Extensions (means a C++ or C# library). These Extensions have a defined entry point which is this DLL interface

void __stdcall RVExtension(char *output, int outputSize, const char  *function);
int __stdcall RVExtensionArgs(char *output, int outputSize, const char *function, const char **args, int argCnt);

When implementing this into C# the entry point for RVExtension would be

[DllExport("_RVExtension@12", CallingConvention = CallingConvention.Winapi)]
public static void RvExtension(StringBuilder output, int outputSize,
    [MarshalAs(UnmanagedType.LPStr)] string function)
{
    output.Append("Foo");
}

I'm using this library for the DLLExport.

As the entry point for RVExtensionArgs is quite new I wanted to implement it aswell. My solution so far is this one here:

[DllExport("_RVExtensionArgs@20", CallingConvention = CallingConvention.Winapi)]
public static int RvExtensionArgs(StringBuilder output, int outputSize,
    [MarshalAs(UnmanagedType.LPStr)] string function, [MarshalAs(UnmanagedType.LPArray)] string[] args,
        int argCount)
{
    output.Append("Foo");
}

In general this works however the array passed for the args argument is not converted properly. You only get the first element of the passed array.

I tried to define the LPArray SizeConst property but this has to be a fixed size I can't provide. The passed array elements can be up to 1024. Plus I got an MarshalException when accessing the DLL with my test console.

How can I fix this behaviour?

2
  • 1
    Not SizeConst, use SizeParamIndex instead. Commented Feb 20, 2017 at 13:22
  • Ok, well this was quite too easy. Will you post this as an answer, just for the completeness? Commented Feb 20, 2017 at 13:23

1 Answer 1

1

Just for the completeness, here is the solution as proposed by Hans Passant.

public static int RvExtensionArgs(StringBuilder output, int outputSize,
        [MarshalAs(UnmanagedType.LPStr)] string function,
        [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStr, SizeParamIndex = 4)] string[] args,
        int argCount)
{
    output.Append("Foo");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.