Autohokey.dll v2 from Powershell

Ask for help, how to use AHK_H, etc.
newvice
Posts: 24
Joined: 12 Jun 2021, 09:20

Autohokey.dll v2 from Powershell

Post by newvice » 23 Aug 2022, 14:09

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.

newvice
Posts: 24
Joined: 12 Jun 2021, 09:20

Re: Autohokey.dll v2 from Powershell

Post by newvice » 24 Aug 2022, 15:41

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.

guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Autohokey.dll v2 from Powershell

Post by guest3456 » 24 Aug 2022, 22:51

why are you trying to use NewThread in the v2 dll?

what about using the normal dll funcs?


newvice
Posts: 24
Joined: 12 Jun 2021, 09:20

Re: Autohokey.dll v2 from Powershell

Post by newvice » 25 Aug 2022, 13:56

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?

User avatar
thqby
Posts: 408
Joined: 16 Apr 2021, 11:18
Contact:

Re: Autohokey.dll v2 from Powershell

Post by thqby » 30 Aug 2022, 02:45

mmexport1661845547296.png
mmexport1661845547296.png (50.04 KiB) Viewed 2396 times
@newvice

Post Reply

Return to “Ask for Help”