Your example w/o 3rd party libraries always returns an empty MsgBox here, but the FlaUI version does work!
Thank you for this, no doubt that this is something I can build upon. And an even bigger thanks for pointing me to
. I spend the last few days dealing with that UIDeskAutomation.dll (+ getting along with c#) , and was able to create this working solution:
Code: Select all
; Using CLR.ahk v1.2
; Requirements:
; UIDeskAutomation.dll
; A running instance of file manager xplorer2 lite, for testing purposes (file pane is a custom ListView control)
cSharp =
(
using System;
using System.Windows.Forms;
using UIDeskAutomationLib;
class foo {
public string[,] Readout(int ProcessID) {
var engine = new Engine();
UIDA_DataGrid TheGrid = null;
int NumberOfColumns = 0;
// Find the custom ListView control
TheGrid = engine.GetTopLevelByProcId(ProcessID, "*", "ATL:ExplorerFrame").PaneAt("", 2).PaneAt("", 2).Pane("").DataGrid("Left pane settings");
NumberOfColumns = TheGrid.ColumnCount;
// Copy the content into a UIDA_DataItem[] as it provides significantly faster access
UIDA_DataItem[] AllRows = TheGrid.Rows;
// Creating the output Array
int NumberOfRows = TheGrid.RowCount;
string[,] ArrayOfListView = new string[NumberOfRows + 1, NumberOfColumns + 1];
// Filling the Array with the grids contents - including the index of the selected row!
for (int RowX = 0; RowX < NumberOfRows ; RowX++)
{
if (AllRows[RowX].IsSelected)
ArrayOfListView[0,0] = (RowX + 1).ToString();
for (int ColumnX = 0; ColumnX < NumberOfColumns; ColumnX++)
{
ArrayOfListView[RowX + 1,ColumnX + 1] = AllRows[RowX][ColumnX];
}
}
return ArrayOfListView;
}
}
)
; -----
Process, Exist, xplorer2_lite.exe
ProcessID := ErrorLevel
If not ProcessID
{ MsgBox, xplorer2 lite not found.`n`nExiting.
ExitApp
}
asm := CLR_CompileC#(cSharp, "System.dll | " DotNetPath "System.Windows.Forms.dll | UIDeskAutomation.dll")
obj := CLR_CreateObject(asm, "foo")
ArrayOfListView := obj.Readout(ProcessID)
If (not ArrayOfListView.MaxIndex() OR not ArrayOfListView[1,1])
MsgBox, No content has been found.
else
{ If (ArrayOfListView[0,0])
MsgBox, % "Selected row:`n`n" ArrayOfListView[ArrayOfListView[0,0],1]
else
MsgBox, There is no row selected.
msgbox % ArrayOfListView[1,1] "`n" ArrayOfListView[1,2] "`n" ArrayOfListView[1,3] "`n" ArrayOfListView[1,4] "`n" ArrayOfListView[1,5]
msgbox % ArrayOfListView[2,1] "`n" ArrayOfListView[2,2] "`n" ArrayOfListView[2,3] "`n" ArrayOfListView[2,4] "`n" ArrayOfListView[2,5]
}
ExitApp
Esc::
ExitApp
; ==== CLR.ahk v1.2 by Lexikos ====
CLR_LoadLibrary(AssemblyName, AppDomain=0)
{
if !AppDomain
AppDomain := CLR_GetDefaultDomain()
e := ComObjError(0)
Loop 1 {
if assembly := AppDomain.Load_2(AssemblyName)
break
static null := ComObject(13,0)
args := ComObjArray(0xC, 1), args[0] := AssemblyName
typeofAssembly := AppDomain.GetType().Assembly.GetType()
if assembly := typeofAssembly.InvokeMember_3("LoadWithPartialName", 0x158, null, null, args)
break
if assembly := typeofAssembly.InvokeMember_3("LoadFrom", 0x158, null, null, args)
break
}
ComObjError(e)
return assembly
}
CLR_CreateObject(Assembly, TypeName, Args*)
{
if !(argCount := Args.MaxIndex())
return Assembly.CreateInstance_2(TypeName, true)
vargs := ComObjArray(0xC, argCount)
Loop % argCount
vargs[A_Index-1] := Args[A_Index]
static Array_Empty := ComObjArray(0xC,0), null := ComObject(13,0)
return Assembly.CreateInstance_3(TypeName, true, 0, null, vargs, null, Array_Empty)
}
CLR_CompileC#(Code, References="", AppDomain=0, FileName="", CompilerOptions="")
{
return CLR_CompileAssembly(Code, References, "System", "Microsoft.CSharp.CSharpCodeProvider", AppDomain, FileName, CompilerOptions)
}
CLR_CompileVB(Code, References="", AppDomain=0, FileName="", CompilerOptions="")
{
return CLR_CompileAssembly(Code, References, "System", "Microsoft.VisualBasic.VBCodeProvider", AppDomain, FileName, CompilerOptions)
}
CLR_StartDomain(ByRef AppDomain, BaseDirectory="")
{
static null := ComObject(13,0)
args := ComObjArray(0xC, 5), args[0] := "", args[2] := BaseDirectory, args[4] := ComObject(0xB,false)
AppDomain := CLR_GetDefaultDomain().GetType().InvokeMember_3("CreateDomain", 0x158, null, null, args)
return A_LastError >= 0
}
CLR_StopDomain(ByRef AppDomain)
{ ; ICorRuntimeHost::UnloadDomain
DllCall("SetLastError", "uint", hr := DllCall(NumGet(NumGet(0+RtHst:=CLR_Start())+20*A_PtrSize), "ptr", RtHst, "ptr", ComObjValue(AppDomain))), AppDomain := ""
return hr >= 0
}
; NOTE: IT IS NOT NECESSARY TO CALL THIS FUNCTION unless you need to load a specific version.
CLR_Start(Version="") ; returns ICorRuntimeHost*
{
static RtHst := 0
; The simple method gives no control over versioning, and seems to load .NET v2 even when v4 is present:
; return RtHst ? RtHst : (RtHst:=COM_CreateObject("CLRMetaData.CorRuntimeHost","{CB2F6722-AB3A-11D2-9C40-00C04FA30A3E}"), DllCall(NumGet(NumGet(RtHst+0)+40),"uint",RtHst))
if RtHst
return RtHst
EnvGet SystemRoot, SystemRoot
if Version =
Loop % SystemRoot "\Microsoft.NET\Framework" (A_PtrSize=8?"64":"") "\*", 2
if (FileExist(A_LoopFileFullPath "\mscorlib.dll") && A_LoopFileName > Version)
Version := A_LoopFileName
if DllCall("mscoree\CorBindToRuntimeEx", "wstr", Version, "ptr", 0, "uint", 0
, "ptr", CLR_GUID(CLSID_CorRuntimeHost, "{CB2F6723-AB3A-11D2-9C40-00C04FA30A3E}")
, "ptr", CLR_GUID(IID_ICorRuntimeHost, "{CB2F6722-AB3A-11D2-9C40-00C04FA30A3E}")
, "ptr*", RtHst) >= 0
DllCall(NumGet(NumGet(RtHst+0)+10*A_PtrSize), "ptr", RtHst) ; Start
return RtHst
}
;
; INTERNAL FUNCTIONS
;
CLR_GetDefaultDomain()
{
static defaultDomain := 0
if !defaultDomain
{ ; ICorRuntimeHost::GetDefaultDomain
if DllCall(NumGet(NumGet(0+RtHst:=CLR_Start())+13*A_PtrSize), "ptr", RtHst, "ptr*", p:=0) >= 0
defaultDomain := ComObject(p), ObjRelease(p)
}
return defaultDomain
}
CLR_CompileAssembly(Code, References, ProviderAssembly, ProviderType, AppDomain=0, FileName="", CompilerOptions="")
{
if !AppDomain
AppDomain := CLR_GetDefaultDomain()
if !(asmProvider := CLR_LoadLibrary(ProviderAssembly, AppDomain))
|| !(codeProvider := asmProvider.CreateInstance(ProviderType))
|| !(codeCompiler := codeProvider.CreateCompiler())
return 0
if !(asmSystem := (ProviderAssembly="System") ? asmProvider : CLR_LoadLibrary("System", AppDomain))
return 0
; Convert | delimited list of references into an array.
StringSplit, Refs, References, |, %A_Space%%A_Tab%
aRefs := ComObjArray(8, Refs0)
Loop % Refs0
aRefs[A_Index-1] := Refs%A_Index%
; Set parameters for compiler.
prms := CLR_CreateObject(asmSystem, "System.CodeDom.Compiler.CompilerParameters", aRefs)
, prms.OutputAssembly := FileName
, prms.GenerateInMemory := FileName=""
, prms.GenerateExecutable := SubStr(FileName,-3)=".exe"
, prms.CompilerOptions := CompilerOptions
, prms.IncludeDebugInformation := true
; Compile!
compilerRes := codeCompiler.CompileAssemblyFromSource(prms, Code)
if error_count := (errors := compilerRes.Errors).Count
{
error_text := ""
Loop % error_count
error_text .= ((e := errors.Item[A_Index-1]).IsWarning ? "Warning " : "Error ") . e.ErrorNumber " on line " e.Line ": " e.ErrorText "`n`n"
MsgBox, 16, Compilation Failed, %error_text%
return 0
}
; Success. Return Assembly object or path.
return compilerRes[FileName="" ? "CompiledAssembly" : "PathToAssembly"]
}
CLR_GUID(ByRef GUID, sGUID)
{
VarSetCapacity(GUID, 16, 0)
return DllCall("ole32\CLSIDFromString", "wstr", sGUID, "ptr", &GUID) >= 0 ? &GUID : ""
}
I am pretty happy with this, the only downside is speed. Especially the two nested loops which copy the content of the UIDA_DataItem[] into the array are somewhat time-consuming, the context menu which is going to follow may take several seconds to appear. I don't know a faster way yet, but if I would be able to return the
the menu. It's possible that I won't even need an array that way. So I tried this one:
... and several variations, but none of them is working. I don't even get an error, IsObject() just always return zero.