Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Preprocessor directives - resources

Preprocessor directives - resources

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialc++windows-adminquestionlearning
12 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    You can add preprocessor directives (#define, #if, #endif) to your resource files as long as you do it outside of any blocks that are managed by the IDE. If you open the project's .rc file you should be able to figure out where to do it. In fact, if memory serves, there is a second user editable resource (projectname.rc2 I think) in MFC projects.

    d@nish[^] has the answer.

    B Offline
    B Offline
    bob16972
    wrote on last edited by
    #3

    Looks like that will work for the strings and maybe a few other things as I'm not getting any errors. I was hesitant because I've had nothing but trouble over the years when messing with the .rc files. Usually I was trying to copy dialogs from one project to another and tended to get corrupt looking results when I did that. I needed some peer support to know that technique is common enough to carry on. Thanks for taking the time to share your thoughts.

    1 Reply Last reply
    0
    • L Lost User

      You can add preprocessor directives (#define, #if, #endif) to your resource files as long as you do it outside of any blocks that are managed by the IDE. If you open the project's .rc file you should be able to figure out where to do it. In fact, if memory serves, there is a second user editable resource (projectname.rc2 I think) in MFC projects.

      d@nish[^] has the answer.

      B Offline
      B Offline
      bob16972
      wrote on last edited by
      #4

      Ah crud. I spoke too soon. (I had only compiled it and did not try to run it when I replied earlier) I removed those two string resources, IDR_MAINFRAME and AFX_IDS_APP_TITLE, from the .rc file and put them in the .rc2 file. If I simply place them in there, in a STRINGTABLE, the application works correctly (has the correct title and such). However, once I try to put a preprocessor directive in there, it compiles but the values will be incorrect sometimes depending on which value is in the if branch and the else branch. After some experimentation, it appears that the last string assigment wins. I'm guessing that means it is ignoring the preprocessor directives or my symbol is not defined yet. I defined the symbol way up toward the top in my stdafx.h file but I'm guessing this isn't early enough? You don't happen to remember where you defined your symbol when you had the need to do something like this? I apologize for the extra question.

      L C 2 Replies Last reply
      0
      • B bob16972

        Ah crud. I spoke too soon. (I had only compiled it and did not try to run it when I replied earlier) I removed those two string resources, IDR_MAINFRAME and AFX_IDS_APP_TITLE, from the .rc file and put them in the .rc2 file. If I simply place them in there, in a STRINGTABLE, the application works correctly (has the correct title and such). However, once I try to put a preprocessor directive in there, it compiles but the values will be incorrect sometimes depending on which value is in the if branch and the else branch. After some experimentation, it appears that the last string assigment wins. I'm guessing that means it is ignoring the preprocessor directives or my symbol is not defined yet. I defined the symbol way up toward the top in my stdafx.h file but I'm guessing this isn't early enough? You don't happen to remember where you defined your symbol when you had the need to do something like this? I apologize for the extra question.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #5

        bob16972 wrote:

        I removed those two string resources, IDR_MAINFRAME and AFX_IDS_APP_TITLE, from the .rc file and put them in the .rc2 file.

        Probably not a good idea as these are auto generated and maintained; especially be careful with anything with the prefix AFX.

        bob16972 wrote:

        I defined the symbol way up toward the top in my stdafx.h file

        I don't think stdafx.h gets included into the .rc fie. You should use some other header, or put the values direct into the resource script.

        d@nish[^] has the answer.

        B 1 Reply Last reply
        0
        • B bob16972

          Ah crud. I spoke too soon. (I had only compiled it and did not try to run it when I replied earlier) I removed those two string resources, IDR_MAINFRAME and AFX_IDS_APP_TITLE, from the .rc file and put them in the .rc2 file. If I simply place them in there, in a STRINGTABLE, the application works correctly (has the correct title and such). However, once I try to put a preprocessor directive in there, it compiles but the values will be incorrect sometimes depending on which value is in the if branch and the else branch. After some experimentation, it appears that the last string assigment wins. I'm guessing that means it is ignoring the preprocessor directives or my symbol is not defined yet. I defined the symbol way up toward the top in my stdafx.h file but I'm guessing this isn't early enough? You don't happen to remember where you defined your symbol when you had the need to do something like this? I apologize for the extra question.

          C Offline
          C Offline
          Chris Meech
          wrote on last edited by
          #6

          You can also have the symbol defined as part of the project's settings as part of the command line. Since it sounds like you have different solutions, building different targets, but using a lot of common source files, this might provide more convenient. :)

          Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

          B 1 Reply Last reply
          0
          • L Lost User

            bob16972 wrote:

            I removed those two string resources, IDR_MAINFRAME and AFX_IDS_APP_TITLE, from the .rc file and put them in the .rc2 file.

            Probably not a good idea as these are auto generated and maintained; especially be careful with anything with the prefix AFX.

            bob16972 wrote:

            I defined the symbol way up toward the top in my stdafx.h file

            I don't think stdafx.h gets included into the .rc fie. You should use some other header, or put the values direct into the resource script.

            d@nish[^] has the answer.

            B Offline
            B Offline
            bob16972
            wrote on last edited by
            #7

            I hear you on the caution. I'm not feeling too great about messing with these but the boss want miracles and I'm running out of tricks so I guess I'm desperate to try this as I'm sure to screw up manually changing stuff back and forth for every release. changing a single define is so much easier in the long run. I went ahead and put all my custom preprocessor symbol definitions in a stdafx2.h file and reference it from the top of the stdafx.h and the .rc2 file. This appears to have done the trick. I kicked it a few times and my Resource symbols (including the "readonly" Afx... ones are showing being used by string tables so I'm guessing "she might hold together captain". I'll probably regret this later but I'm game to try it out and see. :~ Thanks again for all the advice.

            L 1 Reply Last reply
            0
            • C Chris Meech

              You can also have the symbol defined as part of the project's settings as part of the command line. Since it sounds like you have different solutions, building different targets, but using a lot of common source files, this might provide more convenient. :)

              Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

              B Offline
              B Offline
              bob16972
              wrote on last edited by
              #8

              Thanks for the ideas. This was already a single solution with a single project, that compiled into two different apps based on preprocessor symbols. Now it needs to have two identities for each so this might get interesting. Boy what a mess. :~ (I posted the approach I'm going to try in a different thread for this post) Thanks again for taking some time out to help!

              1 Reply Last reply
              0
              • B bob16972

                I have a typical MFC MDI application that was built with the wizards. I have been using the resources as they are provided but I am now faced with the need to be able to compile/maintain the same code base with multiple application identities (this includes company name, application name, application title, file extensions, icons, verbs, etc...). Some of this will be handled by maintaining multiple installer solutions to handle file associations, registry stuff, verbs, etc...) but I have not figured out how to handle the stuff that exists in the resource files. For example, the string for IDR_MAINFRAME and AFX_IDS_APP_TITLE need to be sensitive to a preprocessor directive or something similar in nature. I'm guessing if I try messing with the .rc file directly, I will have trouble as I edit stuff via the GUI down the road. Is there a way to modify some of those resources based on a preprocessor directive(or something similar) without introducing maintenance isssues?

                C Offline
                C Offline
                cp9876
                wrote on last edited by
                #9

                Not sure what version of VS you are using, but in VS2005 you could: - extract the particular strings to a separate string table - in VS, highlight the string table in Resource View, right click and show properties - in the Condition field you can specify some preprocessor define - e.g BUILD1, this should now be added to the name of the string table - right click on the string table and Insert Copy, and add your new condition on the copy - e.g. BUILD2 - create multiple builds of the project, and set the preprocessor defines in Project Properties / Resources / General

                Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

                B 1 Reply Last reply
                0
                • B bob16972

                  I hear you on the caution. I'm not feeling too great about messing with these but the boss want miracles and I'm running out of tricks so I guess I'm desperate to try this as I'm sure to screw up manually changing stuff back and forth for every release. changing a single define is so much easier in the long run. I went ahead and put all my custom preprocessor symbol definitions in a stdafx2.h file and reference it from the top of the stdafx.h and the .rc2 file. This appears to have done the trick. I kicked it a few times and my Resource symbols (including the "readonly" Afx... ones are showing being used by string tables so I'm guessing "she might hold together captain". I'll probably regret this later but I'm game to try it out and see. :~ Thanks again for all the advice.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #10

                  bob16972 wrote:

                  Thanks again for all the advice.

                  Happy to help, good luck!

                  d@nish[^] has the answer.

                  1 Reply Last reply
                  0
                  • C cp9876

                    Not sure what version of VS you are using, but in VS2005 you could: - extract the particular strings to a separate string table - in VS, highlight the string table in Resource View, right click and show properties - in the Condition field you can specify some preprocessor define - e.g BUILD1, this should now be added to the name of the string table - right click on the string table and Insert Copy, and add your new condition on the copy - e.g. BUILD2 - create multiple builds of the project, and set the preprocessor defines in Project Properties / Resources / General

                    Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

                    B Offline
                    B Offline
                    bob16972
                    wrote on last edited by
                    #11

                    Thanks for the idea as it sounds a bit more elegant. I'm using VS 2003 and unfortunately if I try and create a second string table it reads... "There cannot be more than one instance of this type. The instance of this object already created will be opened." I've tried to add a string table at various nodes of the resource tree but I get the same error. I'll keep your idea in mind when(if) I migrate this project to VS 2008 (we skipped 2005). thanks for the tip.

                    C 1 Reply Last reply
                    0
                    • B bob16972

                      Thanks for the idea as it sounds a bit more elegant. I'm using VS 2003 and unfortunately if I try and create a second string table it reads... "There cannot be more than one instance of this type. The instance of this object already created will be opened." I've tried to add a string table at various nodes of the resource tree but I get the same error. I'll keep your idea in mind when(if) I migrate this project to VS 2008 (we skipped 2005). thanks for the tip.

                      C Offline
                      C Offline
                      cp9876
                      wrote on last edited by
                      #12

                      We've been using this since VC6 to have customised menus, dialogs, bitmaps etc in different builds. I just checked VC6 and it appears that in VC6 you can add a preprocessor condition to any resource item EXCEPT string tables, these seem to only depend on language. We skipped VS2003 so I can't comment on what you can do there, but if there is no Condition field in the Properties window on string tables then I guess it isn't supported yet. Just look at the properties windows for other resources (e.g. menus) and you should see the Condition field. You should be able to use the same syntax in the .rc file that VS uses for other resources dependent on preprocessor definitions, and use the same in string tables. For example, a menu that is only included when DEFINE1 is defined appears as:

                      #if defined(APSTUDIO_INVOKED) || defined(DEFINE1)
                      #if defined(APSTUDIO_INVOKED)
                      IDR_MENU1$(DEFINE1) MENU DISCARDABLE
                      #else
                      IDR_MENU1 MENU DISCARDABLE
                      #endif
                      BEGIN
                      POPUP "Menu1"
                      BEGIN
                      MENUITEM "Help", ID_HELP_X
                      ...
                      ...
                      END
                      END
                      #endif

                      I'm not sure about the APSTUDIO_INVOKED, it seems to use two different mechanisms for the conditional inclusion, one for the IDE and the other for a command line version. You should be able to create a couple of conditional menus or other resources and check the syntax of the .rc file. Don't forget that you specify these defines under the Resources settings in the Project settings.

                      Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups