为 ChildWindowFromPointEx 函数获取意外的 CA1901 警告

Getting unexpected CA1901 warning for ChildWindowFromPointEx function

当我在我的 Visual Studio 项目中使用代码分析功能时,我遇到了意外 CA1901 warning for ChildWindowFromPointEx and also for RealChildWindowFromPoint, at their parameters of type POINT

我刚刚定义了一个名为 NativePoint 的结构,其中包含 XY 字段 Int32 类型,我将其用作那些 POINT 参数的等价物。

我理解在 x86 或 x64 下 运行 时可移植性在值大小方面意味着什么,但是,在这种情况下,我不确定如何解决此警告,因为我使用的是相同的NativePoint 结构用于 POINT 其他函数的参数,例如:ChildWindowFromPoint, ClientToScreen, GetCursorPos 和许多其他函数,代码分析不会警告它们,并且它们在 x86 和 x64 进程上 运行 时工作。

事实上,ChildWindowFromPointChildWindowFromPointEx 似乎只有一个附加参数不同,两者采用相同的 POINT 结构作为参数,所以我不明白为什么 ChildWindowFromPointEx 警告可移植性问题,而 ChildWindowFromPoint 则一切正常。

我的问题是:在 C# 或 VB.NET 中,如何正确修复(而不是抑制)针对 ChildWindowFromPointExRealChildWindowFromPoint[= 的警告45=] 功能?。也许我需要定义另一个具有不同字段类型的 NativePoint 结构,仅用于这两个函数?但是为什么这两个函数抛出警告而 ChildWindowFromPoint 如果两者都采用相同的 POINT (我的 NativePoint) 结构?.

请注意,如果我使用 System.Drawing.Point 结构,我会收到与这两个函数相同的警告,但仅针对这两个函数。


[DllImport("user32.dll", EntryPoint="ChildWindowFromPointEx", SetLastError=false)]
public extern static IntPtr ChildWindowFromPointEx(IntPtr hwndParent,
                                              NativePoint point, 
                                                     uint flags);

[DllImport("user32.dll", EntryPoint="RealChildWindowFromPoint", SetLastError=false)]
public extern static IntPtr RealChildWindowFromPoint(IntPtr hwndParent, 
                                                NativePoint point);

[StructLayout(LayoutKind.Sequential)]
public struct NativePoint {
    public int X;
    public int Y;
}

在负责的分析程序集中查找后,Windows API 文档与分析插件使用的数据集不匹配。

规则认为 RealChildWindowFromPoint 每 4 个字节应该有 3 个参数,而 ChildWindowFromPointEx 在 x86 上每 4 个字节应该有 4 个参数。另一方面,ChildWindowFromPoint 被列为具有预期的一个 4 字节参数和一个 8 字节参数。

确实,这样声明RealChildWindowFromPoint似乎可以满足代码分析,但我对Windows API调用约定了解不够,无法说明这是否真的是一个调用该方法的有效方式:

[DllImport("user32.dll", EntryPoint = "RealChildWindowFromPoint", SetLastError = false)]
public static extern IntPtr RealChildWindowFromPoint(IntPtr hwndParent, int x, int y);

考虑到 RealChildWindowFromPointChildWindowFromPointEx 的规则缺少 return 值大小和 x64 声明的数据,而 ChildWindowFromPoint 的数据是完整的,我认为这只是数据错误,而不是有意推荐。