Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Apply the New Vista APIs to Sidebar Gadgets, Part 1  : Page 3

Explore the various Windows Vista Sidebar gadget objects and how they can be used to enhance the functionality of your gadget.


advertisement
Debugging and Diagnostics
Microsoft provides the following objects to help developers better debug their Sidebar gadgets:
  • System.Debug
  • System.Diagnostics.EventLog
System.Debug
The System.Debug object provides a means to display debugging information from within your gadget. It has only one method—outputString()—that writes a message to the output window of a debugger.

The following code writes the current time to the debugger window:

("Current Date and Time is " + Date());

The user won’t be able to see the debugging string; you have to use a script debugger to see the message. You can use Visual Studio 2005 for this purpose. To attach a debugger to your Sidebar, first invoke the Windows Task Manager by pressing Ctrl+Alt+Delete. Select the Processes tab, and then right-click the sidebar.exe process and select Debug.



A dialog appears, warning you about the potential loss of data. Click Yes to proceed. The Visual Studio Just-In-Time Debugger dialog appears, which enables you to select a debugger for debugging. If you have Visual Studio 2005 installed, it appears in the list of possible debuggers. Select it and click Yes.

The output generated by the outputString() method will appear in the Output window of Visual Studio 2005 (see Figure 1).

The System.Diagnostics.EventLog Object
The System.Diagnostics.EventLog object enables you to write entries in the Event Viewer event log. It has only one method—writeEntry(), which writes an entry into Event Viewer. It has the following signature:

System.Diagnostics.EventLog.writeEntry(strEventLogEntry [, intEventType])

Figure 1. The Output Window: This output is generated by the outputString() method.

The strEventLogEntry parameter takes in a string containing the information to write to the event log. The intEventType parameter specifies the type of event being logged. It can be one of the following values:

  • 0: Success
  • 1: Error
  • 2: Warning
  • 3: Information (default)
The following code example writes four entries in the event log:

System.Diagnostics.EventLog.writeEntry("Success message", 0); System.Diagnostics.EventLog.writeEntry("Error message", 1); System.Diagnostics.EventLog.writeEntry("Warning message", 2); System.Diagnostics.EventLog.writeEntry("Information message", 3);

To view the entries written to the event log, select Start—>Settings—>Control Panel—>Administrative Tools—>Event Viewer. Choose the Application log to see the newly written entries.

System Information
You can also use Sidebar gadgets to access basic system information—system root, user name, Windows directory, and so on—using the System.Environment object. This object enables you to obtain values of environment variables. It contains one property, machineName, which returns the name of the machine on the current workgroup or network.

The following code shows how to display the name of the machine hosting the Sidebar gadget:

var result = document.getElementById("result"); result.innerHTML = System.Environment.machineName + "<br/>";

The system.Environment object has only one method—getEnvironmentVariable(), which retrieves the value for an environment variable. Some examples of environment variables are: HOMEPATH, PATH, USERNAME, WINDIR, etc.

You use the getEnvironmentVariable() method to retrieve the value of environment variables, as the following code sample shows:

var result = document.getElementById("result"); result.innerHTML = System.Environment.machineName + "<br/>"; result.innerHTML += System.Environment.getEnvironmentVariable("WINDIR") + "<br/>"; result.innerHTML += System.Environment.getEnvironmentVariable("USERPROFILE");



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap