Convert library to v2

pull/99/head
Clive Galway 1 year ago
parent ca52f695ff
commit 8c0ba4671f

@ -0,0 +1,264 @@
#include %A_LineFile%\..\CLR.ahk
class AutoHotInterception {
_contextManagers := Map()
__New(){
bitness := A_PtrSize == 8 ? "x64" : "x86"
dllName := "interception.dll"
if (A_IsCompiled){
dllFile := A_LineFile "\..\Lib\" bitness "\" dllName
DirCreate("Lib")
FileInstall("Lib\AutoHotInterception.dll", "Lib\AutoHotInterception.dll")
if (bitness == "x86"){
DirCreate("Lib\x86")
FileInstall("Lib\x86\interception.dll", "Lib\x86\interception.dll")
} else {
DirCreate("Lib\x64")
FileInstall("Lib\x64\interception.dll", "Lib\x64\interception.dll")
}
} else {
dllFile := A_LineFile "\..\" bitness "\" dllName
}
if (!FileExist(dllFile)) {
MsgBox("Unable to find " dllFile ", exiting...`nYou should extract both x86 and x64 folders from the library folder in interception.zip into AHI's lib folder.")
ExitApp
}
hModule := DllCall("LoadLibrary", "Str", dllFile, "Ptr")
if (hModule == 0) {
this_bitness := A_PtrSize == 8 ? "64-bit" : "32-bit"
other_bitness := A_PtrSize == 4 ? "64-bit" : "32-bit"
MsgBox("Bitness of " dllName " does not match bitness of AHK.`nAHK is " this_bitness ", but " dllName " is " other_bitness ".")
ExitApp
}
DllCall("FreeLibrary", "Ptr", hModule)
dllName := "AutoHotInterception.dll"
if (A_IsCompiled){
dllFile := A_LineFile "\..\Lib\" dllName
} else {
dllFile := A_LineFile "\..\" dllName
}
hintMessage := "Try right-clicking " dllFile ", select Properties, and if there is an 'Unblock' checkbox, tick it`nAlternatively, running Unblocker.ps1 in the lib folder (ideally as admin) can do this for you."
if (!FileExist(dllFile)) {
MsgBox("Unable to find " dllFile ", exiting...")
ExitApp
}
asm := CLR_LoadLibrary(dllFile)
try {
this.Instance := asm.CreateInstance("AutoHotInterception.Manager")
}
catch {
MsgBox(dllName " failed to load`n`n" hintMessage)
ExitApp
}
if (this.Instance.OkCheck() != "OK") {
MsgBox(dllName " loaded but check failed!`n`n" hintMessage)
ExitApp
}
}
GetInstance() {
return this.Instance
}
; --------------- Input Synthesis ----------------
SendKeyEvent(id, code, state) {
this.Instance.SendKeyEvent(id, code, state)
}
SendMouseButtonEvent(id, btn, state) {
this.Instance.SendMouseButtonEvent(id, btn, state)
}
SendMouseButtonEventAbsolute(id, btn, state, x, y) {
this.Instance.SendMouseButtonEventAbsolute(id, btn, state, x, y)
}
SendMouseMove(id, x, y) {
this.Instance.SendMouseMove(id, x, y)
}
SendMouseMoveRelative(id, x, y) {
this.Instance.SendMouseMoveRelative(id, x, y)
}
SendMouseMoveAbsolute(id, x, y) {
this.Instance.SendMouseMoveAbsolute(id, x, y)
}
SetState(state){
this.Instance.SetState(state)
}
MoveCursor(x, y, cm := "Screen", mouseId := -1){
if (mouseId == -1)
mouseId := 11 ; Use 1st found mouse
oldMode := A_CoordModeMouse
CoordMode("Mouse", cm)
Loop {
MouseGetPos(&cx, &cy)
dx := this.GetDirection(cx, x)
dy := this.GetDirection(cy, y)
if (dx == 0 && dy == 0)
break
this.SendMouseMove(mouseId, dx, dy)
}
CoordMode("Mouse", oldMode)
}
GetDirection(cp, dp){
d := dp - cp
if (d > 0)
return 1
if (d < 0)
return -1
return 0
}
; --------------- Querying ------------------------
GetDeviceId(IsMouse, VID, PID, instance := 1) {
static devType := {0: "Keyboard", 1: "Mouse"}
dev := this.Instance.GetDeviceId(IsMouse, VID, PID, instance)
if (dev == 0) {
MsgBox("Could not get " devType[isMouse] " with VID " VID ", PID " PID ", Instance " instance)
ExitApp
}
return dev
}
GetDeviceIdFromHandle(isMouse, handle, instance := 1) {
static devType := {0: "Keyboard", 1: "Mouse"}
dev := this.Instance.GetDeviceIdFromHandle(IsMouse, handle, instance)
if (dev == 0) {
MsgBox("Could not get " devType[isMouse] " with Handle " handle ", Instance " instance)
ExitApp
}
return dev
}
GetKeyboardId(VID, PID, instance := 1) {
return this.GetDeviceId(false, VID, PID, instance)
}
GetMouseId(VID, PID, instance := 1) {
return this.GetDeviceId(true, VID, PID, instance)
}
GetKeyboardIdFromHandle(handle, instance := 1) {
return this.GetDeviceIdFromHandle(false, handle, instance)
}
GetMouseIdFromHandle(handle, instance := 1) {
return this.GetDeviceIdFromHandle(true, handle, instance)
}
GetDeviceList() {
DeviceList := Map()
arr := this.Instance.GetDeviceList()
for v in arr {
; ToDo: Return a class, so code completion works?
DeviceList[v.id] := { ID: v.id, VID: v.vid, PID: v.pid, IsMouse: v.IsMouse, Handle: v.Handle }
}
return DeviceList
}
; ---------------------- Subscription Mode ----------------------
SubscribeKey(id, code, block, callback, concurrent := false) {
this.Instance.SubscribeKey(id, code, block, callback, concurrent)
}
UnsubscribeKey(id, code){
this.Instance.UnsubscribeKey(id, code)
}
SubscribeKeyboard(id, block, callback, concurrent := false) {
this.Instance.SubscribeKeyboard(id, block, callback, concurrent)
}
UnsubscribeKeyboard(id){
this.Instance.UnsubscribeKeyboard(id)
}
SubscribeMouseButton(id, btn, block, callback, concurrent := false) {
this.Instance.SubscribeMouseButton(id, btn, block, callback, concurrent)
}
UnsubscribeMouseButton(id, btn){
this.Instance.UnsubscribeMouseButton(id, btn)
}
SubscribeMouseButtons(id, block, callback, concurrent := false) {
this.Instance.SubscribeMouseButtons(id, block, callback, concurrent)
}
UnsubscribeMouseButtons(id){
this.Instance.UnsubscribeMouseButtons(id)
}
SubscribeMouseMove(id, block, callback, concurrent := false) {
this.Instance.SubscribeMouseMove(id, block, callback, concurrent)
}
UnsubscribeMouseMove(id){
this.Instance.UnsubscribeMouseMove(id)
}
SubscribeMouseMoveRelative(id, block, callback, concurrent := false) {
this.Instance.SubscribeMouseMoveRelative(id, block, callback, concurrent)
}
UnsubscribeMouseMoveRelative(id){
this.Instance.UnsubscribeMouseMoveRelative(id)
}
SubscribeMouseMoveAbsolute(id, block, callback, concurrent := false) {
this.Instance.SubscribeMouseMoveAbsolute(id, block, callback, concurrent)
}
UnsubscribeMouseMoveAbsolute(id){
this.Instance.UnsubscribeMouseMoveAbsolute(id)
}
; ------------- Context Mode ----------------
; Creates a context class to make it easy to turn on/off the hotkeys
CreateContextManager(id) {
if (this._contextManagers.Has(id)) {
Msgbox("ID " id " already has a Context Manager")
ExitApp
}
cm := AutoHotInterception.ContextManager(this, id)
this._contextManagers[id] := cm
return cm
}
RemoveContextManager(id) {
if (!this._contextManagers.Has(id)) {
Msgbox("ID " id " does not have a Context Manager")
ExitApp
}
this._contextManagers[id].Remove()
this._contextManagers.Delete(id)
}
; Helper class for dealing with context mode
class ContextManager {
IsActive := 0
__New(parent, id) {
this.parent := parent
this.id := id
result := this.parent.Instance.SetContextCallback(id, this.OnContextCallback.Bind(this))
}
OnContextCallback(state) {
Sleep 0
this.IsActive := state
}
Remove(){
this.parent.Instance.RemoveContextCallback(this.id)
}
}
}

