0

Hi I want to access the menu of a HWND and somehow I cant get it to work. itemCount is always -1 but I think I dont even have an hMenu..

my Code is

void bla(){
    #if _WIN32
        HWND hwnd;
        HMENU hMenu;
        MENUITEMINFO mmi;
        mmi.cbSize = sizeof(MENUITEMINFO);

        hwnd = GetForegroundWindow(); 
        hMenu = GetMenu(hwnd);
        int itemCount = GetMenuItemCount(hMenu);
        std::cout << "item count: " << itemCount << std::endl;
        GetMenuItemInfo(hMenu, 0, true, &mmi);
        DWORD dw = GetLastError();
    #endif
}

What am I doing wrong?

H.R.
  • 496
  • 4
  • 14
  • What did you discover when you tried stepping through the program line by line checking all the return values? – Raymond Chen May 24 '13 at 16:23
  • after GetForegroundWindow() `hwnd 0x00430ce0 {unused=??? }` after GetMenu(hwnd) `hMenu 0x00000000 {unused=??? }` so I know I dont have a hMenu, but why? – H.R. May 24 '13 at 17:10
  • Well that explains why `GetMenuItemCount` returns `-1`. The question then isn't "why is itemCount always -1" but rather "Why does `GetMenu` return `0x000000`?" And the answer is probably "Because the window doesn't have a menu." It sounds like you are trying to do UI automation. The recommended way of doing this is by using [the UI Automation feature](http://msdn.microsoft.com/en-us/library/windows/desktop/ee684076(v=vs.85).aspx). – Raymond Chen May 24 '13 at 17:45
  • The first time I start the app I dont have a menu, since the active window is cmd, but I call it every second and once I switch to VS2012 the program is able to read out the correct title of the current foreground window, so I would have thought that it should find a menu too. Thank you I will look into the UI Automation. – H.R. May 25 '13 at 01:58
  • VS does not use a menu. That thing that looks like a menu is a custom control. UI Automation will figure that out for you. – Raymond Chen May 25 '13 at 14:20
  • so I tried the example [How to Find UI Element by name](http://msdn.microsoft.com/en-us/library/windows/desktop/ff625913(v=vs.85).aspx) `IUIAutomationElement* GetTopLevelWindowByName(LPWSTR windowName)` but how do I get the proper windowName? I tryed `GetWindowText(hwnd, string, 128);` but string is TCHAR, do i need to convert it or is there another way to get the name as LPWSTR? } – H.R. Jun 03 '13 at 16:58
  • [TCHAR, LPWSTR, LPTSTR and GetWindow Text function](http://stackoverflow.com/questions/2231720/tchar-lpwstr-lptstr-and-getwindow-text-function/2231753#2231753). – Raymond Chen Jun 03 '13 at 21:16
  • Thank you I have had a look at the doc a few days before you send it to me but I still had problems with it, when I tried to provide the correct type for my dartapp. since i am currently working on another project I will come back to tell you how my progress on this issue was. – H.R. Jul 02 '13 at 22:44

1 Answers1

1

According to the documentation for GetMenuItemCount:

"If the function fails, the return value is -1. To get extended error information, call GetLastError."

What does GetLastError() tell you?

brooks
  • 141
  • 1
  • 2