Page 1 of 1

Autohokey.dll v2 from Powershell

Posted: 23 Aug 2022, 14:09
by newvice
In my other thread I asked how to get the AHK_H v1 dll imported into powershell which I then figured out:

Code: Select all

$MethodDefinition = @'

[DllImport("C:\\Users\\XXXDocuments\\XXX\\XXX\\XXX\\XXX\\AutoHotkey.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, EntryPoint = "ahkdll")]
public static extern int ahkdll(string scriptFilePath, string parameters = "", string title = "");

'@

$ahk = Add-Type -MemberDefinition $MethodDefinition -Name 'ahk' -PassThru

$ahk::ahkdll("C:\Users\XXX\Documents\XXX\XXX\XXX\XXX\test.ahk")


Now how do I do this for v2? I can do DLLImport without error but when I run this code...

Code: Select all

$MethodDefinition = @'

[DllImport("C:\\Users\\XXX\\Documents\\XXX\\XXX\\XXX\\XXX\\AutoHotkeyv2.dll", EntryPoint = "NewThread")]
public static extern int NewThread(string script);

'@

$ahk = Add-Type -MemberDefinition $MethodDefinition -Name 'ahk29' -PassThru

$tr = $ahk::NewThread("MsgBox Message from thread.")


...I get a nasty looking error:
Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at Microsoft.PowerShell.Commands.AddType.AutoGeneratedTypes.ahk29.NewThread(String script)
at CallSite.Target(Closure , CallSite , Object , String )
at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
at System.Management.Automation.Interpreter.DynamicInstruction`3.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)


Please help. Its the only way to run my beautiful script in the environment I intend to.

Re: Autohokey.dll v2 from Powershell

Posted: 24 Aug 2022, 15:41
by newvice
Can someone please tell me how to use the v2 dll in Powershell? Or any .NET environment. Scratch that, just give me any code. I searched the forum and scrolled trough several pages and didnt find anything.

Re: Autohokey.dll v2 from Powershell

Posted: 24 Aug 2022, 22:51
by guest3456
why are you trying to use NewThread in the v2 dll?

what about using the normal dll funcs?

Re: Autohokey.dll v2 from Powershell

Posted: 25 Aug 2022, 13:56
by newvice
I checked with dumpbin and didnt see the funcs I knew from v1. Then I saw in some thread what looked like that I need to use NewThread first.

Tried this now:

Code: Select all

$MethodDefinition = @'

[DllImport("C:\\Users\\XXX\\Documents\\XXX\\XXX\\XXX\\XXX\\XXX\\AutoHotkeyv2.dll", EntryPoint = "ahkExec")]
public static extern int ahkExec(string script);

'@

$ahk = Add-Type -MemberDefinition $MethodDefinition -Name 'ahk3' -PassThru

$ahk::ahkExec('msgbox("hellov2")')
Returned a 0 now and no msgbox. At least better than before. As I read a 0 means error.

Any ideas?

Re: Autohokey.dll v2 from Powershell

Posted: 30 Aug 2022, 02:45
by thqby
mmexport1661845547296.png
mmexport1661845547296.png (50.04 KiB) Viewed 2564 times
@newvice