Sam
Sam

Reputation: 251

Resource compiler unable to compile enum statement in resource scripts for Win32?

I have been working with resource scripts for Win32. After a while, I made a menu and implemented the menu item IDs as enum values in a .h header file. After including the header file into the resource script and trying to compile the project, I noticed that enums didn't work with my resource compiler, and was forced to #define all of the menu IDs.

The errors I recieved:

[Error] Invalid argument

[Resource error] Syntax error (code line in resource script where first enum value occours)

[Resource error] Preprocessing failed

I use Dev-C++ with windres.exe as resource compiler.

I would like to know whether this is normal for an unmodified IDE. Is there a general solution to this like adding a specific preprocessor to my IDE's bin library, or am I left with coding my own little preprocessor? Generally, how do the professionals do it without keeping track of the resource IDs manually and trying to avoid "ID collisions"? Also, would this problem be off the table if the C++ compiler somehow borrowed the #define macros to any other involved compile process?

Following is an equivalent of what I have tried:

Resource script: (resources.rc)

#include <windows.h>
#include <resource.h>

IDR_MYMENU MENU
{
    POPUP "Über..."
    {
        MENUITEM "Dieses Programm", MENU_UEBER_DIESES_PROGRAMM, GRAYED
    }
    POPUP "Einstellungen"
    {
        MENUITEM "Konfiguration", MENU_EINST_KONFIG
        MENUITEM "Optionen", MENU_EINST_OPTIONEN, GRAYED
    }
    POPUP "Daten"
    {
        MENUITEM "Hersteller", MENU_DATEN_HRST
        MENUITEM "Verfahren", MENU_DATEN_VERF, GRAYED
        POPUP "Werkzeuge"
        {
            MENUITEM "Oberwerkzeuge", MENU_DATEN_OW
            MENUITEM "Unterwerkzeuge", MENU_DATEN_UW, GRAYED
        }
        MENUITEM "Werkstoffe", MENU_DATEN_WS, GRAYED
        MENUITEM "Messwerte", MENU_DATEN_MW, GRAYED
    }
    POPUP "Diagnose"
    {
        MENUITEM "Logdatei", MENU_DIAGNOSE_LOGDATEI
        MENUITEM "Letzter Fehlerbericht", MENU_DIAGNOSE_FEHLERBERICHT, GRAYED
    }
    POPUP "Entwickler"
    {
        MENUITEM "XML Import", MENU_ENTW_XML_IMP, GRAYED
        MENUITEM "Konsole anzeigen", MENU_ENTW_KONSOLE, GRAYED
    }
    POPUP "?"
    {
        MENUITEM "Entwickler kontaktieren", MENU_HILFE_ENTW_KONTAKT, GRAYED
        MENUITEM "Readme", MENU_HILFE_README, GRAYED
    }
}

This is the header file as simple as I would have expected it to work (resource.h):

#ifndef RESOURCE_H_
#define RESOURCE_H_

// Menu
enum {
IDR_MYMENU = 200,
MENU_UEBER_DIESES_PROGRAMM,

MENU_EINST_KONFIG,
MENU_EINST_OPTIONEN,

MENU_DATEN_HRST,
MENU_DATEN_VERF,
MENU_DATEN_OW,
MENU_DATEN_UW,
MENU_DATEN_WS,
MENU_DATEN_MW,

MENU_DIAGNOSE_LOGDATEI,
MENU_DIAGNOSE_FEHLERBERICHT,

MENU_ENTW_XML_IMP,
MENU_ENTW_KONSOLE,

MENU_HILFE_ENTW_KONTAKT,
MENU_HILFE_README
}

#endif // RESOURCE_H_

This is the unhandy header file, as I have been fixing it temporarily: (resource.h)

#ifndef RESOURCE_H_
#define RESOURCE_H_

// Menu
#define IDR_MYMENU 200

// START Menu items
#define MENU_UEBER_DIESES_PROGRAMM 11

#define MENU_EINST_KONFIG 21
#define MENU_EINST_OPTIONEN 22

#define MENU_DATEN_HRST 31
#define MENU_DATEN_VERF 32
#define MENU_DATEN_OW 33
#define MENU_DATEN_UW 34
#define MENU_DATEN_WS 35
#define MENU_DATEN_MW 36

#define MENU_DIAGNOSE_LOGDATEI 41
#define MENU_DIAGNOSE_FEHLERBERICHT 42

#define MENU_ENTW_XML_IMP 51
#define MENU_ENTW_KONSOLE 52

#define MENU_HILFE_ENTW_KONTAKT 61
#define MENU_HILFE_README 62
// END Menu items

#endif // RESOURCE_H_

Upvotes: 3

Views: 709

Answers (1)

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145439

The resource compiler doesn't know anything about C or C++. The script is just passed through the C preprocessor (with RC_INVOKED I think it was, defined) before the resource compiler uses the result. The resource script language, e.g. for string literals, is very different from C.

So, to define a constant that can be used both in the resource script and by your C or C++ compiler, use a #define.

For the C or C++ side (only) you can define an enum with values specified by the macro symbols, but I don't think there's much advantage in that. At least I cannot think of any.

Upvotes: 4

Related Questions