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

GetWindowText() function is not executing correctly at all times
2021-07-22
  •  译文(汉语)
  •  原文(英语)

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

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

 IntPtr WindowTitleTextPtr = GetForegroundWindow();

 StringBuilder WindowTitleText = new StringBuilder();

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

 ...

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

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

但是,如果我打开一个网页,在这种情况下,URL填充了一些URL,然后在 GetWindowText()行中我得到: vs32host.exe已停止工作消息窗口,要求我执行以下操作:GetWindowText()函数并非始终正确执行

这是怎么回事?

帮助!

速聊1:
如果您将try包含GetWindowText()try / catch中,这可能会有所帮助,并确切地查看是什么异常导致了此错误...假定它是一个异常,而不是某些外部原因-只是将其发布为一种可能自己找到答案的方法,或至少接近找到一个.
速聊2:
:抱歉,忘记了我的问题.我也尝试过,我将GetWindowText()包含在try块内,但是控件根本不进入catch块:-(
速聊3:
很有意思...我非常怀疑,但是"调试"按钮有什么用吗?
速聊4:
我什至尝试了,它试图打开及时调试器,但是却没有打开(抛出其他错误消息,说-无法调试).
解决过程1

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

  StringBuilder WindowTitleText = new StringBuilder();

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

  // Read text into allocated memory
  GetWindowText(WindowTitleTextPtr, WindowTitleText, WindowTitleText.Length); 
速聊1:
我曾在VS2010项目中使用过与HaggarTheHorrible基本上相同的代码,并且效果很好.我不知道它如何在VS2012中停止工作.但是,为什么这样做呢?是否不像动态分配内存?这让我感到非常奇怪.
速聊2:
:访问冲突错误通常是不稳定的错误.当您不分配内存时,默认情况下,StringBuilder的内部缓冲区指针可以分配少量内存,例如16字节(为了不进行重新分配而是进行确认);当其他用户不太满意时,写数据可能超出内部缓冲区边界并不会破坏您的环境中的数据.
速聊3:
冒险超出托管代码范围时,会发生奇怪的事情.谢谢,您的解决方案极大地帮助了我.

I'm writing a small application where I wish to get the URL from the Chrome browse.

In order to first check if the Chrome browser is open or not I use the following code:

 IntPtr WindowTitleTextPtr = GetForegroundWindow();

 StringBuilder WindowTitleText = new StringBuilder();

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

 ...

I'm using GetWindowText() function to get the Windows title text, but I'm facing a problem there.

If the Chrome window has NO URL and is simply a New Tab then I have no issues, WindowTitleText.ToString() is equal to New Tab - Google Chrome.

However if I open a webpage, in which case the URL is filled with some URL then at the line GetWindowText() I get: vs32host.exe has stopped working message window asking for me to GetWindowText() function is not executing correctly at all times

What's going on?

Help!

Talk1:
It might help if you enclose the GetWindowText() in a try/catch, and see exactly what exception is causing this error... assuming it's an exception, rather than some external thing - just posting this as a way to possibly find answers on your own, or at least come closer to finding one.
Talk2:
: Sorry, forgot to mention that in my question. I have tried that as well, I enclosed GetWindowText() inside a try block but the control doesn't enter into catch block at all :-(
Talk3:
that's interesting... I very much doubt it but does the 'Debug' button do anything useful?
Talk4:
I tried even that, it tries to open just in time debugger but it fails to do so(Throws up some other error message saying - unable to debug).
Solutions1

You should allocate memory within the StringBuilder instance:

  StringBuilder WindowTitleText = new StringBuilder();

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

  // Read text into allocated memory
  GetWindowText(WindowTitleTextPtr, WindowTitleText, WindowTitleText.Length); 
Talk1:
I had used code which looked basically the same like HaggarTheHorrible's in a VS2010 project and it worked perfectly. I don't know how it stopped working in VS2012. Nonetheless, why does this work? Isn't memory allocated, like, dynamically? This strikes me as a real oddity.
Talk2:
: Access Violation errors are usually unstable ones. When you don't allocate memory, StringBuilder's internal buffer pointer could allocate small memory amount, say 16 byte, by default (in order not to do re-allocatation but confirmation); it may happen that writing data beyond internal buffer borders doesn't spoil data in your very environment, when other users aren't so happy.
Talk3:
Bizarre things happen when venturing past the confines of managed code. Thanks, your solution helped me tremendously.
转载于:https://stackoverflow.com/questions/17377453/getwindowtext-function-is-not-executing-correctly-at-all-times

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入