开发者

.NET Portability Warning: CA1901 PInvoke Declarations Should Be Portable

开发者 https://www.devze.com 2023-03-22 06:57 出处:网络
When I add the following lines into my code [DllImport(\"user32.dll\")] static extern void keybd_event(byte key, byte scan, int flags, int extraInfo);

When I add the following lines into my code

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

and run a code analysis against Microsoft Basic Correctness Rules, I get a CA1901 warning. Basically, it complains the 4th parameter int extraInfo works fine on a 32-bit platform but a 64-bit integer type is expected on 64-bit platform.

When I modified the code into long extraInfo, the 64-bit platform requirement is met but the 32-bit platform is expecting a 32-bit integer.

How to solve th开发者_开发知识库is dilemma without suppressing the warning?


By using an IntPtr which is a platform-specific type that is used to represent a pointer or a handle:

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

精彩评论

暂无评论...
验证码 换一张
取 消