Search code examples
c++visual-studioresource-files

#ifdef gets lost in resource file


I have resource file which specifies a dialog. In this dialog I display the app name, version and if it is the 32bit or 64bit version of the program.

#ifdef WIN64
    LTEXT           "My App, Version 1.2.3.0 (64 bit)", IDC_APPLICATION_TITLE_TEXT, 42, 14, 251, 16, SS_NOPREFIX
#else
    LTEXT           "My App, Version 1.2.3.0 (32 bit)", IDC_APPLICATION_TITLE_TEXT, 42, 14, 251, 16, SS_NOPREFIX
#endif

This all works great, until I use Visual Studio to edit any of my dialogs, this triggers the resource file to be saved by VS and it strips out my #ifdef leaving only one of the entries (either 32bit or 64bit)

    LTEXT           "My App, Version 1.2.3.0 (64 bit)", IDC_APPLICATION_TITLE_TEXT, 42, 14, 251, 16, SS_NOPREFIX   

My question is, is there a way to prevent VS from striping out the #ifdefs when I edit dialogs in directly in VS, or is there a way to construct the text used in the resource in a way that can be used in the resource.


Solution

  • The only way I know if is to edit the files by hand and NOT use the Resource Editor. Any #define or #ifdef get processed and then removed by the Resource Editor itself, and the "post processed" rc is what gets saved :-\

    EDIT: You could stick the compiler directived resources in your .rc2 file, which is NOT processed by the Resource Editor. At least it's "localized" to entries in your RC2 file.

    EDIT2: Here's a sample of our VS2019 .RC file for an MFC DLL that utilizes an .RC2 file for non-appstudio sections. Yours should be similar.

    // Microsoft Visual C++ generated resource script.
    //
    #include "resource.h"
    
    #define APSTUDIO_READONLY_SYMBOLS
    /////////////////////////////////////////////////////////////////////////////
    //
    // Generated from the TEXTINCLUDE 2 resource.
    //
    #ifndef APSTUDIO_INVOKED
    #include "targetver.h"
    #endif
    #include "afxres.h"
    #include "verrsrc.h"
    
    /////////////////////////////////////////////////////////////////////////////
    #undef APSTUDIO_READONLY_SYMBOLS
    
    #ifdef APSTUDIO_INVOKED
    /////////////////////////////////////////////////////////////////////////////
    //
    // TEXTINCLUDE
    //
    
    1 TEXTINCLUDE
    BEGIN
        "resource.h\0"
    END
    
    2 TEXTINCLUDE
    BEGIN
        "#ifndef APSTUDIO_INVOKED\r\n"
        "#include ""targetver.h""\r\n"
        "#endif\r\n"
        "#include ""afxres.h""\r\n"
        "#include ""verrsrc.h""\r\n"
        "\0"
    END
    
    3 TEXTINCLUDE
    BEGIN
        "#define _AFX_NO_SPLITTER_RESOURCES\r\n"
        "#define _AFX_NO_OLE_RESOURCES\r\n"
        "#define _AFX_NO_TRACKER_RESOURCES\r\n"
        "#define _AFX_NO_PROPERTY_RESOURCES\r\n"
        "\r\n"
        "#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)\r\n"
        "LANGUAGE 9, 1\r\n"
        "#include ""res\\Utils.rc2""  // non-Microsoft Visual C++ edited resources\r\n"
    #ifndef _AFXDLL
        "#include ""afxres.rc""      // Standard components\r\n"
    #endif
        "#endif\r\n"
        "\0"
    END
    
    /////////////////////////////////////////////////////////////////////////////
    #endif    // APSTUDIO_INVOKED
    
    
    #ifndef APSTUDIO_INVOKED
    
    /////////////////////////////////////////////////////////////////////////////
    //
    // Generated from the TEXTINCLUDE 3 resource.
    //
    #define _AFX_NO_SPLITTER_RESOURCES
    #define _AFX_NO_OLE_RESOURCES
    #define _AFX_NO_TRACKER_RESOURCES
    #define _AFX_NO_PROPERTY_RESOURCES
    
    #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
    LANGUAGE 9, 1
    #include "res\\Utils.rc2"  // non-Microsoft Visual C++ edited resources
    #ifndef _AFXDLL
    #include "afxres.rc"      // Standard components
    #endif
    #endif
    
    /////////////////////////////////////////////////////////////////////////////
    #endif    // not APSTUDIO_INVOKED
    

    Note the two different sections of code for including the .RC2 file. One is for the RC Compiler grammar and one is for the Resource Editor itself (yes, strange).