From b727a972adbc90c3cdfbff2c462a583aefd48474 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Tue, 11 Feb 2014 16:25:27 -0500 Subject: Set up initialization on Windows (2/2). --- init_windows.go | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ uitask_windows.go | 5 +--- winmain_windows.go | 77 ------------------------------------------------------ 3 files changed, 78 insertions(+), 81 deletions(-) create mode 100644 init_windows.go delete mode 100644 winmain_windows.go diff --git a/init_windows.go b/init_windows.go new file mode 100644 index 0000000..bea6516 --- /dev/null +++ b/init_windows.go @@ -0,0 +1,77 @@ +// 8 february 2014 +package main + +import ( + "fmt" +// "syscall" + "unsafe" +) + +const ( + windowclass = "gouiwndclass" +) + +var ( + hInstance HANDLE + nCmdShow int + // TODO font + // TODO common window class +) + +// TODO is this trick documented in MSDN? +func getWinMainhInstance() (err error) { + r1, _, err := kernel32.NewProc("GetModuleHandleW").Call(uintptr(NULL)) + if r1 == 0 { // failure + return err + } + hInstance = HANDLE(r1) + return 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 + } + const _STARTF_USESHOWWINDOW = 0x00000001 + + // does not fail according to MSDN + kernel32.NewProc("GetStartupInfoW").Call(uintptr(unsafe.Pointer(&info))) + if info.dwFlags & _STARTF_USESHOWWINDOW != 0 { + nCmdShow = int(info.wShowWindow) + return nil + } + nCmdShow = _SW_SHOWDEFAULT + return nil +} + +func doWindowsInit() (err error) { + err = getWinMainhInstance() + if err != nil { + return fmt.Errorf("error getting WinMain hInstance: %v", err) + } + err = getWinMainnCmdShow() + if err != nil { + return fmt.Errorf("error getting WinMain nCmdShow: %v", err) + } + // TODO others + return nil // all ready to go +} diff --git a/uitask_windows.go b/uitask_windows.go index 4f0f04f..8fe705c 100644 --- a/uitask_windows.go +++ b/uitask_windows.go @@ -24,11 +24,8 @@ type uiret struct { func ui(initDone chan error) { runtime.LockOSThread() - // initialize hInstance - // initialize nCmdShow - // initialize the common window class uitask = make(chan *uimsg) - initDone <- nil + initDone <- doWindowsInit() for m := range uitask { r1, _, err := m.msg.Call(m.p...) diff --git a/winmain_windows.go b/winmain_windows.go deleted file mode 100644 index bea6516..0000000 --- a/winmain_windows.go +++ /dev/null @@ -1,77 +0,0 @@ -// 8 february 2014 -package main - -import ( - "fmt" -// "syscall" - "unsafe" -) - -const ( - windowclass = "gouiwndclass" -) - -var ( - hInstance HANDLE - nCmdShow int - // TODO font - // TODO common window class -) - -// TODO is this trick documented in MSDN? -func getWinMainhInstance() (err error) { - r1, _, err := kernel32.NewProc("GetModuleHandleW").Call(uintptr(NULL)) - if r1 == 0 { // failure - return err - } - hInstance = HANDLE(r1) - return 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 - } - const _STARTF_USESHOWWINDOW = 0x00000001 - - // does not fail according to MSDN - kernel32.NewProc("GetStartupInfoW").Call(uintptr(unsafe.Pointer(&info))) - if info.dwFlags & _STARTF_USESHOWWINDOW != 0 { - nCmdShow = int(info.wShowWindow) - return nil - } - nCmdShow = _SW_SHOWDEFAULT - return nil -} - -func doWindowsInit() (err error) { - err = getWinMainhInstance() - if err != nil { - return fmt.Errorf("error getting WinMain hInstance: %v", err) - } - err = getWinMainnCmdShow() - if err != nil { - return fmt.Errorf("error getting WinMain nCmdShow: %v", err) - } - // TODO others - return nil // all ready to go -} -- cgit v1.2.3