Enumerating Everything in the System

The diagnostics namespace is built on the same code base that the DxDiag tool as ships in the operating system and versions of DirectX. If you've ever run this tool, it can tell you just about everything you ever wanted to know about your system.

The diagnostics data is arranged in a hierarchal fashion. You have containers, which can contain zero-many properties, as well as zero-many children containers. Enumerating all objects is simple to do with a recursive function. In Listing A.1, look at the function that is included with the DirectX SDK DxDiagOutput sample:

Listing A.1 Outputting Diagnostic Data
 static void OutputDiagData(string parent, Container root) {     try     {         foreach (PropertyData pd in root.Properties)         {             // Just display the data             Console.WriteLine("{0}.{1} = {2}", parent, pd.Name, pd.Data);         }     }     catch     {     }     try     {         foreach (ContainerData cd in root.Containers)         {             // Recurse all the internal nodes             if (parent == null)                 OutputDiagData(cd.Name, cd.Container);             else                 OutputDiagData(parent + "." + cd.Name, cd.Container);         }     }     catch     {     }     // We are done with this container, we can dispose it.     root.Dispose(); } 

If there are any properties in this container, we will enumerate each of them, and display them to the user, as well as the value they contain. The values will always be either a string, Boolean, or integer value, depending on the property. You can check the pd.Data.GetType() method to determine the type if it is important to you.

After we've dealt with the properties for this container, we need to check whether this container contains any children containers. If it does, we enumerate each of those, and for every one we find, we simply call this method once more recursively.

The only thing left to do for this to be useful is to create the first container and make the initial call into this method. The container object contains only one constructor that takes one Boolean value. This value is used to determine whether the diagnostics should include WHQL (Windows Hardware Quality Labs) information as well. Retrieving this information can be time consuming, so unless you need it, it's probably better to pass in false here. The DirectX SDK sample uses this entry into the executable (see Listing A.2):

Listing A.2 Starting the Application
 static void Main(string[] args) {     try     {         // Just start our recursive loop with our root container.  Don't worry         // about checking Whql         OutputDiagData(null, new Container(false));     }     catch     {         // Something bad happened     } } 

If you built this diagnostics functionality into your application and could detect when a failure had occurred, you could automatically send this data back from your customers' machine to you, allowing you to narrow down the causes of problems. You could package the data up into a small XML file, and use that as a parameter into a Web service on your Web site to collect the failures. Be sure to inform your users if you will be sending machine-specific information to your servers, and allow them to opt out of the action completely. If they do allow you to send the data to your servers, make sure your privacy and data retention policies are known to the user as well.



Managed DirectX 9 Graphics and Game Programming, Kick Start
Managed DirectX 9 Kick Start: Graphics and Game Programming
ISBN: B003D7JUW6
EAN: N/A
Year: 2002
Pages: 180
Authors: Tom Miller

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