gpt4 book ai didi

c# - GetWindowText() 函数并非始终正确执行

转载 作者:行者123 更新时间:2023-11-30 14:59:04 25 4
gpt4 key购买 nike

我正在编写一个小应用程序,我希望从 Chrome 浏览器中获取 URL。

为了首先检查 Chrome 浏览器是否打开,我使用以下代码:

 IntPtr WindowTitleTextPtr = GetForegroundWindow();

StringBuilder WindowTitleText = new StringBuilder();

GetWindowText(WindowTitleTextPtr, WindowTitleText, 256); // Problem

...

我正在使用 GetWindowText() 函数来获取 Windows 标题文本,但我在那里遇到了问题。

如果 Chrome 窗口没有 URL,只是一个新标签,那么我没有问题,WindowTitleText.ToString() 等于 New Tab - Google Chrome

但是,如果我打开一个网页,在这种情况下,URL 会填充一些 URL,然后在 GetWindowText() 行,我得到:vs32host.exe 已停止工作 消息窗口要求我 enter image description here

这是怎么回事?

帮助!

最佳答案

您应该在 StringBuilder 实例中分配内存:

  StringBuilder WindowTitleText = new StringBuilder();

int size = 256;
WindowTitleText.Length = size; // <- Memory allocation

// Read text into allocated memory
GetWindowText(WindowTitleTextPtr, WindowTitleText, WindowTitleText.Length);

关于c# - GetWindowText() 函数并非始终正确执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17377453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com