views:

368

answers:

3

Hi

I'm working on an MFC application, that got pretty messy over years and over different teams of developers. The resource.h file, which contains all command/message mappings grew pretty big over time, and has lots of problems (like duplicate IDs). I am not proficient with MFC, so the question might sound pretty stupid...

MSDN docs mention that Command IDs and Message IDs should not be less than WM_USER and WM_APP correspondingly. I saw that most of the command IDs in resource.h generated by Visual Studio begin around 100. Shouldn't this cause some interfering with MFC/Windows commands and messages, that overlap with the application defined IDs? For example, I have a command ID :

#define ID_MY_ID 101

and there is a windows command that has the same ID. When MC send this command to the APP, it's handled like an application defined ID_MY_ID, and the app is taking unnecessary actions. Is it a possible scenario?

Also, is there some third party tool that helps to profile the project resources?

Update 1:

New question showed up: What is the preferred way of adding new custom commands to the application classes? As I understood, before they were added in the following way: add a command ID to the resouce.h, and then add a message map handler to the handling class.

A: 

command messages are sent in WM_COMMAND with command id in parameter so it won't conflict other messages.

Sheng Jiang 蒋晟
+1  A: 

Generally, there is no need to insert or edit the identifiers in resources manually (identifiers assingned by VS automatically in a correct manner). There are some cases that require manual interference in identifiers, but you can start with assumption, that work of previous teams of developers with resources was right. So if you did not encountered a problem because of resources, keep them untouched (IMHO).

"MSDN docs mention that Command IDs and Message IDs should not be less than WM_USER and WM_APP correspondingly." - It seems you something mixed up.

VitalyVal
The problem is that this assumption is not correct. The resource.h was edited manually many times and for some reason the Message IDs are there too! That's why I started asking about them along with the command IDs. I have no idea why they were put there... maybe in order to have all command IDs in a single place.
ak
Command IDs and message IDs are different things. As for command IDs the resource.h is right place to store (as were noted). As for private messages - extract them to a separate place, remove unused, verify IDs range on correctness, move them to appropriate places (most often to sorce .cpp and .h files with window class code for windows that process that messages).
VitalyVal
+1  A: 

You are mixing two things:

  1. Message IDs. These must be larger than WM_USER. Message IDs are not defined in resource.h. It seems from your description that you are not using application private messages.
  2. Command IDs. Your application itself must not have duplicate command IDs. The command ID values should also not interfere with the standard MFC IDs defined in afxres.h. Theses command IDs start at 0xE100, so it is unlikely that the values in resource.h. The resource compiler will generate an error for duplicate IDs in you rc file

There is probably no need for you to edit resource.h manually.

I would recommend to use the "Resource symbols" tool (right click on the resources in resource view and choose from the popup menu, I assume you are using VC++), to remove all the unused IDs from resource.h.

Dani van der Meer
part of the problem is that the IDs were added in resource.h manually, and the command handlers were added to the message maps manually as well. So the resource compiler wont probably complain about such duplicates. Also the "resource symbols" tool marks such manually added commands as unused, although they are actually used.
ak
The "resource symbols" tool only marks commands as unused if they are not found in the resources file (.rc). It does not monitor if they are used in code.
djeidot