GoでWin32apiのIDesktopWallpaperを使って壁紙の取得や変更などをしようとしています。
IDesktopWallpaperを扱えるライブラリが見つからないのでgo-oleを使って自作することにしました。
go-oleのole.IDispatchクラスの実装を参考にして、ole.IUnknownクラスを継承したIDesktopWallpaperクラスを作りました。
IDesktopWallpaperクラス作成時のSyscallNの呼び出しでは何も起こらないのですが、作成したクラスのメソッド内でのSyscallNの呼び出し時に以下のような例外が発生し、プログラムが強制終了してしまいます。
> go run main.go idesktopwallpaper.go
Exception 0xc0000005 0x0 0xffffffffffffffff 0x7ff97a88084a
PC=0x7ff97a88084a
runtime.cgocall(0x3c2360, 0x4bee08)
C:/Program Files/Go/src/runtime/cgocall.go:157 +0x3e fp=0xc000071e30 sp=0xc000071df8 pc=0x3641de
syscall.SyscallN(0x7ff96a927440?, {0xc00000a2c0?, 0x1?, 0x7ff96ab5c520?})
C:/Program Files/Go/src/runtime/syscall_windows.go:544 +0x107 fp=0xc000071ea8 sp=0xc000071e30 pc=0x3bdbe7
main.(*IDesktopWallpaper).Enable(0x209b6ff7378?, 0x0)
C:/Users/yukki/Desktop/idesktopwallpaper/idesktopwallpaper.go:54 +0x58 fp=0xc000071ef0 sp=0xc000071ea8 pc=0x3f5658
main.main()
C:/Users/yukki/Desktop/idesktopwallpaper/main.go:23 +0xbf fp=0xc000071f40 sp=0xc000071ef0 pc=0x3f541f
runtime.main()
C:/Program Files/Go/src/runtime/proc.go:267 +0x2b2 fp=0xc000071fe0 sp=0xc000071f40 pc=0x397352
runtime.goexit()
C:/Program Files/Go/src/runtime/asm_amd64.s:1650 +0x1 fp=0xc000071fe8 sp=0xc000071fe0 pc=0x3c0c21
goroutine 2 [force gc (idle)]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
C:/Program Files/Go/src/runtime/proc.go:398 +0xce fp=0xc00004bfa8 sp=0xc00004bf88 pc=0x39776e
runtime.goparkunlock(...)
C:/Program Files/Go/src/runtime/proc.go:404
runtime.forcegchelper()
C:/Program Files/Go/src/runtime/proc.go:322 +0xb8 fp=0xc00004bfe0 sp=0xc00004bfa8 pc=0x3975f8
runtime.goexit()
C:/Program Files/Go/src/runtime/asm_amd64.s:1650 +0x1 fp=0xc00004bfe8 sp=0xc00004bfe0 pc=0x3c0c21
created by runtime.init.6 in goroutine 1
C:/Program Files/Go/src/runtime/proc.go:310 +0x1a
goroutine 3 [GC sweep wait]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
C:/Program Files/Go/src/runtime/proc.go:398 +0xce fp=0xc00004df78 sp=0xc00004df58 pc=0x39776e
runtime.goparkunlock(...)
C:/Program Files/Go/src/runtime/proc.go:404
runtime.bgsweep(0x0?)
C:/Program Files/Go/src/runtime/mgcsweep.go:280 +0x94 fp=0xc00004dfc8 sp=0xc00004df78 pc=0x382834
runtime.gcenable.func1()
C:/Program Files/Go/src/runtime/mgc.go:200 +0x25 fp=0xc00004dfe0 sp=0xc00004dfc8 pc=0x377bc5
runtime.goexit()
C:/Program Files/Go/src/runtime/asm_amd64.s:1650 +0x1 fp=0xc00004dfe8 sp=0xc00004dfe0 pc=0x3c0c21
created by runtime.gcenable in goroutine 1
C:/Program Files/Go/src/runtime/mgc.go:200 +0x66
goroutine 4 [GC scavenge wait]:
runtime.gopark(0xc000056000?, 0x438a08?, 0x1?, 0x0?, 0xc000048b60?)
C:/Program Files/Go/src/runtime/proc.go:398 +0xce fp=0xc00005df70 sp=0xc00005df50 pc=0x39776e
runtime.goparkunlock(...)
C:/Program Files/Go/src/runtime/proc.go:404
runtime.(*scavengerState).park(0x4be6c0)
C:/Program Files/Go/src/runtime/mgcscavenge.go:425 +0x49 fp=0xc00005dfa0 sp=0xc00005df70 pc=0x380109
runtime.bgscavenge(0x0?)
C:/Program Files/Go/src/runtime/mgcscavenge.go:653 +0x3c fp=0xc00005dfc8 sp=0xc00005dfa0 pc=0x38069c
runtime.gcenable.func2()
C:/Program Files/Go/src/runtime/mgc.go:201 +0x25 fp=0xc00005dfe0 sp=0xc00005dfc8 pc=0x377b65
runtime.goexit()
C:/Program Files/Go/src/runtime/asm_amd64.s:1650 +0x1 fp=0xc00005dfe8 sp=0xc00005dfe0 pc=0x3c0c21
created by runtime.gcenable in goroutine 1
C:/Program Files/Go/src/runtime/mgc.go:201 +0xa5
goroutine 5 [finalizer wait]:
runtime.gopark(0x36b9fe?, 0x209b718c750?, 0x0?, 0x0?, 0x0?)
C:/Program Files/Go/src/runtime/proc.go:398 +0xce fp=0xc00004fe20 sp=0xc00004fe00 pc=0x39776e
runtime.runfinq()
C:/Program Files/Go/src/runtime/mfinal.go:193 +0x107 fp=0xc00004ffe0 sp=0xc00004fe20 pc=0x376c27
runtime.goexit()
C:/Program Files/Go/src/runtime/asm_amd64.s:1650 +0x1 fp=0xc00004ffe8 sp=0xc00004ffe0 pc=0x3c0c21
created by runtime.createfing in goroutine 1
C:/Program Files/Go/src/runtime/mfinal.go:163 +0x3d
rax 0xc00000a200
rbx 0x456aea02bf1ed004
rcx 0xb0959ff540
rdi 0x4
rsi 0xb0959ff540
rbp 0xb0959ff510
rsp 0xb0959ff160
r8 0x4
r9 0x0
r10 0xc00000a2c0
r11 0x0
r12 0x2c
r13 0x4beb40
r14 0xc000071ef8
r15 0x12
rip 0x7ff97a88084a
rflags 0x10206
cs 0x33
fs 0x53
gs 0x2b
exit status 2
コード
main.go
package main
import "github.com/go-ole/go-ole"
func main() {
// CoInitializeExを使用しても同じ問題が発生します。
if err := ole.CoInitialize(0); err != nil {
panic(err)
}
defer ole.CoUninitialize()
clsid := ole.NewGUID("C2CF3110-460E-4fc1-B9D0-8A1C0C9CC4BD")
iid := ole.NewGUID("B92B56A9-8B55-4E14-9A89-0199BBB6F93B")
unk, err := ole.CreateInstance(clsid, iid)
if err != nil {
panic(err)
}
defer unk.Release()
idesk, err := NewIDesktopWallpaper(unk)
if err != nil {
panic(err)
}
// Enable以外のメソッド内(GetMonitorDevicePathCountなど)でも同じ問題が発生します。
if err := idesk.Enable(false); err != nil {
panic(err)
}
}
idesktopwallpaper.go
package main
import (
"syscall"
"unsafe"
"github.com/go-ole/go-ole"
)
type IDesktopWallpaper struct {
ole.IUnknown
}
// https://learn.microsoft.com/ja-jp/windows/win32/api/shobjidl_core/nn-shobjidl_core-idesktopwallpaper
type IDesktopWallpaperVtbl struct {
ole.IUnknownVtbl
AdvanceSlideshow uintptr
Enable uintptr
GetBackgroundColor uintptr
GetMonitorDevicePathAt uintptr
GetMonitorDevicePathCount uintptr
GetMonitorRECT uintptr
GetPosition uintptr
GetSlideshow uintptr
GetSlideshowOptions uintptr
GetStatus uintptr
GetWallpaper uintptr
SetBackgroundColor uintptr
SetPosition uintptr
SetSlideshow uintptr
SetSlideshowOptions uintptr
SetWallpaper uintptr
}
func NewIDesktopWallpaper(unk *ole.IUnknown) (desk *IDesktopWallpaper, err error) {
iid := ole.NewGUID("B92B56A9-8B55-4E14-9A89-0199BBB6F93B")
if hr, _, _ := syscall.SyscallN(
unk.VTable().QueryInterface,
uintptr(unsafe.Pointer(unk)),
uintptr(unsafe.Pointer(iid)),
uintptr(unsafe.Pointer(&desk)),
); hr != 0 {
err = ole.NewError(hr)
}
return
}
func (v *IDesktopWallpaper) VTable() *IDesktopWallpaperVtbl {
return (*IDesktopWallpaperVtbl)(unsafe.Pointer(v.RawVTable))
}
// https://learn.microsoft.com/ja-jp/windows/win32/api/shobjidl_core/nf-shobjidl_core-idesktopwallpaper-enable
func (v *IDesktopWallpaper) Enable(enable bool) error {
if hr, _, _ := syscall.SyscallN(
v.VTable().Enable,
uintptr(unsafe.Pointer(&enable)),
); hr != 0 {
return ole.NewError(hr)
}
return nil
}
// https://learn.microsoft.com/ja-jp/windows/win32/api/shobjidl_core/nf-shobjidl_core-idesktopwallpaper-getmonitordevicepathcount
func (v *IDesktopWallpaper) GetMonitorDevicePathCount() (count uint, err error) {
if hr, _, _ := syscall.SyscallN(
v.VTable().GetMonitorDevicePathCount,
uintptr(unsafe.Pointer(&count)),
); hr != 0 {
return 0, ole.NewError(hr)
}
return
}
// https://learn.microsoft.com/ja-jp/windows/win32/api/shobjidl_core/nf-shobjidl_core-idesktopwallpaper-getposition
func (v *IDesktopWallpaper) GetPosition() (position int, err error) {
if hr, _, _ := syscall.SyscallN(
v.VTable().GetPosition,
uintptr(unsafe.Pointer(&position)),
); hr != 0 {
return -1, ole.NewError(hr)
}
return
}
// https://learn.microsoft.com/ja-jp/windows/win32/api/shobjidl_core/nf-shobjidl_core-idesktopwallpaper-setposition
func (v *IDesktopWallpaper) SetPosition(position int) error {
if hr, _, _ := syscall.SyscallN(
v.VTable().SetPosition,
uintptr(unsafe.Pointer(&position)),
); hr != 0 {
return ole.NewError(hr)
}
return nil
}
Goのバージョン
go version go1.21.5 windows/amd64
NULL
参照エラーが出たのであればご自身で原因箇所を確認し「NULLとなるべきではない箇所でNULLとなった旨」を質問された方がよいでしょう。