@ -0,0 +1,142 @@
; ==========================================================
; .NET Framework Interop
; https://www.autohotkey.com/boards/viewtopic.php?t=4633
; ==========================================================
;
; Author: Lexikos
; Version: 2.0
; Requires: AutoHotkey v2.0-beta.1
;
CLR_LoadLibrary(AssemblyName, AppDomain:=0)
{
if !AppDomain
AppDomain := CLR_GetDefaultDomain()
try
return AppDomain.Load_2(AssemblyName)
static null := ComValue(13,0)
args := ComObjArray(0xC, 1), args[0] := AssemblyName
typeofAssembly := AppDomain.GetType().Assembly.GetType()
try
return typeofAssembly.InvokeMember_3("LoadWithPartialName", 0x158, null, null, args)
catch
return typeofAssembly.InvokeMember_3("LoadFrom", 0x158, null, null, args)
}
CLR_CreateObject(Assembly, TypeName, Args*)
{
if !(argCount := Args.Length)
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 := ComValue(13,0)
return Assembly.CreateInstance_3(TypeName, true, 0, null, vargs, null, Array_Empty)
}
CLR_CompileCS(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(&AppDomain, BaseDirectory:="")
{
static null := ComValue(13,0)
args := ComObjArray(0xC, 5), args[0] := "", args[2] := BaseDirectory, args[4] := ComValue(0xB,false)
AppDomain := CLR_GetDefaultDomain().GetType().InvokeMember_3("CreateDomain", 0x158, null, null, args)
}
; ICorRuntimeHost::UnloadDomain
CLR_StopDomain(AppDomain) => ComCall(20, CLR_Start(), "ptr", ComObjValue(AppDomain))
; 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
if Version = ""
Loop Files EnvGet("SystemRoot") "\Microsoft.NET\Framework" (A_PtrSize=8?"64":"") "\*","D"
if (FileExist(A_LoopFilePath "\mscorlib.dll") && StrCompare(A_LoopFileName, Version) > 0)
Version := A_LoopFileName
static CLSID_CorRuntimeHost := CLR_GUID("{CB2F6723-AB3A-11D2-9C40-00C04FA30A3E}")
static IID_ICorRuntimeHost := CLR_GUID("{CB2F6722-AB3A-11D2-9C40-00C04FA30A3E}")
DllCall("mscoree\CorBindToRuntimeEx", "wstr", Version, "ptr", 0, "uint", 0
, "ptr", CLSID_CorRuntimeHost, "ptr", IID_ICorRuntimeHost
, "ptr*", &RtHst:=0, "hresult")
ComCall(10, RtHst) ; Start
return RtHst
}
;
; INTERNAL FUNCTIONS
;
CLR_GetDefaultDomain()
{
; ICorRuntimeHost::GetDefaultDomain
static defaultDomain := (
ComCall(13, CLR_Start(), "ptr*", &p:=0),
ComObjFromPtr(p)
)
return defaultDomain
}
CLR_CompileAssembly(Code, References, ProviderAssembly, ProviderType, AppDomain:=0, FileName:="", CompilerOptions:="")
{
if !AppDomain
AppDomain := CLR_GetDefaultDomain()
asmProvider := CLR_LoadLibrary(ProviderAssembly, AppDomain)
codeProvider := asmProvider.CreateInstance(ProviderType)
codeCompiler := codeProvider.CreateCompiler()
asmSystem := (ProviderAssembly="System") ? asmProvider : CLR_LoadLibrary("System", AppDomain)
; Convert | delimited list of references into an array.
Refs := References is String ? StrSplit(References, "|", " `t") : References
aRefs := ComObjArray(8, Refs.Length)
Loop Refs.Length
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,-4)=".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"
throw Error("Compilation failed",, "`n" error_text)
}
; Success. Return Assembly object or path.
return FileName="" ? compilerRes.CompiledAssembly : compilerRes.PathToAssembly
}
; Usage 1: pGUID := CLR_GUID(&GUID, "{...}")
; Usage 2: GUID := CLR_GUID("{...}"), pGUID := GUID.Ptr
CLR_GUID(a, b:=unset)
{
DllCall("ole32\IIDFromString"
, "wstr", sGUID := IsSet(b) ? b : a
, "ptr", GUID := Buffer(16,0), "hresult")
return IsSet(b) ? GUID.Ptr : GUID
}
Loading…
Cancel
Save