OS build: 22621.1702,
AutoHotkey x64 v2.0.2.0
First time posting here, so apologies if I am doing something wrong.
I am now trying to implement an icon encoded with base64 in my script instead
of referencing an external (ico) file. Yes, I know that I can compile and specify
an icon, but I prefer the icon in the source script.
I searched and came across a function called Base64PNG_to_HICON,
by a chap called SKAN, found here:
This seems to be what I need, however it is for v1. I am trying to
convert it to v2, but have encountered an error that I cannot overcome:
requires a variable reference, but received an Integer.
Specifically: 0
036: Return DllCall("Crypt32.dll\CryptStringToBinary", "Str",Base64PNG,
"UInt",BLen, "UInt",1 ,"Ptr",&(Bin := VarSetStrCapacity(Bin, nBytes)),
"UIntP",nBytes, "UInt",0, "UInt",0) ? DllCall("CreateIconFromResourceEx",
"Ptr",&Bin, "UInt",nBytes, "Int",True, "UInt" ,0x30000, "Int",W, "Int",H,
"UInt",0, "UPtr") : 0
Here is my version of SKAN's function:
Code: Select all
Base64PNG_to_HICON(Base64PNG, W:=0, H:=0)
{
Local BLen := StrLen(Base64PNG), Bin:=0
Local nBytes := Floor(StrLen(RTrim(Base64PNG,"="))*3/4)
Return DllCall("Crypt32.dll\CryptStringToBinary", "Str",Base64PNG, "UInt",BLen, "UInt",1
,"Ptr",&(Bin := VarSetStrCapacity(Bin, nBytes)), "UIntP",nBytes, "UInt",0, "UInt",0)
? DllCall("CreateIconFromResourceEx", "Ptr",&Bin, "UInt",nBytes, "Int",True, "UInt"
,0x30000, "Int",W, "Int",H, "UInt",0, "UPtr")
: 0
}
I have searched for solutions from the forum, the web, and a few deities,
all to no avail.
Could someone with more experience point me in the right direction?
Any help would be very acceptable.
Here is my complete test script, ready to run:
Code: Select all
;-----------------------------------------------------------------------------
; Auto-execute section
;-----------------------------------------------------------------------------
#Requires AutoHotkey v2.0
#SingleInstance Force
#Warn
;-----------------------------------------------------------------------------
; Configuration and variables
;-----------------------------------------------------------------------------
Base64PNG := " ; Small 16px x 16px green leaf
(
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABpElEQVR42p2TQShEURiFb6ZmMS
sLWSilzEwUReyUBZmVjY2SKGWBBVKahSxISFHYGbFAEouRlDTKYiJFo4wFVsLC0kKN1fV/d+bq
zsukvDq9+95/zvnPf+97Sv1xtUyqSHNUrQpuBBp0LZXrzmW/jkyrTCFRQLArBD24UaHXkr36ID
Wqk29Rs75+X9HD29U6HFHdv4mnEC6ctOnEc1YUf+gwArD31KQnEj6SvAg94BXf0hERXXpiRYZs
DdfTYT17lX0nY6QLiiHSvX9LGTHxrRhgJgnuXPEMm3L/OWdEkIhNAmJz5z0Y2ZcE8aAWjf4xEL
cMAuabT1QZEsCUUcDR44DeTLWaOnevgREvJksNMXdEZk10ZrYYP1aGl2fAg52PPUDMcWHgFVt4
E1wwn7vjgCTMb+e2o4E8g8Y+NWTJ7DgnYOHOTgO7N9L01B1hBzEjIPLGdTvDo3tJSNUasUQ+tE
dHEXeICK0Rz3CoIZZPuF2kPtv9jGLO+VWKYxLvEqIHX/I+JpIGgd/9CINSOBfCh6xrBMWCUI7o
ol5QVuivrRTUqX9c354hhkwsQvG0AAAAAElFTkSuQmCC
)"
;-----------------------------------------------------------------------------
; Functions
;-----------------------------------------------------------------------------
; Based on 'Base64PNG_to_HICON' by SKAN:
; https://www.autohotkey.com/boards/viewtopic.php?p=168658#p168658
Base64PNG_to_HICON(Base64PNG, W:=0, H:=0)
{
Local BLen := StrLen(Base64PNG), Bin:=0
Local nBytes := Floor(StrLen(RTrim(Base64PNG,"="))*3/4)
Return DllCall("Crypt32.dll\CryptStringToBinary", "Str",Base64PNG, "UInt",BLen, "UInt",1
,"Ptr",&(Bin := VarSetStrCapacity(Bin, nBytes)), "UIntP",nBytes, "UInt",0, "UInt",0)
? DllCall("CreateIconFromResourceEx", "Ptr",&Bin, "UInt",nBytes, "Int",True, "UInt"
,0x30000, "Int",W, "Int",H, "UInt",0, "UPtr")
: 0
}
;-----------------------------------------------------------------------------
; Execution
;-----------------------------------------------------------------------------
TraySetIcon("HICON:" Base64PNG_to_HICON(Base64PNG))