0

I found some question about calling WinAPI functions from C# via DllImport, which I couldn't understand. So, initially I work with fucntion SetConsoleCursorPosition, but I think that question is some broader than about one concrete function. But it is a great example. Its unmanaged signature looks like:

BOOL WINAPI SetConsoleCursorPosition(  _In_ HANDLE hConsoleOutput,  _In_ SomeStruct  dwCursorPosition);

where SomeStruct is:

typedef struct _SomeStrcut { SHORT a; SHORT b; } SomeStruct;

In C#:

[DllImport("kernel32.dll", EntryPoint="SetConsoleCursorPosition", SetLastError = true)]
public static extern bool SetData(IntPtr screenHandle, SomeStruct ab);

[StructLayout(LayoutKind.Explicit)] //Sequential always works (size 4 bytes).
public struct SomeStruct
{
    [FieldOffset(0)] public ushort A;

    [FieldOffset(2)]public ushort B;

    public SomeStruct(ushort a, ushort b) { this.A = a; this.B = b; }
}

It works fine but I decide to replace SomeStruct with two ushort parameters for usability. Two short integer have same size in 4 bytes like one SomeStruct - and there is no difference, I thought:

[DllImport("kernel32.dll", EntryPoint = "SetConsoleCursorPosition", SetLastError = true)]
public static extern bool SetData(IntPtr screenHandle, ushort a, ushort b);

But function results with false and 87 error code via GetLastError - ErrorInvalidParameter. I tested some expirements with different signatures and they surprise me. With int/uint all works:

[DllImport("kernel32.dll", EntryPoint = "SetConsoleCursorPosition", SetLastError = true)]
public static extern bool SetData(IntPtr screenHandle, int ab);

Lower 2 bytes - a, upper 2 bytes - b. Perfectly. All my experiments code:

public static class Question
{
    [DllImport("kernel32.dll")]
    public static extern IntPtr GetStdHandle(int val);

    /// <summary>Initial declaration as in documentation</summary>
    [DllImport("kernel32.dll", EntryPoint="SetConsoleCursorPosition", SetLastError = true)]
    public static extern bool SetData(IntPtr screenHandle, SomeStruct ab);

    [DllImport("kernel32.dll", EntryPoint = "SetConsoleCursorPosition", SetLastError = true)]
    public static extern bool SetData(IntPtr screenHandle, int ab);

    [DllImport("kernel32.dll", EntryPoint = "SetConsoleCursorPosition", SetLastError = true)]
    public static extern bool SetData(IntPtr screenHandle, int ab, int someInt);

    [DllImport("kernel32.dll", EntryPoint = "SetConsoleCursorPosition", SetLastError = true)]
    public static extern bool SetData(IntPtr screenHandle, ulong ab);

    [DllImport("kernel32.dll", EntryPoint = "SetConsoleCursorPosition", SetLastError = true)]
    public static extern bool SetData(IntPtr screenHandle, ushort a, ushort b);

    [DllImport("kernel32.dll", EntryPoint = "SetConsoleCursorPosition", SetLastError = true)]
    public static extern bool SetData(IntPtr screenHandle, byte b1, byte b2, byte b3, byte b4);

    [DllImport("kernel32.dll", EntryPoint = "SetConsoleCursorPosition", SetLastError = true)]
    public static extern bool SetData(IntPtr screenHandle, byte b1, byte b2, byte b3, byte b4, uint some);

    [DllImport("kernel32.dll", EntryPoint = "SetConsoleCursorPosition", SetLastError = true)]
    public static extern bool SetData(IntPtr screenHandle, ushort a, uint b);

    [StructLayout(LayoutKind.Sequential)]
    public struct SomeStruct
    {
        [FieldOffset(0)] public ushort A;

        [FieldOffset(2)]public ushort B;

        public SomeStruct(ushort a, ushort b) { this.A = a; this.B = b; }
    }

    public static void Test()
    {
        IntPtr handle = GetStdHandle(-11); string[] testResults = new string[10]; int i = 0;

        //Test 1: Struct - Works
        testResults[i++] = string.Format("Result: {0} Err: {1}", SetData(handle, new SomeStruct(5, 1)), Marshal.GetLastWin32Error());

        //Test 2: int or uint -> Works (lower two bytes - A, upper two bytes - B)
        testResults[i++] = string.Format("Result Int: {0} Err: {1}", SetData(handle, 5 | (1 << 16)), Marshal.GetLastWin32Error()); Console.Write('I');

        //Test 3: ulong -> Works (lower two bytes - A, next two bytes - B)
        testResults[i++] = string.Format("Result Ulong: {0} Err: {1}", SetData(handle, (ulong)(9 | (2 << 16))), Marshal.GetLastWin32Error()); Console.Write('U');

        //Test 4: int/int -> Works (lower two bytes - A, upper two bytes - B), next int - not used at all
        testResults[i++] = string.Format("Result Int/Int: {0} Err: {1}", SetData(handle, 2 | (3 << 16), 2789), Marshal.GetLastWin32Error()); Console.Write("II");

        //Test 5: ushort/ushort -> not works (Error 87 - Invalid Parameter)
        testResults[i++] = string.Format("Result Ushort/Ushort: {0} Err: {1}", SetData(handle, (ushort)5, (ushort)1), Marshal.GetLastWin32Error());

        //Test 6: 4 bytes -> not works (Error 87 - Invalid Parameter)
        testResults[i++] = string.Format("Result 4 bytes: {0} Err: {1}", SetData(handle, 5, 0, 1, 0), Marshal.GetLastWin32Error());

        //Test 7: 4 bytes and int -> WORKS?? How it works??
        testResults[i++] = string.Format("Result 4 bytes Int: {0} Err: {1}", SetData(handle, 5, 0, 1, 0, 7 | (4 << 16)), Marshal.GetLastWin32Error()); 
        Console.Write("4bI");

        //Test 8: ushort and uint -> not works
        testResults[i++] = string.Format("Result ushort/uint {0} Err: {1}", SetData(handle, (ushort)12897, (uint)(2 | (2 << 16))), Marshal.GetLastWin32Error()); 

        //Printing all results on console
        SetData(handle, new SomeStruct(0, 5));
        //Size of SomeStruct: 4 bytes
        Console.WriteLine("SomeStructSize: {0}", Marshal.SizeOf(typeof(SomeStruct)));
        for (int j = 0; j < i; j++) Console.WriteLine(testResults[j]);
    }
}

Output is:

     I
         U
  II
     4bI
SomeStructSize: 4
Result: True Err: 2
Result Int: True Err: 2
Result Ulong: True Err: 0
Result Int/Int: True Err: 0
Result Ushort/Ushort: False Err: 87
Result 4 bytes: False Err: 87
Result 4 bytes Int: True Err: 87
Result ushort/uint False Err: 87

Now I'm a lot confused. I understand only what I don't understand what is going on with calling native code. Why function works with int/uint and does not work with 2 ushort or 4 bytes?? How it works with 4 bytes and int and why??? Please, explain me. Thanks!

Tadeusz
  • 6,453
  • 9
  • 40
  • 58
  • The return value of a method is put in the AX register while parameters are push on the execution stack. – jdweng Mar 01 '21 at 09:11
  • 1
    Apparently you are compiling for x64, where the first four parameters [go to different registers](https://stackoverflow.com/q/4429398/11683). – GSerg Mar 01 '21 at 09:30
  • Short answer: Padding – Charlieface Mar 01 '21 at 09:33
  • Yes GSerg - on x64 machine. I now what to read...Thanks! – Tadeusz Mar 01 '21 at 09:41
  • @GSerg I have read about x64-calling conventions, but It not describes all cases. Why not works with one ushort value? No difference between types of sizes below 8 bytes - all of them go throw RDX registry. Calle can't know that it is ushort or uint..or not? – Tadeusz Mar 01 '21 at 11:07
  • The first ushort goes into rdx, the second into r8. – GSerg Mar 01 '21 at 13:48

0 Answers0