1

Тема: AHK: CLR-как заставить работать скрипт?

На англоязычном форуме нашел скрипт который должен перемещать курсор нажатием клавиш. Почему то не работает. Скачал драйвер MicroTimer.dll нашел CLR.ahk расположил их в директории скрипта. Скрипт запускается, ошибок не выдает, висит в трее -но никак не реагирует на нажатия.


#include CLR.ahk

#SingleInstance force

if (!FileExist("MicroTimer\MicroTimer.dll")){
	MsgBox DLL Not found
	ExitApp
}
asm := CLR_LoadLibrary("MicroTimer\MicroTimer.dll")
; Use CLR to instantiate a class from within the DLL
mt := asm.CreateInstance("MicroTimer")

XState := 0
YState := 0
XTimer := mt.Create(Func("MoveX"), 1)
YTimer := mt.Create(Func("MoveY"), 1)
return

MoveX(){
	global XState
	DllCall("user32.dll\mouse_event", "UInt", 0x0001, "UInt", XState, "UInt", 0, "UInt", 0, "UPtr", 0)
}

MoveY(){
	global YState
	DllCall("user32.dll\mouse_event", "UInt", 0x0001, "UInt", 0, "UInt", YState, "UInt", 0, "UPtr", 0)
}

XEvent(evt){
	global XState, XTimer
	XState := evt
	XTimer.SetState(abs(XState))
}

YEvent(evt){
	global YState, YTimer
	YState := evt
	YTimer.SetState(abs(YState))
}

~Left::
	XEvent(-1)
	return
	
~Right::
	XEvent(1)
	return

~Left up::
~Right up::
	XEvent(0)
	return
	
~Up::
	YEvent(-1)
	return

~Down::
	YEvent(1)
	return

up up::
down up::
	YEvent(0)
	return

^Esc::
	ExitApp

Так же нашел код CLR.ahk:


; ==========================================================
;                  .NET Framework Interop
;      https://autohotkey.com/boards/viewtopic.php?t=4633
; ==========================================================
;
;   Author:     Lexikos
;   Version:    1.2
;   Requires:	AutoHotkey_L v1.0.96+
;

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 : ""
}

Для меня здесь все непонятно еще  AutoHotkey_L v1.0.96+ будет ли это работать на нынешней версии AHK?
Перемещать мышь это не самоцель, интересно разобраться - здесь ведь используется и самодельный драйвер на C# и библиотеки- хочется понять.

2

Re: AHK: CLR-как заставить работать скрипт?

Все работает:

; NOTE: While a script like this is running, the entire operating system and all applications are
; affected by timeBeginPeriod below.

SetBatchLines -1  ; Ensures maximum effectiveness of this method.
ntchs:=2
SleepDuration = 1  ; This can sometimes be finely adjusted (e.g. 2 is different than 3) depending on the value below.
TimePeriod = 3 ; Try 7 or 3.  See comment below.
; On a PC whose sleep duration normally rounds up to 15.6 ms, try TimePeriod=7 to allow
; somewhat shorter sleeps, and try TimePeriod=3 or less to allow the shortest possible sleeps.

;~ DllCall("Winmm\timeBeginPeriod", uint, TimePeriod)  ; Affects all applications, not just this script's DllCall("Sleep"...), but does not affect SetTimer.

~Right::
	DllCall("user32.dll\mouse_event", "UInt", 0x0001, "UInt", 1*ntchs, "UInt", 0, "UInt", 0, "UPtr", 0)
    DllCall("Sleep", UInt, SleepDuration)  ; Must use DllCall instead of the Sleep command.
DllCall("Winmm\timeEndPeriod", UInt, TimePeriod)  ; Should be called to restore system to normal.
return
~Left::
loop
{
	DllCall("user32.dll\mouse_event", "UInt", 0x0001, "UInt", -1*ntchs, "UInt", 0, "UInt", 0, "UPtr", 0)
    DllCall("Sleep", UInt, SleepDuration)  ; Must use DllCall instead of the Sleep command.
	if !GetKeyState("Left", "P")
	break
}
DllCall("Winmm\timeEndPeriod", UInt, TimePeriod)  ; Should be called to restore system to normal.
return
~Down::
loop
{
	DllCall("user32.dll\mouse_event", "UInt", 0x0001, "UInt", 0, "UInt", 1*ntchs, "UInt", 0, "UPtr", 0)
    DllCall("Sleep", UInt, SleepDuration)  ; Must use DllCall instead of the Sleep command.
	if !GetKeyState("Down", "P")
	break
}
DllCall("Winmm\timeEndPeriod", UInt, TimePeriod)  ; Should be called to restore system to normal.
return
~Up::
loop
{
	DllCall("user32.dll\mouse_event", "UInt", 0x0001, "UInt", 0, "UInt", -1*ntchs, "UInt", 0, "UPtr", 0)
    DllCall("Sleep", UInt, SleepDuration)  ; Must use DllCall instead of the Sleep command.
	if !GetKeyState("Up", "P")
	break
}
DllCall("Winmm\timeEndPeriod", UInt, TimePeriod)  ; Should be called to restore system to normal.
return


