The documentation for SldWorks::AddMenuItem2 states the following:
================================ Syntax (COM)
status = SldWorks->AddMenuItem2 ( DocumentType, Cookie, MenuItem, Position, MenuCallback, MenuEnableMethod, HintString, &IsMenuItemAdded )
Input: (long) DocumentType Document type to which to add the menu item
Input: (long) Cookie Identifier of the menu as defined in swMenuIdentifiers_e; this is the same Cookie that you specified in SwAddin::ConnectToSW
Input: (BSTR) MenuItem Menu string ("menuItem@subMenuString@menuString"); SolidWorks creates menus and submenus only if they do not already exist
Note the description for the 'cookie' parameter. First it says that it should be a value from the swMenuIdentifiers_e enumeration, and then goes on to say that it should be the same cookie *I* specified in ConnectToSW. The problem is, *I* don't specify a cookie in ConnectToSW. The cookie is handed to me by SW. In any event, the point is moot, because if I use the cookie, the addin crashes. I suspect that this a another in a long list of cut and paste errors in the API docs.
If I call the above like this:
hr = sldWorks->AddMenuItem2 ( swDocNONE, swToolsMenu, menuItemInfo, 0, menuFunction, empty, empty, &added);
menuItemInfo = a BSTR containing "Item@Addin" menuFunction = a BSTR containing "helloWorldFromMenu " empty = a BSTR containing ""
I expect this to add a submenu named 'Addin' to the 'Tools' menu, with a single menu item named 'Item'.
What I get is a new menu named 'Addin', with no items under it. Upon checking the eroror status, the 'added' variable is FALSE, which means that the item was not added. Howevr the menu is, so clearly SW is not doing what it should. If the call to add an item fails, all of its parent should not be created.
It seems as though the 'cookie' parameter does nothing.
Why the call is returning FALSE for 'added' is a separate issue most likely related to IDispatch, but if someone has made sense of this horrible bit of API documentation, I would appreciate a snippet of code showing the 'proper' way to do this.