.NET portability warning: CA1901 PInvoke declarations must be portable

When I add the following lines to my code

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

and do some code analysis regarding the basic Microsoft correctness rules, I get warning CA1901. In principle, he complains that the 4th parameter int extraInfo works fine on a 32-bit platform, but it is expected that a 64-bit integer type is expected on a 64-bit platform.

When I modified the code in long extraInfo, the requirements for the 64-bit platform are satisfied, but the 32-bit platform expects a 32-bit integer.

How to solve this dilemma without suppressing a warning?

+6
source share
1 answer

Using IntPtr , which is a platform-specific type that is used to represent a pointer or handle:

 [DllImport("user32.dll")] static extern void keybd_event(byte key, byte scan, int flags, IntPtr extraInfo); 
+4
source

Source: https://habr.com/ru/post/893322/


All Articles