繁体   English   中英

GetWindowRect返回NullReferenceException

[英]GetWindowRect returns NullReferenceException

我正在尝试获取附加到javaw.exe进程的特定窗口的大小和位置。

可悲的是,GetWindowRect引发错误:“ NullReferenceException”-我已经检查过,它都不是参数== null。

这是一段代码

调用样本:

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool GetWindowRect(IntPtr handle, out WindowRect rect);
    [StructLayout(LayoutKind.Sequential)]
    private class WindowRect
    {
        public int Left;
        public int Top;
        public int Right;
        public int Bottom;
    }

运行静态函数来附加进程:

NB.Attach( Process.GetProcessesByName("javaw")[0] );

用法样本:

    public static void Attach( Process process )
    {
        FocusProcess = process;
        FocusWindow = FindWindow(null, process.MainWindowTitle);
    }

    public static int[] GetWindowPosition()
    {
        WindowRect rect = new WindowRect();

        Console.WriteLine(FocusProcess == null);
        Console.WriteLine(FocusProcess.MainWindowHandle == null);
        Console.WriteLine(rect==null);
        GetWindowRect(FocusProcess.MainWindowHandle, out rect);
        if ( rect.Top != 0 )
        {
            return new int[] { rect.Left, rect.Top };
        }
        return new int[] { 0, 0 };
    }

在此先感谢您,我对使用本机函数完全没有经验。

您将该结构声明为C#类。 那已经是引用类型了。 因此,当您将其作为out参数传递时,您现在有了一个双指针。 要么

  • 从类更改为结构,
  • 或按价值通过课程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM