C# help

S

Scott Dowd

I need explicit example of how to use SetupDi* functions
in both C# .NET and VB .NET to determine device interface
detail for a given device class with interface information
detail. All examples available on the web and/or
Microsoft documentation are C or C++ based and do not take
into account the various issues and pitfalls that the .NET
based environment incurs, or otherwise avoids over
unmanaged code, in short, no examples of how to use the
SetupDi* functions in a managed code of any significant
scenario exist.


Of specific interest is how to get the ‘Device Path’
information from SP_DEVICE_INTERFACE_DETAIL_DATA structure
via a managed code example in C# .NET and VB .NET, which
would include examples of how to declare, and use
following API functions…

SetupDiCreateDeviceInfoList()
SetupDIGetClassDevsEx()
SetupDiEnumDeviceInterfaces()
SetupDiGetDeviceInterfaceDetail()
SetupDiDestroyDeviceInfoList()

For example, via only official and proper methods for a
managed code environment, determine the actual number of
tape devices that are present, and which publish
interfaces, and what the device path is for each device,
i.e. \\.\Tape0, \\.\Tape1, etc. etc.
 
T

Tian Min Huang

Hello Scott,

Thanks for your post. As I understand, you want to call SetupDi functions
to get hardware information from .NET Managed code. Please correct me if
there is any misunderstanding. Now I'd like to share the following
information with you:

1. Since SetupDi are unmanaged APIs, we have to use Platform Invoke to call
these functions in .NET. Please refer to the following MSDN article for
detailed information on P/Invoke:

Consuming Unmanaged DLL Functions
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconconsumingunmanageddllfunctions.asp

One thing difficult is interop marshaling which governs how data is passed
in method arguments and return values between managed and unmanaged memory
during calls. Especially when we pass sophiscated data structures and
arrays. I believe the article below is very helpful:

Marshaling Data with Platform Invoke
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconmarshalingdatawithplatforminvoke.asp?frame=true

As you know, SetupDi functions are documented in DDK, please download
appropriate DDK from Microsoft:

Microsoft Windows Driver Development Kits
http://www.microsoft.com/whdc/ddk/winddk.mspx
Regards,

2. In .NET environment, I strongly recommend you using classes under
System.Management to access WMI. WMI (Windows Management Instrumentation)
is a scalable system management infrastructure that uses a single
consistent, standards-based, extensible, object-oriented interface. WMI
provides you with a standard way to interact with system management
information and the underlying WMI APIs. For example, the following code
snippet demonstrates enumerating PnP registered devices using WMI class
Win32_PnPentity:

//-------------------code snippet--------------------
using System;
using System.Management;

class App {
public static void Main() {
ManagementPath path = new ManagementPath();
ManagementClass devs = null;
path.Server = "."; // local machine
path.NamespacePath = @"root\CIMV2";
path.RelativePath = @"Win32_PnPentity";
using(devs = new ManagementClass(new ManagementScope(path), path, new
ObjectGetOptions(null, new TimeSpan(0,0,0,2), true)))
{
ManagementObjectCollection moc = devs.GetInstances();
foreach(ManagementObject mo in moc) {

PropertyDataCollection devsProperties = mo.Properties;
foreach (PropertyData devProperty in devsProperties ) {
Console.WriteLine("Property = {0}\t Value = {1}",
devProperty.Name, devProperty.Value);
}
}
}
}
}
//-------------------------end of---------------------------

Please refer to the following MSDN articles for detailed information:

System.Management Lets You Take Advantage of WMI APIs within Managed Code
http://msdn.microsoft.com/msdnmag/issues/02/05/WMIMan/default.aspx

Managing Applications Using WMI
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconmanagingapplicationsusingwmi.asp

Computer System Hardware Classes
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/
computer_system_hardware_classes.asp

3. By the way, it would be best to post WMI questions in the following
newsgroup.
microsoft.public.win32.programmer.wmi

Hope this helps.

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
T

Tian Min Huang

Hi Scott,

I'd like to follow up and see if there is any further I can do for you.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
S

Schorschi

Tim,

I am a friend of Scott, and reading this question and answer, the one
thing that your suggestions do not point out is how to get the device
name/path? The only why I know of to get this 'device path' is from
SetupDI* routines, WMI does not include it-that I can find...
"\\.\Tape0" for example... is only available via the SetupDI*
functions in the API.

Scott needs a working example in VB .NET or C#. Although I have done
this device path retrieval from straight C, getting it to work via VB
..NET or C# does not seem to work, or both Scott and I are missing
something in the translation. Hence the need for a working example.

What would really be cool is a working example that returns
"\\.\Tape0" or even "\\.\PhysicalDisk0"? I would love to see someone
convert the EnumDisks example that Microsoft published in straight C
to VB .NET. This would illustrative the solution well.

Schor-
 
T

Tian Min Huang

Hi Schor,

Thanks for your response. As you know, "\\.\Tape0" is an MS-DOS Device Name
which maps to the underlying NT device name. I suggest that you can use
QueryDosDevice() API to retrieves information about MS-DOS device names.
Please refer to the following MSDN articles for detailed information:

QueryDosDevice
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base
/querydosdevice.asp

INFO: Understanding Device Names and Symbolic Links
http://support.microsoft.com/default.aspx?scid=kb;en-us;235128

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top