Home
Products
Forums
Help
Publish Article

SetupApi.dll usage in .Net application

Downloads

If you are seeing this section and do not see download links, this means that you are not logged into our site. If you already are a member, click on the login link and login into site and come back to this page for downloading the control files. If you are not a member, click on registration link to become a Winista member and download the control for free.

SetupApi.dll presents some APIs that help you enumerate all devices installed on your machine or some remote machine. And if you are writing some device driver and had to deal with installation of the driver then you will be using this API. There are no managed classes that expose this API. So only choice you have is using PInvoke to call the methods contained in setupapi.dll. This article will present you some code that will show you can use APIs in setupapi.dll in your .Net applications.

Following are some of the prototypes defined in the demo project included with this article.

[DllImport("setupapi.dll")]
internal static extern IntPtr SetupDiGetClassDevsEx(IntPtr ClassGuid, 
    [MarshalAs(UnmanagedType.LPStr)]String enumerator, 
    IntPtr hwndParent, Int32 Flags, IntPtr DeviceInfoSet, 
    [MarshalAs(UnmanagedType.LPStr)]String MachineName, IntPtr Reserved);

[DllImport("setupapi.dll")]
internal static extern Int32 SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);

[DllImport("setupapi.dll")]
internal static extern Int32 SetupDiEnumDeviceInterfaces(IntPtr DeviceInfoSet, 
    IntPtr DeviceInfoData, IntPtr InterfaceClassGuid, 
    Int32 MemberIndex, ref  SP_DEVINFO_DATA DeviceInterfaceData);

[DllImport("setupapi.dll")]
internal static extern Int32 SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, 
    Int32 MemberIndex, ref SP_DEVINFO_DATA DeviceInterfaceData);

[DllImport("setupapi.dll")]
internal static extern Int32 SetupDiClassNameFromGuid(ref Guid ClassGuid, 
    StringBuilder className, Int32 ClassNameSize, ref Int32 RequiredSize);

[DllImport("setupapi.dll")]
internal static extern Int32 SetupDiGetClassDescription(ref Guid ClassGuid, 
    StringBuilder classDescription, Int32 ClassDescriptionSize, ref Int32 RequiredSize);

[DllImport("setupapi.dll")]
internal static extern Int32 SetupDiGetDeviceInstanceId(IntPtr DeviceInfoSet, 
    ref SP_DEVINFO_DATA DeviceInfoData, 
    StringBuilder DeviceInstanceId, Int32 DeviceInstanceIdSize, ref Int32 RequiredSize);
							

Here is code snippet from the demo project showing how the functions from API are called to enumerate all device classes and then extract some more information about the classes and interfaces.

pNewDevInfoSet = Win32DeviceMgmt.SetupDiGetClassDevsEx(
    classGuid, strEnumerator, hwndParent,
    flags, pDevInfoSet, strMachineName, IntPtr.Zero);
if (pNewDevInfoSet == IntPtr.Zero)
{
	Console.WriteLine("Failed to get device information list");
	return;
}

Int32 iRet;
Int32 iMemberIndex = 0;
do
{
	Win32DeviceMgmt.SP_DEVINFO_DATA devInfoData = new Win32DeviceMgmt.SP_DEVINFO_DATA();
	devInfoData.cbSize = 28;
	devInfoData.ClassGuid = Guid.Empty;
	devInfoData.DevInst = 0;
	devInfoData.Reserved = UIntPtr.Zero;
	iRet = Win32DeviceMgmt.SetupDiEnumDeviceInfo(pNewDevInfoSet, iMemberIndex, ref devInfoData);
	if (iRet == 0)
	{
		Int32 iLastError = Win32DeviceMgmt.GetLastError();
		if (iLastError == Win32DeviceMgmt.ERROR_NO_MORE_FILES)
		{
			Console.WriteLine("No more devices in list");
			Console.WriteLine("***********************");
			break;
		}
		else
		{
			iMemberIndex++;
			continue;
		}
	}

	Console.WriteLine("Device: {0}", iMemberIndex);
	Console.WriteLine("\tGuid={0}", devInfoData.ClassGuid);
	Console.WriteLine("\tName={0}", GetClassNameFromGuid(devInfoData.ClassGuid));
	Console.WriteLine("\tDescription={0}", GetClassDescriptionFromGuid(devInfoData.ClassGuid));
	Console.WriteLine("\tInstance Id={0}", GetDeviceInstanceId(pNewDevInfoSet, devInfoData));

	iMemberIndex++;
} while (true);							
							

Following is part of the output from the demo console application after it was run on my machine.

Device: 0
	Guid=72631e54-78a4-11d0-bcf7-00aa00b7b32a
	Name=Battery
	Description=Batteries
	Instance Id=ACPI\ACPI0003\2&DABA3FF&0
Device: 1
	Guid=50127dc3-0f36-415e-a6cc-4cb3be910b65
	Name=Processor
	Description=Processors
	Instance Id=ACPI\GENUINEINTEL_-_X86_FAMILY_6_MODEL_13\_0
Device: 2
	Guid=4d36e97d-e325-11ce-bfc1-08002be10318
	Name=System
	Description=System devices
	Instance Id=ACPI\PNP0000\4&61F3B4B&0
Device: 3
	Guid=4d36e97d-e325-11ce-bfc1-08002be10318
	Name=System
	Description=System devices
	Instance Id=ACPI\PNP0100\4&61F3B4B&0							
							
Go Freelance
Home     About us     Contact us    Copyright    Privacy Policy    Return Policy    Advertisers
Copyright © Netomatix