summaryrefslogtreecommitdiff
path: root/prevlib/init_windows.go
blob: b8f11a5e6cfbdd054a6ea1b3c89a8baa90826312 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// 8 february 2014

package ui

import (
	"fmt"
	"unsafe"
)

var (
	hInstance _HANDLE
	nCmdShow  int
)

// TODO is this documented?
func getWinMainhInstance() (err error) {
	r1, _, err := kernel32.NewProc("GetModuleHandleW").Call(uintptr(_NULL))
	if r1 == 0 { // failure
		return err
	}
	hInstance = _HANDLE(r1)
	return nil
}

// this is what MinGW-w64 does (for instance, http://sourceforge.net/p/mingw-w64/code/6604/tree/trunk/mingw-w64-crt/crt/crtexe.c#l320); Burgundy in irc.freenode.net/#winapi said that the Visual C++ runtime does this too
func getWinMainnCmdShow() {
	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
	kernel32.NewProc("GetStartupInfoW").Call(uintptr(unsafe.Pointer(&info)))
	if info.dwFlags&_STARTF_USESHOWWINDOW != 0 {
		nCmdShow = int(info.wShowWindow)
	} else {
		nCmdShow = _SW_SHOWDEFAULT
	}
}

func doWindowsInit() (err error) {
	err = getWinMainhInstance()
	if err != nil {
		return fmt.Errorf("error getting WinMain hInstance: %v", err)
	}
	getWinMainnCmdShow()
	err = initWndClassInfo()
	if err != nil {
		return fmt.Errorf("error initializing standard window class auxiliary info: %v", err)
	}
	err = registerStdWndClass()
	if err != nil {
		return fmt.Errorf("error registering standard window class (for Window): %v", err)
	}
	err = registerAreaWndClass()
	if err != nil {
		return fmt.Errorf("error registering Area window class: %v", err)
	}
	err = getStandardWindowFonts()
	if err != nil {
		return fmt.Errorf("error getting standard window fonts: %v", err)
	}
	err = initCommonControls()
	if err != nil {
		return fmt.Errorf("error initializing Common Controls (comctl32.dll): %v", err)
	}
	// others go here
	return nil // all ready to go
}