c++builderresourcestring

String ID inside .RC file is not recognized in code


I'm using C++ Builder 11.2 and have a following strings.rc file added to my project:

#define ID_TESTSTRING    1

STRINGTABLE
BEGIN
    ID_TESTSTRING,   "Just for testing"
END

Inside my Form1 (Unit1.cpp) I use the following code:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    wchar_t resString[100];
    LoadString(HInstance, ID_TESTSTRING, resString, 100); // undeclared identifier ID_TESTSTRING!
    ShowMessage(resString);
}

The problem is that I get this 'undeclared identifier' error. If I replace ID_TESTSTRING with its numeric constant 1 then everything works as expected. Why the identifier is not recognized? Because, I want to work with ID's in my code, not with their numeric constants.


Solution

  • Identifiers are not "exported" automatically, or created as identifiers in all source files.

    You need to put the ID_TESTSTRING macro definition in a separate header file, that you include in both your resource and C++ source files.

    resources.rh

    #define ID_TESTSTRING    1
    

    resources.rc

    #include "resources.rh"
    
    STRINGTABLE
    BEGIN
        ID_TESTSTRING,   "Just for testing"
    END
    

    Unit1.cpp

    #include "resources.rh"
    
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
        wchar_t resString[100];
        LoadString(HInstance, ID_TESTSTRING, resString, 100);
        ShowMessage(resString);
    }