L版本AHK通过调用H版本autohotkey.dll实现多线程

脚本路径下需要有autohotkey.ahk

https://www.autohotkey.com/boards/viewtopic.php?f=65&t=28803

保存下面脚本为script1.ahk

;script1
#NoEnv
#NoTrayIcon

TargetScriptTitle := "mutithread.ahk ahk_class AutoHotkey"
main()
return

main()
{
	global TargetScriptTitle
	StringToSend := "I'm a thread"
	result := Send_WM_COPYDATA(StringToSend, TargetScriptTitle)
	if (result = "FAIL")
		MsgBox SendMessage failed. Does the following WinTitle exist?:
%TargetScriptTitle%
	else if (result = 0)
		MsgBox Message sent but the target window responded with 0, which may mean it ignored it.
	return
}

Send_WM_COPYDATA(ByRef StringToSend, ByRef TargetScriptTitle)  ; 在这种情况中使用 ByRef 能节约一些内存.
; 此函数发送指定的字符串到指定的窗口然后返回收到的回复.
; 如果目标窗口处理了消息则回复为 1, 而消息被忽略了则为 0.
{
    VarSetCapacity(CopyDataStruct, 3*A_PtrSize, 0)  ; 分配结构的内存区域.
    ; 首先设置结构的 cbData 成员为字符串的大小, 包括它的零终止符:
    SizeInBytes := (StrLen(StringToSend) + 1) * (A_IsUnicode ? 2 : 1)
    NumPut(SizeInBytes, CopyDataStruct, A_PtrSize)  ; 操作系统要求这个需要完成.
    NumPut(&StringToSend, CopyDataStruct, 2*A_PtrSize)  ; 设置 lpData 为到字符串自身的指针.
    Prev_DetectHiddenWindows := A_DetectHiddenWindows
    Prev_TitleMatchMode := A_TitleMatchMode
    DetectHiddenWindows On
    SetTitleMatchMode 2
    TimeOutTime := 4000  ; Optional. Milliseconds to wait for response from receiver.ahk. Default is 5000
    ; 必须使用发送 SendMessage 而不是投递 PostMessage.
    SendMessage, 0x4a, 0, &CopyDataStruct,, %TargetScriptTitle%  ; 0x4a 为 WM_COPYDAT
    DetectHiddenWindows %Prev_DetectHiddenWindows%  ; 恢复调用者原来的设置.
    SetTitleMatchMode %Prev_TitleMatchMode%         ; 同样.
    return ErrorLevel  ; 返回 SendMessage 的回复给我们的调用者.
}

 

保存下面脚本为mutithread.ahk

;mutithread.ahk
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
OnMessage(0x4a, "Receive_WM_COPYDATA")  ; 0x4a 为 WM_COPYDATA
gui,1:add, Button,section gthread ,runThread
gui,1:add,Edit,vThreadValue
gui,1:Show
return

thread()
{
    AhkDllPath := A_ScriptDir "AutoHotkey.dll"
    hModule := DllCall("LoadLibrary","Str",AhkDllPath)
    DllCall(AhkDllPath "ahkdll","Str","script1.ahk","Str","","Str","","Cdecl UPTR")
    return
}

Receive_WM_COPYDATA(wParam, lParam)
{
    global ThreadValue
    StringAddress := NumGet(lParam + 2*A_PtrSize)  ; 获取 CopyDataStruct 的 lpData 成员.
    CopyOfData := StrGet(StringAddress)  ; 从结构中复制字符串.
    GuiControl, , ThreadValue , %CopyOfData%
    return true  ; 返回 1 (true) 是回复此消息的传统方式.
}

给TA捐赠
共{{data.count}}人
人已捐赠
教程

按键码的获取和使用

2020-9-25 14:25:50

教程

【答圈子】sleep问题

2020-10-29 22:15:00

2 条回复 A文章作者 M管理员
  1. ╲

    谢谢

  2. jiahao

    厉害

个人中心
购物车
优惠劵
有新私信 私信列表
搜索