I am trying to call an unmanaged dll. While looking for information about this and trying it, I thought I could use params object[] instead of __arglist , so I changed it like the following code, but got different results. Why does this work differently?
using System.Runtime.InteropServices;
namespace ConsoleApp1
{
internal class Program
{
[DllImport("msvcrt.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
internal static extern int printf(string format, __arglist);
[DllImport("msvcrt.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
internal static extern int printf(string format, params object[] i);
static void Main(string[] args)
{
printf("Print Integer: %d\n", __arglist(10)); // Print Integer: 10
printf("Print Integer: %d\n", 10); // Print Integer: 1363904384
}
}
}
Because they're not the same thing.
__arglistis specifically and only for var args in unmanaged code. The params keyword is something else entirely and the generated code just builds an array for you from the list of parameters. All it is doing is allowing you to writeMyFunc(p1,p2,p3)instead ofMyFunc(new object [] { p1, p2, p3}). On your second example, that number is probably the address of the parameter array passed to printf.