1

I'm working on a Windows 7 C++ program that will capture the text on a screen. Microsoft's UIAutomation framework seems to be the best way of going about it. I've got it pretty much working, except for one thing--when I use the framework's functions to get the text, it seems to only be returning the first million characters. That might seem like a lot, but try doing a listing of every file on your hard drive, and it very quickly gets up in the multi-million-character range. I've tried two different ways to get that text, and both ways return the same thing, and both are limited to the first million characters.

Here's a sample of the code:

IUIAutomationElement *element = findElement();

VARIANT v;
VariantInit(&v);

element->GetCurrentPropertyValue(UIA_LegacyIAccessibleValuePropertyId, &v);

BSTR tempString = V_BSTR(&v);

Everything works as intended when the amount of text is less than a million characters long, but anything over that limit gets truncated.

I hypothesis that there might be two places where the problem occurs. First, it's possible that a VARIANT can only hold a million characters, but that seems to not be correct, based on what I could find out about Microsoft's VARIANT implemented. The second is that the GetCurrentPropertyValue() call only returns up to a million characters. Which is rather bad because I don't see a way around it.

Note that I've also done this using IUIAutomationValuePattern and IUIAutomationTextRange, but it yields the same results.

Any thoughts?

DukeBrymin
  • 351
  • 3
  • 15

1 Answers1

1

After instrumenting the code a bit more, I've realized that I'm incorrect in my assertion about VARIANT's ability to hold more than a million characters. What's actually happening is that the BSTR I'm pulling from the VARIANT is holding all the data, so it's not truncating at all. What's really happening is that the wstring that I'm creating from the BSTR (for ease of string manipulation) is what's doing the truncating. Seems as if

wstring(tempString);

will only give me a string of at most a million characters. So, while I haven't fixed the problem, I have succeeded in narrowing down the culprit some more, and at least it's a tractable problem, since I'm not actually losing the data.

EDIT: And I'm wrong about wstring's ability to handle ultra-long strings too. If I just look at the length of the wstring, I see that it's copying the BSTR correctly. Which is a relief.

The thing that is limited to one million characters is Visual Studio 2015's string inspection facility--anytime I'd look at the contents of the wstring, it would be no longer than one million characters.

So, thanks for this journey of self-discovery you've helped me with--nothing more to see here. Move along. Move along.

DukeBrymin
  • 351
  • 3
  • 15