Zerowalker
Zerowalker

Reputation: 767

P/Invoke - Int 4 byte, tried changing to UInt but it causes problems

The erros seems pretty common, but here it is:

eCA1901 P/Invoke declarations should be portable    As it is declared in your code, parameter 'dwExtraInfo' of P/Invoke 'NativeMethods.mouse_event(int, int, int, int, int)' will be 4 bytes wide on 64-bit platforms. This is not correct, as the actual native declaration of this API indicates it should be 8 bytes wide on 64-bit platforms. Consult the MSDN Platform SDK documentation for help determining what data type should be used instead of 'int'

Here is the line of code:

[System.Runtime.InteropServices.DllImport("user32.dll")]
internal static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

Now i have tried changin to Uint or soemthing that is compatible with 64bit, or that can be used on both (Pint or something, can´t remember the name).

But if i change from Int to Uint or whatever, it breaks this code:

if (click == "Left")
{
    NativeMethods.mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, MousePosition.X, MousePosition.Y, MousePosition.X, MousePosition.Y);
}
if (click == "Right")
{
    NativeMethods.mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, MousePosition.X, MousePosition.Y, MousePosition.X, MousePosition.Y);
}
if (down == "Left"+"True")
{
    NativeMethods.mouse_event(MOUSEEVENTF_LEFTDOWN , MousePosition.X, MousePosition.Y, MousePosition.X, MousePosition.Y);
}
if (down == "Right"+"True")
{
    NativeMethods.mouse_event(MOUSEEVENTF_RIGHTDOWN, MousePosition.X, MousePosition.Y, MousePosition.X, MousePosition.Y);
}

As it says (can´t convert from int...) It seems to "work" if i use (uint) on everything there, but i don´t think that´s a very optimal way to do it.

here are the MouseEvent codes:

private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;

Also tried changing them to Uint.

Now why i am going on about Uint is because i read that i should change it to that. I have no real clue what Uint is compared to Int.

So if there is a better way, or i am doing it wrong, please tell.

Upvotes: 3

Views: 1071

Answers (1)

max
max

Reputation: 34417

Original declaration:

VOID WINAPI mouse_event(
  _In_  DWORD dwFlags,
  _In_  DWORD dx,
  _In_  DWORD dy,
  _In_  DWORD dwData,
  _In_  ULONG_PTR dwExtraInfo
);

Correct C# declaration (one of possible options):

[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern void mouse_event(
    int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo);

Why last parameter is declared as IntPtr:

Because it is a pointer type in original declaration and it will be 8 bytes in case of 64-bit process. IntPtr is 4 bytes for 32-bit process and 8 bytes for 64-bit processes, which means if you want to compile your assembly as AnyCPU or x64 your mouse_event code stays the same.

If you don't want to cast last parameter to (IntPtr) every time you use mouse_event, you can provide an overload which does that:

static void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo)
{
    mouse_event(dwFlags, dx, dy, dwData, (IntPtr)dwExtraInfo);
}

Also, I don't think you are providing valid values for dwData & dwExtraInfo parameters. Make sure that you follow documentation: MSDN

Upvotes: 3

Related Questions