Menu, ..., Rename error (bug?)

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Menu, ..., Rename error (bug?)

12 Nov 2018, 07:16

The following code

Code: Select all

#NoEnv
Menu, CtxMenu, Add, Start, MenuHandler
Menu, CtxMenu, Add, Exit, GuiClose
HMENU := MenuGetHandle("CtxMenu")
Gui, Show, w400 h400, Right-Click ContextMenu
Return

GuiClose:
ExitApp

GuiContextMenu:
NewName := "Start"
Menu, CtxMenu, Rename, Start, %NewName%
; If !DllCall("ModifyMenu", "Ptr", HMENU, "UInt", 0, "UInt", 0x0400, "UIntP", Dummy, "Str", NewName, "UInt")
;    MsgBox, 16, %A_ThisLabel%, ModifyMenu failed with Error %A_LAstError%!
Menu, CtxMenu, Show
Return

MenuHandler:
MenuItem := A_ThisMenuItem
MsgBox, 0, %A_ThisLabel%, You chose %MenuItem% from the menu!
Return
produces the error message

Code: Select all

---------------------------
H6062862.ahk
---------------------------
Error:  Rename failed (name too long?).

Specifically: Start

	Line#
	043: Menu,CtxMenu,Add,Start,MenuHandler
	044: Menu,CtxMenu,Add,Exit,GuiClose
	045: HMENU := MenuGetHandle("CtxMenu")
	046: Gui,Show,w400 h400,Right-Click ContextMenu
	047: Return
	050: ExitApp
	053: NewName := "Start"
--->	054: Menu,CtxMenu,Rename,Start,%NewName%
	057: Menu,CtxMenu,Show
	058: Return
	061: MenuItem := A_ThisMenuItem
	062: MsgBox,0,%A_ThisLabel%,You chose %MenuItem% from the menu!
	063: Return
	064: Exit
	065: Exit

The current thread will exit.
---------------------------
OK
---------------------------
ModifyMenu is working, so I suppose it's an AHK bug.

Edit:
Think I found the bug in script_menu.cpp -> RenameItem():

Code: Select all

	if (*aNewName)
	{
		// Names must be unique only within each menu:
		for (UserMenuItem *mi = mFirstMenuItem; mi; mi = mi->mNextMenuItem)
			if (!lstrcmpi(mi->mName, aNewName)) // Match found (case insensitive).
				return FAIL; // Caller should display an error message.
CyL0N
Posts: 211
Joined: 27 Sep 2018, 09:58

Re: Menu, ..., Rename error (bug?)

12 Nov 2018, 11:40

It seems to have been around for ages same error in 1.24(tested out of curiousity)... Only happens if attempting to rename to it's own name, though. // Names must be unique only within each menu:, so might actually be intentional,given the error is generated when a unique name is not used?!?
live ? long & prosper : regards
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Menu, ..., Rename error (bug?)

12 Nov 2018, 11:48

If you rename a menu item with its own name, the name stays unique within the menu.
CyL0N
Posts: 211
Joined: 27 Sep 2018, 09:58

Re: Menu, ..., Rename error (bug?)

12 Nov 2018, 11:55

just me wrote:
12 Nov 2018, 11:48
If you rename a menu item with its own name, the name stays unique within the menu.
That doesn't sound right... If it was this should work,No?

Code: Select all

#NoEnv
Menu, CtxMenu, Add, Start, MenuHandler
Menu, CtxMenu, Add, Exit, GuiClose
HMENU := MenuGetHandle("CtxMenu")
Gui, Show, w400 h400, Right-Click ContextMenu
Menu, CtxMenu, Rename, Start, Start

MenuHandler:
GuiClose:
Return
live ? long & prosper : regards
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Menu, ..., Rename error (bug?)

12 Nov 2018, 12:07

That doesn't sound right...
I don't understand. If you have one menu item named Start and replace this item's name with Start the name Start still occurs only once within the menu.

This kind of 'renaming' doesn't make sense, but it shouldn't terminate the current thread.
CyL0N
Posts: 211
Joined: 27 Sep 2018, 09:58

Re: Menu, ..., Rename error (bug?)

12 Nov 2018, 12:21

just me wrote:
12 Nov 2018, 12:07
That doesn't sound right...
I don't understand. If you have one menu item named Start and replace this item's name with Start the name Start still occurs only once within the menu.
If something already exists,even if you're trying to replace it,then the object with which your replacing it with is no longer unique.
just me wrote:
12 Nov 2018, 12:07
This kind of 'renaming' doesn't make sense, but it shouldn't terminate the current thread.
Agreed, THIS i think is the bug.
live ? long & prosper : regards
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: Menu, ..., Rename error (bug?)

16 Nov 2018, 04:17

Yes, it's a bug.

Supposing that menu item names must be unique, the obvious way to ensure this is to check for any item which already has the new name and abort if one is found (and that's what the code does). Allowing "renaming" without any actual change would require either detecting that condition (newname = oldname) or detecting when the found item is the item being renamed. Either way, it requires additional code that probably wouldn't be written if the developer hasn't considered the possibility of a no-op rename.

Note that renaming from "Start" to "start" or "START" is also impossible without first renaming to something else.

Also note that the check is only performed if the menu has a Win32 handle (i.e. you have called MenuGetHandle(), shown the menu or added it to a GUI). That was probably never intentional.

Anyway, menu item names don't have to be unique. This check should have been removed when the Insert sub-command was added, or when the capability to address items by position was added. I probably thought it already had when I changed the error message from "Menu item name already in use (or too long)." to "Rename failed (name too long?)."

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 276 guests