summaryrefslogtreecommitdiff
path: root/winmain.go
blob: b79ad815f01b0c124cd74941a01ac055df41e5c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// 8 february 2014
package main

import (
//	"syscall"
	"unsafe"
)

// this provides the hInstance and nCmdShow that are normally passed to WinMain()

const (
	STARTF_USESHOWWINDOW = 0x00000001
)

var (
	getModuleHandle = kernel32.NewProc("GetModuleHandleW")
	getStartupInfo = kernel32.NewProc("GetStartupInfoW")
)

// TODO is this trick documented in MSDN?
func getWinMainhInstance() (hInstance HANDLE, err error) {
	r1, _, err := getModuleHandle.Call(uintptr(NULL))
	if r1 == 0 {
		return NULL, err
	}
	return HANDLE(r1), nil
}

// TODO this is what MinGW-w64's crt (svn revision xxx) does; is it best? is any of this documented anywhere on MSDN?
// TODO I highly doubt Windows API functions ever not fail, so figure out what to do should an error actually occur
func getWinMainnCmdShow() (nCmdShow int, err error) {
	var info struct {
		cb				uint32
		lpReserved		*uint16
		lpDesktop			*uint16
		lpTitle			*uint16
		dwX				uint32
		dwY				uint32
		dwXSize			uint32
		dwYSzie			uint32
		dwXCountChars	uint32
		dwYCountChars	uint32
		dwFillAttribute		uint32
		dwFlags			uint32
		wShowWindow		uint16
		cbReserved2		uint16
		lpReserved2		*byte
		hStdInput			HANDLE
		hStdOutput		HANDLE
		hStdError			HANDLE
	}

	// does not fail according to MSDN
	getStartupInfo.Call(uintptr(unsafe.Pointer(&info)))
	if info.dwFlags & STARTF_USESHOWWINDOW != 0 {
		return int(info.wShowWindow), nil
	}
	return SW_SHOWDEFAULT, nil
}