简体   繁体   中英

Windows LoadMenu error: “The specified resource name cannot be found in the image file.”

I'm writing a program that, among other things, needs to display a context menu on right-click. I'm trapping WM_NOTIFY , the identifier of the control being clicked on, and NM_RCLICK . That all works great.

The problem comes in when I'm processing that right-click:

case NM_RCLICK:
{
    HMENU Popup = LoadMenu(0, MAKEINTRESOURCE(IDR_NED_MENU));
    if ( !Popup ) {
        DWORD err = GetLastError();
        char* buf;
        FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_ALLOCATE_BUFFER, 0, err, 0, buf, 1<<19, 0);
        _ERROR("LoadMenu(0, MAKEINTRESOURCE(IDR_NED_MENU)); Error '%s' thrown; no menu loaded.", buf);
        delete [] buf;
    }
    Popup = GetSubMenu(Popup, 0);
    CheckMenuItem(Popup, 1, MF_CHECKED|MF_BYPOSITION);

    POINT Point;
    GetCursorPos(&Point);

    switch (TrackPopupMenu(Popup, TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RETURNCMD, Point.x, Point.y, 0, GetActiveWindow(), NULL)) {
        // ...

Primarily, LoadMenu(0, MAKEINTRESOURCE(IDR_NED_MENU)); is returning NULL , and I'm getting an error message that states that "The specified resource name cannot be found in the image file."

Now, IDR_NED_MENU is the ID of a menu I have in the .rc file, and I've included the corresponding .rc.h file in this .cpp file. The actual dialog window IDs contained in the same .rc file work perfectly. This code is further copied and pasted from another project where the LoadMenu call worked perfectly: I did recreate the IDR_NED_MENU from scratch, though, and the IDs are somewhat different (but they do match between the .rc file and the .cpp file that has the code snippet I've pasted here); originally I'd accidentally created the menu in a separate .rc file so I sought to rectify that here. I noticed that in Visual Studio's Resource View, the dialogs are contained in the Dialog folder, while this is contained in the Menu folder (sensible), but I'm not sure what, if any, difference that makes.

Why would I be getting this error? Why can't it find IDR_NED_MENU ?

I'm using Visual Studio 2010, and this is not an MFC project. I'm not sure what, if any, other relevant details I should include; let me know in comments and I'll edit-update.

Thanks.

The first parameter to LoadMenu must be a handle to your executable image where the resource resides. The handle is the first HINSTANCE that you get in WinMain . Alternatively you can obtain it by a call to GetModuleHandle(0) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM