If you know the range of command ID, then you can use ON_COMMAND_RANGE.
For example, command id of the dynamic menu items are from 1000 to 2000, then you can handle all commands between these command ids by ON_COMMAND_RANGE( 1000, 2000, OnMenuHandler).
afx_msg void OnMenuHandler(UINT nMenuID_i);
nMenuID_i can be used to identify the id of menu item.