.NET可移植性警告:CA1901 PInvoke声明应该是可移植的

Jac*_*cob 6 .net pinvoke code-analysis portable-applications

当我在代码中添加以下行时

[DllImport("user32.dll")]
static extern void keybd_event(byte key, byte scan, int flags, int extraInfo);
Run Code Online (Sandbox Code Playgroud)

并针对Microsoft Basic Correctness Rules运行代码分析,我收到CA1901警告.基本上,它抱怨第4个参数int extraInfo在32位平台上工作正常,但在64位平台上预计会有64位整数类型.

当我将代码修改为long extraInfo时,满足64位平台要求,但32位平台需要32位整数.

如何在不压制警告的情况下解决这一难题?

Dar*_*rov 4

通过使用IntPtr,它是特定于平台的类型,用于表示指针或句柄:

[DllImport("user32.dll")]
static extern void keybd_event(byte key, byte scan, int flags, IntPtr extraInfo);
Run Code Online (Sandbox Code Playgroud)