Set Up My Visual C++ Project
For simplicity and clarity, the C/C++ applications presented in this tutorial are constructed as Win32 console application projects in Microsoft Visual Studio 2008.
The procedure below outlines the steps to build and run a simple program.
To create a new Win32 console application project
Start Visual Studio 2008.
From the File menu, select New, and then select Project. The New Project dialog box will open. For Project types, select Visual C++ / Win32. For Templates, select Win32 Console Application. Then type the name of the project (in this case, "trivialProj") in the Name field. For the project Location field, either accept the default setting or choose another location (e.g., c:\examples\HowDoI). Click OK.
The Win32 Application Wizard will start. You do not need to change any options. Click Finish.
Set up the include directory for Visual Studio. From the Tools menu, select Options. In the Options dialog box, expand Projects and Solutions. Select VC++ Directories. Select Include files in the Show directories for drop down box. Click the New Line button, and add a new line to the list of directories. Browse to the directory that contains the MSXML header file. For MSXML 6.0, by default, the directory is C:\Program Files\MSXML 6.0\inc. Click OK.
Set up the lib directory for Visual Studio. From the Tools menu, select Options. In the Options dialog box, expand Projects and Solutions. Select VC++ Directories. Select Library files in the Show directories for drop down box. Click the New Line button, and add a new line to the list of directories. Browse to the directory that contains the MSXML libraries. For MSXML 6.0, by default, the directory is C:\Program Files\MSXML 6.0\lib. Click OK.
Modify the project to link with the MSXML 6.0 library. In the Solution Explorer, right click on the project and select Properties. In the tree, expand Configuration Properties, and then expand Linker. Under Linker, click on Command Line. Enter msxml6.lib in the Additional options text box. Click OK.
Now you are ready to add source and header files to the project.
For simplicity's sake, each task in the How Do I Program with DOM in C++ tutorial is implemented as a standalone Win32 console application.
Source Listing for a Trivial Application (trivial.cpp)
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <objbase.h>
#include <msxml6.h>
int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
printf("start using MSXML6\n");
// Add your code here.
CoUninitialize();
return 0;
}
To edit the source code for your project
Open the Solution Explorer. Expand the Source Files tab. Right click on trivalProj.cpp and select View Code.
Copy the source code snippet above and paste it into trivalProj.cpp.
Note
Before using any COM module, you must initialize the COM library by calling the
CoInitialize(NULL)
function in your application. You then must close the COM library by calling theCoUninitialize()
function before the application exits.
To build and run the application
To test the application while in the development environment, select Build Solution from the Build menu.
When the application is built without errors, run the application. From the Debug menu, select Start Without Debugging.
When you build and run trivial.cpp, you should get the following output:
start using MSXML6
You are now ready to start working with XML DOM.