Get usb hardware or instance id using c# - c#

I'am trying to get usb mass storage instance id for eject usb using devcon from last 2 days but not able to get that. I have tried alot of solutions but still failed.
ManagementObject dsk = new ManagementObject(#"win32_logicaldisk.deviceid=""j:""");
dsk.Get();
string id = dsk["VolumeSerialNumber"].ToString();
Console.WriteLine(id);
It returns instance id. when i pass this id to devcon it doesnt remove any device. Passing id to devcon in this way
Devcon remove usbstor\deviceID
is there any way to remove usb using device name as like G, H or I etc or get required instance id.
Any help will be appricated.

Try disabling UAC and also running devcon.exe under Administrator credentials.

I think #usblib can be helpfull for you , it allow you to play around usb divices #usblib

Related

How to map DirectInput (sharpDX) device to it's corresponding HID (C#)

What I need is a way to find out the curresponding HID of a SharpDX.DirectInput DeviceInstance.
What I have on DirectInput side is :
ProductId -> 00060079-0000-0000-0000-504944564944 (always the same)
InstanceId -> 8e3d89c0-6436-11e9-8004-444553540000 (dynamic / changes every time PC starts)
On HID side :
VendorID=0x0079
ProductID=0x0006,
Version=263,
DevicePath=\\?\hid#vid_0079&pid_0006#8&1ec29a1c&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
I can see that the HID vendorId and ProductId are used to build the DirectInput ProductId. the problem is that I have multiple controllers connected with the same VendorId and ProductId (they are the same brand)
is there a way to do this? to know the HID devicePath of an DirectInput InstanceId?
For context:
I'm writing a software to automatic configure many controllers on many emulators for my custom arcade machine. The emulators use a combination of DirectInput/XInput/DSUClient (https://github.com/v1993/cemuhook-protocol). So, for each connected controller, I need to know all controllers info to be able to correctly configure each one on each emulator.
For now as a (very bad) workaround, i'm listening to inputs from DirectInput separeted from HID and when the reading occurs I know that the HID device is the DirectInput device because they occured (almost) on the same time.
I've already tried "Win32_PnPEntity" with "ManagementObjectSearcher" but had no luck
There is DIPROP_GUIDANDPATH property that could be requested via IDirectInputDevice8::GetProperty API. It will return HID device path.
In SharpDX it is represented by SharpDX.DirectInput.PropertyGuidAndPath struct.
I ended up finding the solution...
DirectInput directInput = new DirectInput();
foreach (var deviceInstance in directInput.GetDevices())
{
var joystick = new Joystick(directInput, deviceInstance.InstanceGuid);
Console.WriteLine(joystick.Properties.InterfacePath)
}

Is PNPDeviceID of video card adapter unique?

I need to get a unique id of video card adapter. When searching in the properties of the device (using Device Manager of Windows), I notice that there is a property named Hardware Ids as shown in image bellow.
I tried to get these Ids in my winform application. I found this method:
string VideoCardInfoID()
{
ManagementObjectSearcher objvide = new ManagementObjectSearcher("select * from Win32_VideoController");
string output = string.Empty;
foreach (ManagementObject obj in objvide.Get())
{
output += (obj["PNPDeviceID"] + "\n");
}
return output;
}
The output of this code is:
PCI\VEN_10DE&DEV_1055&SUBSYS_908A104D&REV_A1\4&F7451F8&0&0008
I have two questions:
Is PNPDeviceID of video card adapter unique across all machines? does it change when new fresh Windows installed? I know there some similar questions in stack overflow, but they does not contain a clear answers, such as this question and this question.
Why there is additional characters in the output of the c# function (\4&F7451F8&0&0008)?
Update: I try install new fresh Windows and the Hardware Ids and PNPDeviceID still the same, But I still don't know if PNPDeviceID unique across all machines (I mean the same as MAC address).
Is PNPDeviceID of video card adapter unique across all machines?
No. Essentially what this string is comprised of is
<Bus>\<Device ID>\<Instance ID>
The Instance ID is only unique within the context of the current system, and it may not even be unique for the whole system, only for the device's bus.
That is, if you have two identical video cards installed in the computer, they will have the same Device ID, but different Instance ID.
A graphics card driver might use its own serial number in the Instance ID. So it is possible that Instance ID is globally unique, but WMI cannot make that guarantee for all PNP devices.
At this point you will likely have to use a per-vendor documented way to determine the device's serial number, if at all possible.

Retrieve name of bluetooth device from rfcomm device services

The context is the following, we have multiple trucks that contains a bluetooth to serial device, we have given each truck bluetooth a unique name to be able to connect to a specific truck.
I use this code to retrieve all the RFComm services :
DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort))
The problem is that all the DeviceInformation objects returned contains the name of the RFComm service in the Name property instead of the bluetooth device name. When my project was a Win 8 store app, all was fine since the name property contained the bluetooth device name.
I found that I could create a BluetoothDevice object using the device id returned by the above code, but then the app ask to use the bluetooth device for all devices until I find the good one. I would like to prevent that as it wasn't the case with Win 8 store app.
Second solution I found was to parse the device id of the RFComm service which look like this one
Bluetooth#Bluetooth00:c2:c6:56:b0:61-00:15:be:0f:02:d7#RFCOMM:00000000:{00001101-0000-1000-8000-00805f9b34fb}
to remove everything past "#RFCOMM" and use the DeviceInformation.CreateFromIdAsync() function. This works but I was wondering if there was a cleaner solution to my problem since parsing a string can be a real problem if the string format change.
Is there a way to retrieve the name of the bluetooth device without having to ask to use all bluetooth device until we find it?
You can try with following code to get the name of the Bluetooth device:
var serviceInfoCollection = await DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort), new string[] { "System.Devices.AepService.AepId" });
foreach (var serviceInfo in serviceInfoCollection)
{
var deviceInfo = await DeviceInformation.CreateFromIdAsync((string)serviceInfo.Properties["System.Devices.AepService.AepId"]);
System.Diagnostics.Debug.WriteLine($"Device name is: '{deviceInfo.Name}' and Id is: '{deviceInfo.Id}'");
}
The key point here is that Bluetooth device is one type of AssociationEndpoint objects. AEPs usually represent a device discovered over a wireless or network protocol. An AssociationEndpoint object is a child of a single AssociationEndpointContainer object and can contain 0 or more AssociationEndpointService objects. And RFComm service is one AssociationEndpointService that Bluetooth device contains. For more info, please see DeviceInformationKind enumeration and Enumerate devices over a network.
AssociationEndpointService has several properties. One of them is System.Devices.AepService.AepId which represents the identifier of the parent AssociationEndpoint object. So we can use this property to get the Bluetooth device information and once we get the device information, we can get the device name easily. However System.Devices.AepService.AepId property is not a commen property in DeviceInformation. So we need to use DeviceInformation.FindAllAsync(String, IIterable(String)) method to require this additional propertie. For more info, please see Device information properties.

