Go/window_api

[Go] Windows API - 마우스 커서 위치 찾기

_HelloWorld_ 2025. 3. 5. 11:53
package main

import (
	"fmt"

	"syscall"
	"win_api/cursor"
)

func main() {
	mod := syscall.NewLazyDLL("user32.dll")

	pt, err := cursor.GetCursorPosition(mod)
	if err != nil {
		panic(err)
	}

	fmt.Println(pt)
}
package cursor

import (
	"syscall"
	"unsafe"
)

type Point struct {
	X, Y int32
}

func GetCursorPosition(mod *syscall.LazyDLL) (*Point, error) {
	proc := mod.NewProc("GetCursorPos")

	point := &Point{}

	ret, _, err := proc.Call(uintptr(unsafe.Pointer(point)))
	if ret == 0 {
		return nil, err
	}

	return point, nil
}

 

설명

  • syscall.NewLazyDLL → user32.dll 로드
  • NewProc → GetCursorPos 함수 찾기
  • unsafe.Pointer → Go 구조체 포인터를 C 포인터로 변환
  • proc.Call → API 호출 (리턴값 확인)