Examining the Injected Attribute Code

   

Although using attributes has many advantages, there is a major disadvantage. Because attributes inject source code into your application during the build process, there may be a time when you need to examine the injected code to see what is going on and possibly even debug into it. Fortunately, Visual Studio .NET gives you two ways to do this.

The first and easiest way to inspect the final source code is to enable the /Fx compiler switch. Using the Solution Explorer, right-click the ATLAttributes project and select Properties. Under the Configuration Properties item on the left, select C/C++, Output Files. Change the Expand Attributed Source setting on the right side from No to Yes /Fx and close the dialog by clicking OK.

Rebuild the solution again and navigate to the directory containing the ATLAttributes project files. Within this directory, you should now see several files with a filename.mrg.cpp or a filename.mrg.h file extension. These are the merged files containing the expanded attribute code and your original source code.

However, this still doesn't solve the problem related to debugging. While you are debugging and hit a breakpoint within your code, you still see the regular attributes without any source code substitution. However, you can right-click an attribute in the source window and select View Disassembly from the source window.

Open the AttributeTest.cpp file in the ATLAttributes project. Place a breakpoint on the CAttributeTest constructor. In order to debug a multiple-project solution, make sure the project that contains the executable file is set to be the startup project, which in your case should be the MFCClient. Right-click the MFCClient project and select Set as StartUp Project. Now click Debug, Start on the main menu. Your object hasn't been created yet and therefore the constructor has not been called (not until you click the Hello button).

Once you do that, the debugger will now break at the constructor of your ATL object. As you can see, the C++ source code is surrounded by its assembly language disassembly code. To view the injected code, you have to scroll the source window down until you see a comment that says this:

 //+++ Start Injected Code For Attribute 'coclass' 

Your view should look similar to Figure 15.7. Although debugging code like this may seem like a treasure hunt, it may prove valuable in the long run.

Figure 15.7. Viewing injected attribute source code during debugging.

graphics/15fig07.jpg


   
Top


Sams Teach Yourself Visual C++. NET in 24 Hours
Sams Teach Yourself Visual C++.NET in 24 Hours
ISBN: 0672323230
EAN: 2147483647
Year: 2002
Pages: 237

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net