Code: Select all
;exemple to get any console text by Handle using UI Automation
;https://en.wikipedia.org/wiki/Microsoft_UI_Automation
EnableExplicit
Macro DEFINE_GUID(name, l1, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8)
DataSection
name:
Data.l $l1
Data.w $w1, $w2
Data.b $b1, $b2, $b3, $b4, $b5, $b6, $b7, $b8
EndDataSection
EndMacro
DEFINE_GUID(IID_IUIAutomation, 30cbe57d, d9d0, 452a, ab, 13, 7a, c5, ac, 48, 25, ee);
DEFINE_GUID(CLSID_CUIAutomation, ff48dba4, 60ef, 4201, aa, 87, 54, 10, 3e, ef, 59, 4e);
Interface IUIAutomation Extends IUnknown
CompareElements()
CompareRuntimeIds()
GetRootElement()
ElementFromHandle(hwnd,*Element)
ElementFromPoint()
GetFocusedElement()
GetRootElementBuildCache()
ElementFromHandleBuildCache()
ElementFromPointBuildCache()
GetFocusedElementBuildCache()
CreateTreeWalker()
ControlViewWalker()
ContentViewWalker()
RawViewWalker()
RawViewCondition()
ControlViewCondition()
ContentViewCondition()
CreateCacheRequest()
CreateTrueCondition()
CreateFalseCondition()
CreatePropertyCondition(int,val.p-variant,ptr)
;...
EndInterface
Interface IUIAutomationElement Extends IUnknown
SetFocus()
GetRuntimeId()
FindFirst(long,ptr,ptr)
FindAll()
FindFirstBuildCache()
FindAllBuildCache()
BuildUpdatedCache()
GetCurrentPropertyValue()
GetCurrentPropertyValueEx()
GetCachedPropertyValue()
GetCachedPropertyValueEx()
GetCurrentPatternAs()
GetCachedPatternAs()
GetCurrentPattern(long,ptr)
;....
EndInterface
Interface UIAutomationTextPattern Extends IUnknown
RangeFromPoint()
RangeFromChild()
GetSelection()
GetVisibleRanges()
DocumentRange(ptr)
SupportedTextSelection()
EndInterface
Interface IUIAutomationTextRange Extends IUnknown
Clone()
Compare()
CompareEndpoints()
ExpandToEnclosingUnit()
FindAttribute()
FindText()
GetAttributeValue()
GetBoundingRectangles()
GetEnclosingElement()
GetText(long,*str)
;...
EndInterface
Macro FreeObj(Inter)
If Inter
Inter\Release()
EndIf
EndMacro
Macro GoError(val, laberror, debugtext)
If val <> 0
Debug debugtext
Goto laberror
EndIf
EndMacro
#UIA_NamePropertyId=30005
#TreeScope_Descendants=4
#UIA_TextPatternId=10014
CoInitialize_(0)
Procedure.s UIAutomation_GetConsoleCurrentText(hwndconsole)
Protected vr.variant\vt = #VT_BSTR
vr\bstrVal = SysAllocString_("Text Area") ; control id
Protected pAuto.IUIAutomation,
oElement.IUIAutomationElement,
pCondition0.IUnknown,
oDocument1.IUIAutomationElement,
oDocumentRange.IUIAutomationTextRange,
oTextPattern1.UIAutomationTextPattern,
*bstr_text, sReturn.s
GoError(CoCreateInstance_(?CLSID_CUIAutomation, 0,#CLSCTX_INPROC_SERVER, ?IID_IUIAutomation, @pAuto),l_error,
"error create CUIAutomation object ")
GoError(pAuto\ElementFromHandle(hwndconsole, @oElement),l_error,
"error the handle of console not exist !")
GoError(pAuto\CreatePropertyCondition(#UIA_NamePropertyId, vr,@pCondition0 ),l_error,
"error create condition obj")
GoError(oElement\FindFirst(#TreeScope_Descendants, pCondition0, @oDocument1),l_error,
"error get control !!")
GoError(oDocument1\GetCurrentPattern(#UIA_TextPatternId, @oTextPattern1),l_error,
"error get TextPattern obj")
GoError(oTextPattern1\DocumentRange(@oDocumentRange),l_error,
"error get Document range")
GoError(oDocumentRange\GetText(-1, @*bstr_text),l_error,
"errr get control text !!")
If *bstr_text
sReturn = PeekS(*bstr_text)
EndIf
l_error:
If *bstr_text : SysFreeString_(*bstr_text) : EndIf
If vr\bstrVal : SysFreeString_(vr\bstrVal) : EndIf
FreeObj(pAuto)
FreeObj(oElement)
FreeObj(pCondition0)
FreeObj(oDocument1)
FreeObj(oDocumentRange)
FreeObj(oTextPattern1)
ProcedureReturn ReplaceString(sReturn , #CRLF$ + #CRLF$+ #CRLF$, "")
EndProcedure
Debug UIAutomation_GetConsoleCurrentText(FindWindow_("ConsoleWindowClass", #Null))