3

エラーはかなり一般的なようですが、ここにあります:

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'

コード行は次のとおりです。

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

今、私は64ビットと互換性のある、または両方で使用できるUintまたは何かに変更しようとしました(Pintまたは何か、名前を思い出せません)。

しかし、Int から Uint などに変更すると、次のコードが壊れます。

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);
}

それが言うように(intから変換できません...)そこにあるすべてのものに(uint)を使用すると「うまくいく」ようですが、それが最適な方法だとは思いません。

MouseEvent コードは次のとおりです。

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

また、それらを Uint に変更してみました。

今、私が Uint について話しているのは、それを変更する必要があることを読んだからです。Uint が Int と比べて何なのか、私にはまったくわかりません。

より良い方法がある場合、または私が間違っている場合は、教えてください。

4

1 に答える 1

3

元の宣言:

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

正しい C# 宣言 (可能なオプションの 1 つ):

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

最後のパラメーターが次のように宣言されている理由IntPtr:

本来の宣言ではポインタ型なので、64ビット処理だと8バイトになります。32 ビット プロセスの場合は 4 バイト、 64 ビット プロセスの場合IntPtrは 8 バイトです。AnyCPUx64mouse_event

を使用するたびに最後のパラメーターを (IntPtr) にキャストしたくない場合はmouse_event、それを行うオーバーロードを提供できます。

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

dwDataまた、 &dwExtraInfoパラメータに有効な値を提供しているとは思いません。ドキュメントに従っていることを確認してください: MSDN

于 2013-08-04T00:47:42.403 に答える