Skip to content

Instantly share code, notes, and snippets.

@Lexikos
Created September 9, 2022 23:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lexikos/6ce4c01c73b2d8ad25221745564736c9 to your computer and use it in GitHub Desktop.
Save Lexikos/6ce4c01c73b2d8ad25221745564736c9 to your computer and use it in GitHub Desktop.
Use AutoHotkey v1 libraries in a v2 script by making use of AutoHotkey.dll
; Example 1: Add some code and import a function.
v1 '
(
MakeProgress(Param1:="", Sub:="", Main:="", Title:="", Font:="") {
Progress % Param1, % Sub, % Main, % Title, % Font
}
)'
Progress := v1_func('MakeProgress')
Progress , "Imaginative subtext", "Loading example ..."
Loop 10 {
Sleep 500
Progress A_Index*10
}
Sleep 1000
Progress "Off"
; Example 2: Import a class library where filename matches class name.
; cJSON.ahk : https://www.autohotkey.com/boards/viewtopic.php?f=6&t=92320
; JSON.ahk must be placed in an appropriate Lib folder.
JSON := v1_class_lib('JSON')
; Create some JSON
str := '["abc", 123, {"true": 1, "false": 0, "null": ""}, [true, false, null]]'
obj := JSON.Load(str)
MsgBox obj.1 ; abc
MsgBox obj.2 ; 123
MsgBox obj.3.true ; 1
MsgBox obj.3.false ; 0
MsgBox obj.3.null ; *nothing*
MsgBox obj.4[1] ; 1
MsgBox obj.4[2] ; 0
MsgBox obj.4[3] == JSON.Null ; 1
; ================================================================
v1(code) {
static hThread := DllCall("AutoHotkey.dll\ahktextdll", "Str", "
(
#Persistent
MarshalFunc(name) {
if !f := Func(name)
return 0
return MarshalObject(f)
}
MarshalClass(name) {
if !IsObject(c := %name%)
return 0
return MarshalObject(c)
}
MarshalObject(obj) {
static IID_IDispatch
VarSetCapacity(IID_IDispatch) || (VarSetCapacity(IID_IDispatch, 16), NumPut(0x46000000000000C0, NumPut(0x0000000000020400, IID_IDispatch, "int64"), "int64"))
r := DllCall("ole32\CoMarshalInterThreadInterfaceInStream"
, "ptr", &IID_IDispatch
, "ptr", &obj
, "ptr*", pstm:=0, "int")
return pstm
}
)" , "Str", "", "Str", "", "ptr")
DllCall("AutoHotkey.dll\addScript", "str", code, "int", 1, "cdecl ptr")
}
v1_func(name) {
if !pstm := Integer(DllCall("AutoHotkey.dll\ahkFunction", "str", "MarshalFunc", "str", name, "ptr", 0, "ptr", 0, "ptr", 0, "ptr", 0, "ptr", 0, "ptr", 0, "ptr", 0, "ptr", 0, "ptr", 0, "cdecl str"))
throw ValueError("Function not found", -1, name)
static IID_IDispatch
DllCall("ole32\CoGetInterfaceAndReleaseStream"
, "ptr", pstm
, "ptr", IID_IDispatch ?? NumPut('int64', 0x0000000000020400, 'int64', 0x46000000000000C0, IID_IDispatch := Buffer(16)) - 16
, "ptr*", f := ComValue(9, 0)
, "hresult")
return f
}
v1_class_lib(name) {
v1('#include <' name '>')
return v1_class(name)
}
v1_class(name) {
if !pstm := Integer(DllCall("AutoHotkey.dll\ahkFunction", "str", "MarshalClass", "str", name, "ptr", 0, "ptr", 0, "ptr", 0, "ptr", 0, "ptr", 0, "ptr", 0, "ptr", 0, "ptr", 0, "ptr", 0, "cdecl str"))
throw ValueError("Class not found", -1, name)
static IID_IDispatch
DllCall("ole32\CoGetInterfaceAndReleaseStream"
, "ptr", pstm
, "ptr", IID_IDispatch ?? NumPut('int64', 0x0000000000020400, 'int64', 0x46000000000000C0, IID_IDispatch := Buffer(16)) - 16
, "ptr*", c := ComValue(9, 0)
, "hresult")
return c
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment