Obtaining Device Capabilities Example

It's important to know the capabilities of the devices installed on your system. Of course, Windows provides a myriad of ways to find this information. For example, in Chapter 7 you learned the techniques for discovering the capabilities of the parallel and serial devices attached to a system. However, one function stands out from the rest as providing a little more in the way of generic information, the GetDeviceCaps() function. This function helps you obtain information about any device with a device context, which includes printers and even cameras.

Tip A number of Web sites now offer small Win32 API examples. One of the better places to find short examples on using the Windows Management Interface (WMI) is the VBnet Visual Basic Developers Resource Centre (http://www.mvps.org/vbnet/). This site also offers a number of other interesting examples. For instance, it includes a couple of short examples on performing security tasks and simple data routines (such as converting temperatures from one unit of measure to another). Most of the examples on this site are oriented toward a specific task, so you'll often find interesting nuggets of code buried in a task normally associated with another call.

The GetDeviceCaps() function requires two arguments. The first is a handle to a device context—the IntPtr that we've used in several other examples. The second is an index into the data for that device context. We'll use an enumeration for this example. The actual C header contains a set of #define entries, but an enumeration normally works better in the managed environment. Unlike other functions we've used, you can return only one value at a time when using the GetDeviceCaps() function. This function doesn't accept a structure that returns all of the required values because the values you can request vary by device type.

Note It pays to look through the enumerations for the GetDeviceCaps() function because not all of the functions appear in the Platform SDK documentation. In some cases, such as NUMMARKERS, the value is device specific. In other cases, the value is operating system version specific—several of the values only work with Windows 2000 and Windows XP. Make sure you understand the purpose of an undocumented value before you use it.

Now that you have a better idea of how the GetDeviceCaps() function works, let's look at some code. Listing 10.4 contains the working code for this example. The enumerated values are quite long, so I left them out of the listing in this case. Be sure to check the enumerated values in the source code found in the \Chapter 10\C#\DevCaps and \Chapter 10\VB\DevCaps folders of the CD.

Listing 10.4: Using the GetDeviceCaps() Function

// This function returns the device capability value specified // by the requested index value.

[DllImport("GDI3 2.DLL", CharSet=CharSet.Auto, SetLastError=true )] public static extern Int32 GetDeviceCaps(IntPtr hdc, Int32 nIndex);

private void btnTest_Click(object sender, System.EventArgs e) {

IntPtr hDC; // Device context for current window.

Int32 Result; // The result of the call.

StringBuilder Output; // The output for the method.

// Obtain a device context for the current application. hDC = GetDC(this.Handle);

// Check for errors.

// Display an error message.

MessageBox.Show("Couldn't obtain device context!", "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

return;

// Obtain the current display capability. Output = new StringBuilder();

Result = GetDeviceCaps(hDC, (Int3 2)DevCapParm.DESKTOPHORZRES); Output.Append("The horizontal resolution: " + Result.ToString()); Result = GetDeviceCaps(hDC, (Int3 2)DevCapParm.DESKTOPVERTRES); Output.Append("\r\nThe vertical resolution: " + Result.ToString()); Result = GetDeviceCaps(hDC, (Int3 2)DevCapParm.BITSPIXEL); Output.Append("\r\nThe bits/pixel value: " + Result.ToString());

// Display the results. MessageBox.Show(Output.ToString(),

"Current Display Capabilities",

MessageBoxButtons.OK,

MessageBoxIcon.Information);

// Release the device context when finished. ReleaseDC(this.Handle, hDC);

The application begins by obtaining the device context for the display. It's essential that you obtain the device context for whatever drawing device you want to learn about. This might mean creating a managed object of the right type and using it to obtain the correct device context. In a few cases, you'll need to use additional Win32 API calls to access the device because the .NET Framework doesn't provide the correct support.

After the code obtains the device context handle, it can begin calling GetDeviceCaps(). The output value is always returned as a number that you can convert to something the user will understand or a value your application can use for drawing or other tasks. The application obtains three common values that you can check using the Display Properties dialog box: horizontal resolution, vertical resolution, and the number of bits per pixel. Figure 10.9 shows the output from the application.

It's important to remember to release the device context before the application exits or it will have a memory leak. The last act of the application is to use the ReleaseDC() function discussed in other chapters to release the handle and associated resources obtained using the GetDC() function.

Was this article helpful?

0 0

Responses

  • fre-weini
    How getdevicecaps function works?
    8 years ago
  • bell
    What device data structure has DeviceCapabilities?
    4 years ago

Post a comment