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.