Skip to content

Instantly share code, notes, and snippets.

@jonatino
Created October 25, 2020 04:38
Show Gist options
  • Save jonatino/2be806f284e23ea7a9e23a8388ef529d to your computer and use it in GitHub Desktop.
Save jonatino/2be806f284e23ea7a9e23a8388ef529d to your computer and use it in GitHub Desktop.
Ultralight Workaround for Favicons - Windows 10
// +build windows
package main
import (
"os"
"syscall"
"unsafe"
)
var (
_user32 = syscall.MustLoadDLL("user32.dll")
procEnumWindows = _user32.MustFindProc("EnumWindows")
getProcessIdFromWindow = _user32.MustFindProc("GetWindowThreadProcessId")
loadImage = _user32.MustFindProc("LoadImageW")
sendMessage = _user32.MustFindProc("SendMessageW")
)
func LoadImage(hwnd syscall.Handle, lpsz *uint16, uType uint, cxDesired int, cyDesired int, fuLoad uint) (len uintptr, err error) {
r0, _, e1 := syscall.Syscall6(loadImage.Addr(), 6, uintptr(hwnd), uintptr(unsafe.Pointer(lpsz)), uintptr(uType), uintptr(cxDesired), uintptr(cyDesired), uintptr(fuLoad))
len = r0
if len == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}
func SendMessage(hwnd syscall.Handle, msg uint, wParam int, lParam uintptr) (len int32, err error) {
syscall.Syscall6(sendMessage.Addr(), 4, uintptr(hwnd), uintptr(msg), uintptr(wParam), lParam, 0, 0)
return
}
func EnumWindows(enumFunc uintptr, lparam uintptr) (err error) {
syscall.Syscall(procEnumWindows.Addr(), 2, enumFunc, lparam, 0)
return
}
func GetProcessIdFromWindow(hwnd syscall.Handle, processId *uint64) (len int32, err error) {
r0, _, e1 := syscall.Syscall(getProcessIdFromWindow.Addr(), 2, uintptr(hwnd), uintptr(unsafe.Pointer(processId)), 0)
len = int32(r0)
if len == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}
func UpdateIcon() {
name, _ := syscall.UTF16PtrFromString("icon.ico")
zero := syscall.Handle(0)
img, _ := LoadImage(zero, name, 1, 16, 16, 0x10)
cb := syscall.NewCallback(func(h syscall.Handle, p uintptr) uintptr {
var b uint64 = 0
GetProcessIdFromWindow(h, &b)
if b == uint64(os.Getpid()) {
SendMessage(h, 0x80, 0, img)
return 0
}
return 1 // continue enumeration
})
EnumWindows(cb, 0)
}
@jonatino
Copy link
Author

Use it by calling UpdateIcon() after creating an Ultralight window.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment