CXO

Two Visual Basic tips to make your life easier

These techniques from the Builder.com Visual Basic e-newsletter will help you access Visual SourceSafe programmatically in VB6 and write a CPU monitoring counter in VB.NET.


This article originally appeared in Builder.com's Visual Basic e-newsletter.

If you use Visual SourceSafe for version control, you know it's a handy tool, but it has a few shortcomings. In this article, we'll show you how to tap into the SourceSafe functionality you need from your own programs and automate some tedious tasks. Then, we'll change gears and walk you through a part of the .NET Framework that enables you to access performance CPU monitoring counters in Windows NT/2000 or XP.

Automate SourceSafe from Visual Basic
Many developers use Visual SourceSafe to store the source code from their projects. Although it's a useful tool, some tasks are difficult to accomplish from the user interface. For instance, you can check out files in the same project easily, but what if you want to check out every file in a project, including files in subfolders, that were changed on a specific date?

The developers of SourceSafe provided a solution for this type of problem: the SourceSafe automation components. These objects provide a way to manipulate SourceSafe from your own programs. Simply add a reference to the Microsoft SourceSafe 6.0 Type Library to your Visual Basic program.

The VSSDatabase object provides a reference to a SourceSafe database. Call the Open method and pass the complete filename to the Srcsafe.ini file for the database you want to work with:
vssDatabase.Open(strSourceSafePath)

After opening the database, you can work with the items within it. Files and projects are both represented by the VSSItem object. The Type property lets you identify whether a specific item is a project or a file. To get a VSSItem, call the VSSItem method on the VSSDatabase object, passing the Spec, which is the project path to the item. The Spec looks like $/ProjectName/Filename.

You can iterate all child items for a specific VSSItem by iterating the Items collection. Although our example in Listing A uses VB.NET, it works the same way in VB6.

By using the SourceSafe automation objects, you can create programs to replace the time-consuming manual efforts of working with SourceSafe.

Monitor performance counters
The .NET Framework System.Diagnostics namespace includes the PerformanceCounter class, which can be used to access performance counters in Windows NT, 2000, and XP. This can be helpful if your application needs to monitor CPU or memory usage.

Performance counters are identified by four attributes:
  • ·        The machine name determines which computer should be monitored.
  • ·        The category name is the category the counter is under, such as Processor, Paging File, and PhysicalDisk.
  • ·        The counter name indicates the specific counter, such as % Processor Time.
  • ·        The instance name determines the instance of the counter. For example, a four-processor server has four instances of the % Processor Time counter, plus a total counter.

You can browse the available performance counters by going to Control Panel | Administrative Tools | Performance and clicking Add.

The code below initializes a performance counter to watch the % Processor Time counter for the local machine.
Dim perfCounter As New System.Diagnostics.PerformanceCounter()
Dim loopCount As Integer

perfCounter.CategoryName = "Processor"
perfCounter.CounterName = "% Processor Time"
perfCounter.InstanceName = "_Total"

For loopCount = 1 To 100
    Debug.WriteLine(perfCounter.NextValue.ToString())
Next

perfCounter.Close()


Be sure you call the Close method on the PerformanceCounter when you're finished, since it uses system resources that need to be freed.

Editor's Picks

Free Newsletters, In your Inbox