^Esc::
DllCall("Winmm\timeEndPeriod", UInt, TimePeriod)  ; Should be called to restore system to normal.
ExitApp
return

3 (изменено: sabir.yanin2014, 2017-08-26 02:33:08)

Re: AHK: CLR-как заставить работать скрипт?

svoboden, я не сомневаюсь что ваш код работает, я то просил помочь разобраться почему мой код не реагирует. Или нужна другая версия AHK или нужна какая-то процедура - здесь вся ценность в работе с библиотекой  -хочется продвинуться. Пример как мне кажется простой и удачный.

4

Re: AHK: CLR-как заставить работать скрипт?

Так спросите у того, кто создал эту библиотеку.

5

Re: AHK: CLR-как заставить работать скрипт?

АХК должен быть запущен на АНСИ, если юникод то пробуйте.

Когда вы говорите что не можете сделать, вам всего-лишь не хватает фантазии придумать какой-нибудь костыль.

6

Re: AHK: CLR-как заставить работать скрипт?

belyankin12, другую версию AHK устанавливать или что-то еще делать?

7 (изменено: belyankin12, 2017-08-26 12:03:18)

Re: AHK: CLR-как заставить работать скрипт?

Можно и эту версию, просто переустановите АХК, используя кодировку ANSI. В корне исполняемого файла ahk есть скрипт-переустановщик. Но для начала, попробуй запустить скрипт через контекстное меню "Открыть с помощью" и выберите в списке программ AHK ANSI 32 bit, а если его нету, то укажите к нему путь, он лежит в папке Autohotkey. Если заработает - то нужно переустанавливать AHK на ANSI, если нет, то в этом смысла нет тогда.

Когда вы говорите что не можете сделать, вам всего-лишь не хватает фантазии придумать какой-нибудь костыль.

8

Re: AHK: CLR-как заставить работать скрипт?

belyankin12, к сожалению не работает.

9

Re: AHK: CLR-как заставить работать скрипт?

Еще одна моя цель таймер получить меньше 1000 микросекунд.

10 (изменено: sabir.yanin2014, 2017-08-26 13:11:54)

Re: AHK: CLR-как заставить работать скрипт?

При запуске с помощью ANSI выпадает такая ошибка

Post's attachments

2017-08-26_11-39-44.png
2017-08-26_11-39-44.png 8.61 kb, 2 downloads since 2017-08-26 

You don't have the permssions to download the attachments of this post.

11

Re: AHK: CLR-как заставить работать скрипт?

Микротаймер:

Post's attachments

MicroTimer.dll 9.5 kb, 4 downloads since 2017-08-26 

You don't have the permssions to download the attachments of this post.

12

Re: AHK: CLR-как заставить работать скрипт?

Вам нужен скрипт для перемещения курсора нажатием клавиш? Или заставить работать какой-то незнакомый скрипт, взятый с англоязычного форума?

13 (изменено: sabir.yanin2014, 2017-08-26 15:48:56)

Re: AHK: CLR-как заставить работать скрипт?

ypppu, вы правильно поняли, второе- хочу заставить работать непонятный скрипт. Ведь у автора он работает, я-то почему не могу его применить?

14

Re: AHK: CLR-как заставить работать скрипт?

Проверил, работает. AutoHotkey 1.1.25.1 Unicode 32-bit, Windows XP.

15 (изменено: Malcev, 2017-08-26 16:08:00)

Re: AHK: CLR-как заставить работать скрипт?

Win7 64bit, ahk 1.1.26.01 Unicode 32-bit - не работает.
Но заработает, если в MicroTimer.dll Properties выбрать unblock.

16

Re: AHK: CLR-как заставить работать скрипт?

Malcev, спасибо все заработало: драйвер MicroTimer.dll  был заблокирован - нужно было в его свойствах разрешить его использование .