Cannot create UsbDevice from DeviceInformation.Id

I'm trying to connect to a device (SecuGen Hamster Pro 20) through Windows.Devices.Usb APIs using Universal Windows App for PC only (no phones)(WinRT).
The device is a fingerprint scanner.
I've done all the steps found online to do that:
I've looked for all devices using:
var myDevices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync();
This returns about 1400 devices. After some filteration using:
var resultList = myDevices.Where(s => s.Name.ToLower().Contains("secu")).ToList<DeviceInformation>();
resultList contains 3 devices in my machine (I've tried it on other machine and found 10 results on some).
I didn't use the overload for finding devices DeviceInformation.FindAllAsync(String aqsFilter) because it returns 0 results althought I'm sure I've done it right (used correct VID & PID)
The problem here is when I try to create a UsbDevice object from any of the 3 results using:
UsbDevice device = await UsbDevice.FromIdAsync(resultList[0].Id);
The return value is null, I've tried all of them (resultList[0] , resultList[1] , resultList[2]) with no luck.
I configured the capabilities using:
<DeviceCapability Name="usb">
<Device Id="vidpid:1162 2200">
<Function Type="name:vendorSpecific"/>
</Device>
</DeviceCapability>
I also tried to create a UsbDevice object from any of the 1400 devices returned from DeviceInformation.FindAllAsync() but all returned null and even some throw an exception that says the system cannot find the file specified 0x80070002
I tried to read DeviceAccessInformation for the device it returned DeviceAccessStatus.Unspecified
Anyone can lead me to what am I missing here?
You have to use UsbDevice.GetDeviceSelector and then use the selector when searching for the device. If that returns nothing, then the device isn't properly 'configured' to use the WinUSB.sys driver. (And from what I understand, it must use that driver to be used with the usbdevice class).
If you manually told it to use that driver in the device manager, then, in theory, you still have to change a key with regedit before that works (Note: I did that and it still wouldn't work). I found a solution that solved it here:
http://www.lewisbenge.net/2013/09/20/integrating-windows-8-1-with-owi-535-robotic-arm/
Basically, you have to install the driver using an inf file. use the one linked on that site and replace the NTamd64 with NTarm depending on the target platform
Firstly, try to narrow down your search by vendor and product Id
This method will help you do that:
public static async Task<List<wde.DeviceInformation>> GetDevicesByProductAndVendorAsync(int vendorId, int productId)
{
return ((IEnumerable<wde.DeviceInformation>)await wde.DeviceInformation.FindAllAsync($"System.Devices.InterfaceEnabled:=System.StructuredQueryType.Boolean#True AND System.DeviceInterface.Hid.VendorId:={vendorId} AND System.DeviceInterface.Hid.ProductId:={productId} ").AsTask()).ToList();
}
https://github.com/MelbourneDeveloper/Hid.Net/blob/c69e3368343e59e51e8818c87dbea00e6ccfecae/Hid.Net.UWP/UWPHelpers.cs#L11
You will be able to get a list of connected devices from that. You should then be able to connect with FromIdAsync.

Can't find device on libusbdotnet

I am a beginer on USB.
I found libusbdotnet on http://sourceforge.net/projects/libusbdotnet/ i am using the library.
But i can't find my device by command
MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);
//public UsbDeviceFinder MyUsbFinder = new UsbDeviceFinder(0xc251, 0x2722);
The vendor and product ID are the vender, product ID of Keil ULINK2.
MyUsbDevice always null although The USB is attached.
Thank for help!
You need to download LibUsbDotNet_Setup.2.2.8.exe and run setup. During setup you should enable filter and choose your input device per your vendor id. Then your code will find the device afterwards

Categories