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

问题描述 投票:0回答:1

当我在Visual Studio项目中使用代码分析功能时,我会在CA1901类型的参数中获得ChildWindowFromPointExRealChildWindowFromPoint的意外POINT警告。

我刚刚定义了一个名为NativePoint的结构,其中X和Y字段为Int32类型,我将其用作那些POINT参数的等效项。

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

事实上,ChildWindowFromPoint和ChildWindowFromPointEx似乎只有一个额外的参数不同,两者都采用与参数相同的POINT结构,所以我不明白为什么ChildWindowFromPointEx警告可移植性问题,而ChildWindowFromPoint就是好的。

我的问题是:在C#或VB.NET中,如何正确修复(不抑制)ChildWindowFromPointEx和RealChildWindowFromPoint函数的此警告?也许我需要定义另一个具有不同字段类型的不同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;
}
c# .net vb.net winapi pinvoke
1个回答
1
投票

在负责的分析程序集中探讨之后,Windows API文档与分析插件使用的数据集之间存在不匹配。

该规则认为RealChildWindowFromPoint应该具有每4个字节的3个参数,并且x86上每4个字节的ChildWindowFromPointEx 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的规则缺少返回值大小和x64声明的数据,而ChildWindowFromPoint的数据已经完成,我认为这只是数据中的错误而不是故意推荐。

© www.soinside.com 2019 - 2024. All rights reserved.