Skip to content
This repository has been archived by the owner on Aug 23, 2019. It is now read-only.

Commit

Permalink
resolve #15
Browse files Browse the repository at this point in the history
  • Loading branch information
ergoz committed Aug 11, 2019
1 parent c736118 commit ceebcfa
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
5 changes: 5 additions & 0 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3084,3 +3084,8 @@ const (
NIIF_RESPECT_QUIET_TIME = 0x00000080
NIIF_ICON_MASK = 0x0000000F
)

const (
MF_BYCOMMAND = 0x00000000
MF_BYPOSITION = 0x00000400
)
2 changes: 1 addition & 1 deletion kernel32.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func CreateProcessA(lpApplicationName string,
uintptr(unsafe.Pointer(lpStartupInfo)),
uintptr(unsafe.Pointer(lpProcessInformation)))

if ret == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
if ret == 0 && !IsErrSuccess(err) {
return false, err
}
return ret != 0, nil
Expand Down
28 changes: 20 additions & 8 deletions user32.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ var (
procGetSubMenu = moduser32.NewProc("GetSubMenu")
procRegisterWindowMessageW = moduser32.NewProc("RegisterWindowMessageW")
procGetMenuItemID = moduser32.NewProc("GetMenuItemID")
procDeleteMenu = moduser32.NewProc("DeleteMenu")
)

func SendMessageTimeout(hwnd HWND, msg uint32, wParam, lParam uintptr, fuFlags, uTimeout uint32, lpdwResult uintptr) uintptr {
Expand Down Expand Up @@ -1234,8 +1235,8 @@ func UnregisterClass(lpClassName string, hInstance HINSTANCE) bool {
// See https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-createpopupmenu
func CreatePopupMenu() (HMENU, error) {
ret, _, err := procCreatePopupMenu.Call()
if ret == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
return HMENU(ret), err
if !IsErrSuccess(err) {
return HMENU(nil), err
}
return HMENU(ret), nil
}
Expand All @@ -1245,8 +1246,8 @@ func CreatePopupMenu() (HMENU, error) {
// See https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-createmenu
func CreateMenu() (HMENU, error) {
ret, _, err := procCreateMenu.Call()
if ret == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
return HMENU(ret), err
if !IsErrSuccess(err) {
return HMENU(nil), err
}
return HMENU(ret), nil
}
Expand All @@ -1255,7 +1256,7 @@ func CreateMenu() (HMENU, error) {
// See https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-destroymenu
func DestroyMenu(hMenu HMENU) (bool, error) {
ret, _, err := procDestroyMenu.Call(uintptr(hMenu))
if ret == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
if !IsErrSuccess(err) {
return false, err
}
return ret != 0, nil
Expand All @@ -1269,7 +1270,7 @@ func DestroyMenu(hMenu HMENU) (bool, error) {
// See https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getsubmenu
func GetSubMenu(hMenu HMENU, nPos int) (HMENU, error) {
ret, _, err := procGetSubMenu.Call(uintptr(hMenu), uintptr(nPos))
if ret == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
if !IsErrSuccess(err) {
return HMENU(nil), err
}
return HMENU(ret), nil
Expand All @@ -1281,7 +1282,7 @@ func GetSubMenu(hMenu HMENU, nPos int) (HMENU, error) {
// See https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-registerwindowmessagew
func RegisterWindowMessage(lpString string) (bool, error) {
ret, _, err := procRegisterWindowMessageW.Call(uintptr(pointerStringWithoutError(lpString)))
if ret == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
if !IsErrSuccess(err) {
return false, err
}
return ret != 0, nil
Expand All @@ -1291,8 +1292,19 @@ func RegisterWindowMessage(lpString string) (bool, error) {
// See https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getmenuitemid
func GetMenuItemID(hMenu HMENU, nPos int) (int32, error) {
ret, _, err := procGetMenuItemID.Call(uintptr(hMenu), uintptr(nPos))
if err.(syscall.Errno) != ERROR_SUCCESS {
if !IsErrSuccess(err) {
return -1, err
}
return int32(ret), nil
}

// DeleteMenu Deletes an item from the specified menu. If the menu item opens a menu or submenu, this function
// destroys the handle to the menu or submenu and frees the memory used by the menu or submenu.
// See https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-deletemenu
func DeleteMenu(hMenu HMENU, uPosition uint, uFlags uint) (bool, error) {
ret, _, err := procGetMenuItemID.Call(uintptr(hMenu), uintptr(uPosition), uintptr(uFlags))
if !IsErrSuccess(err) {
return false, err
}
return ret != 0, nil
}
2 changes: 1 addition & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func HexToUint32(hexString string) (result uint32, err error) {
// localized depending on the OS language.
func IsErrSuccess(err error) bool {
if errno, ok := err.(syscall.Errno); ok {
if errno == 0 {
if errno == ERROR_SUCCESS {
return true
}
}
Expand Down

0 comments on commit ceebcfa

Please sign in to comment.