2

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.

user20716902
  • 861
  • 1
  • 14

1 Answers1

1

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).

franji1
  • 3,088
  • 2
  • 23
  • 43
  • Thanks, I've always been manually editing the files by hand, but sometimes I've needed to edit complex dialogs in the Resource Editor, and If I forget to check those lines they get lost. I'll take a look at RC2 that sound interesting. – user20716902 Jan 26 '23 at 20:25
  • My "Microsoft Generated Resource Script" .RC file has provisions already for the .RC2 file (it's convoluted since the .RC file is dealing with both the Resource Editor grammar AND the Resource Compiler grammar). There's are `#ifdef APSTUDIO_INVOKED` sections and a `#ifndef APSTUDIO_INVOKED` sections in . Let the Resource Editor tweak the .RC content - you tweak the .RC2 content which is exclusively for NON-APSTUDIO_INVOKED resources. – franji1 Jan 26 '23 at 21:00
  • I've benn looking at rc2, but it gets included at the end of the main rc file. So anything I define in there isn't picked up in the rc. – user20716902 Jan 26 '23 at 21:02
  • We usually use the .RC2 file for the VERSIONINFO section since we definitely use #defines for most of those values. If you use #defines for VERSIONINFO values but keep it in the .RC file - they will be replace with the simple numeric values upon "saving" the .RC. That's what the .RC2 file is for. – franji1 Jan 26 '23 at 21:03
  • It won't show up in the .RC, but in the OUTPUT of the Resource - see my VERSIONINFO example (look at the contents in your DLL or EXE or RES or ???) – franji1 Jan 26 '23 at 21:05
  • Thank you so much, that's pointed me in the right direction, what I've now done is add `#include "verrsrc.h"` to the Resource Includes Read-only symbol directives. I'm now able to contruct the app title including if it's 32 or 64bit) and have that used in the resource dialog. Editing a dialog in VS no longer loses my values in the dialog. – user20716902 Jan 26 '23 at 21:25