原贴链接:https://www.autohotkey.com/boards/viewtopic.php?f=83&t=98890

 

使用v1的autohotkey.exeautohotkey.dll加载库文件,并返回一个可以使用的对象,如下所示:

; load v1 librarys
lib := import_v1lib("LibPath", "AHK.exe")
lib := import_v1lib(["Lib1Path", "Lib2Path"], "AHK.exe")
lib := import_v1lib("test(a){`n}", "AHK.dll")

; Call a function named 'Function' in the target library:
lib.Function(params)
lib.Function

; Retrieve a global variable:
value := lib["VarName"]

; Set a global variable:
lib["VarName"] := value
lib.VarName := value

; Create a class instance
value := lib["__AHKv1LibHelper"].__New("ClassName", params)
value := lib["__AHKv1LibHelper"].__New(lib["ClassName"].NestedClass, params)

 

所有必需的函数都应安装在函数库中。

/**
 * import ahk v1 library to v2. It can call v1 functions and classes, read and modify global variables.
 * 
 * - import v1 lib `v1 := import_v1lib("gdip_all.ahk", "ahkv1.exe")`
 * - create class instance `v1["__AHKv1LibHelper"].__New("classname", params)` or `v1["__AHKv1LibHelper"].__New(v1["classname"], params)`
 * - call functions `v1.funcname(params)`, *no param* `v1.funcname`
 * - set global variable `v1.varname := "str"` or `v1["varname"] := {}`
 * - retrieve global variable `MsgBox(v1["varname"])`
 * 
 * @param pathsorcode The paths of the include script files or ahk v1 code. `Array or String`
 * @param ahkv1runtime The path of the AutoHotkey v1 runtime (AutoHotkey.exe or AutoHotkey.dll).
 */
import_v1lib(pathsorcode, ahkv1runtime := "C:\Program Files\AutoHotkey\AutoHotkey.exe") {
  static IDispatch := Buffer(16), _ := NumPut("int64", 0x20400, "int64", 0x46000000000000c0, IDispatch)
  lresult := DllCall("oleacc\LresultFromObject", "ptr", IDispatch, "ptr", 0, "ptr", ObjPtr(client := { proxy: 0 }), "ptr")
  ; free com marshal on fail
  autofree := { ptr: lresult, __Delete: (s) => DllCall("oleacc\ObjectFromLresult", "ptr", s, "ptr", IDispatch, "ptr", 0, "ptr*", ComValue(9, 0)) }
  if !FileExist(ahkv1runtime)
    throw Error("AutoHotkey v1 runtime isn't exist")
  if pathsorcode is Array {
    t := pathsorcode, pathsorcode := ""
    for p in t
      pathsorcode .= "`n#include " p
  }
  v1script := Format("
  (
    #Persistent
    #NoTrayIcon
    class __AHKv1LibHelper {
      __New(name, args*) {
        static _ := (VarSetCapacity(IDispatch, 16), NumPut(0x46000000000000c0, NumPut(0x20400, IDispatch, "int64"), "int64"), DllCall("oleacc\ObjectFromLresult", "ptr", {}, "ptr", &IDispatch, "ptr", 0, "ptr*", idisp := 0), ComObject(9, idisp).proxy := new __AHKv1LibHelper(), ObjRelease(idisp))
        return IsObject(name) ? new name(args*) : new %name%(args*)
      }
      __Call(name, args*) {
        return %name%(args*)
      }
      __Get(name) {
        global
        return val := %name%
      }
      __Set(name, val) {
        global
        return %name% := val
      }
      __Delete() {
        SetTimer ExitApp, -1
        return
        ExitApp:
          ExitApp
      }
    }
    {}
  )", lresult, FileExist(pathsorcode) ? "#include " pathsorcode : pathsorcode)
  if ahkv1runtime ~= "i)\.dll$" {
    if !DllCall("GetModuleHandle", "str", ahkv1runtime) && !DllCall("LoadLibrary", "str", ahkv1runtime)
      throw Error("load AutoHotkey.dll fail")
    if !hThread := DllCall(ahkv1runtime "\ahktextdll", "str", v1script, "str", "", "str", "", "cdecl ptr")
      throw Error("Failed to load script")
    while !client.proxy
      Sleep(10)
  } else {
    exec := ComObject("WScript.Shell").Exec('"' ahkv1runtime '" /ErrorStdOut *')
    exec.StdIn.Write(v1script), exec.StdIn.Close()
    while exec.Status = 0 && !client.proxy
      Sleep(10)
    if exec.Status != 0 {
      ex := Error("Failed to load script")
      if RegExMatch(err := exec.StdErr.ReadAll(), "s)(.*?) \((\d+)\) : ==> (.*?)(?:\s*Specifically: (.*?))?\R?$", &m)
        ex.Message .= "`n`nReason:`t" m[3] "`nLine text:`t" m[4] "`nFile:`t" m[1] "`nLine:`t" m[2]
      throw ex
    }
  }
  autofree.DeleteProp("__Delete")
  return client.proxy
}

 

声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。