How to detect particular device's USB port with C# .NET? - c#

I have a device with a USB port and I'm using a WPF app to communicate with it.
Now when the user executes my program it can detect all the available ports and let the user to choose from a drop down menu. The device in this case is this.
So far it is fine, but in this case the user has to first go to Device Manager and figure out the port of the device before selecting and connecting in my program.
Is there also a way that my program can automatically detect the particular device's port?
(I guess in my program I need some info about the device but what I don't know).

Related

Can you have the name of the arduino pre-defined in the system?

I was developing my project and I ran into a problem! I put my C# program so when a device is connected with the name arduino, the C# program automatically detects that that is the COM input, but I tried it on another computer and when I connect the arduino, the arduino name on windows is not arduino, is there any way any computer can know that the connected device is the arduino when it connects?
On the other pc the name is "USB Serial Device (COM3), can i pre-define arduino?
I think microsoft has a way you can register your product so they can recognize it. Companies like Logitech use this so when you plug in their mouse the OS can see that device and then recognise it.
The idea is that you pay microsoft to write a handshaking routine for their OS. Then on the next upgrade the feature goes active.
Two problems.
#1 it would cost thousands of dollars for microsoft. Same for apple.
#2 the arduino would have to have the handshaking routine or unique identifier. The added chip would then be required for the handshaking to happen.
So the answer is yes it is possible. But the cost of implementation would be very high and it would require all arduino board makers to buy the chip or add the handshaking routine.

Running DevicePicker class in Kiosk UWP App

currently im developing a UWP Kiosk app. This app needs to print to a PosPrinter (EPSON TM-T20II) which is connected via ehternet to the network, The app runs in another pc connected with wifi to the same network. I have created the app using a DevicePicker class just like this example from Microsoft
PosPrinter c# configuration sample
in this example you open a poppup window that lists all your connected devices and filters the Printer. You can choose it and you will have a connection to that printer. The problem is when im using the app in Kiosk Mode in Windows 10 this windows with all the connected printers will not show up, and then i cannot stablish any connection. I have look into the set-multiple-kios apps option for windows 10, but i dont know how to stablish a path to the class known as DevicePicker to allow his functionality with my Kiosk app.
DevicePickerClass
Multiple-Kiosk-Apps
I have also tried to use PosPrinter.GetDefaultAsync() method to get the Printer directly but it always returns null, i also added the deviceCapabillities in my appxmanifest.xml
So if anyone could provideme with a hint or something i will be very grateful.
I found a solution by using the class FindAllAsync(DeviceClass), this return a collection with all connected devices belonging to that DeviceClass(in my case PosPrinter), after that i just use the Deviceinformation.id to pair the device with my program, like this: Posprinter.FromIdAsync(String deviceId), This way the device picker wont appear, so the Kiosk App will just find the Device and then will pop up a permission asking to Allow the device or not.

Make 3rd Party program on Windows believe a HID device is connected

It may seems to be an unusual question at first but let me explain what I try to do.
I've developed a program which is able to hook into a 3rd party program called PS4 Remote Play. The PS4 Remote Play program only allows you to use a real Dualshock gamepad for controlling your games.
So my program is hooking into low level APIs from Windows (kernel32.dll, hid.dll) to bypass this limitation. What my hooks are doing is whenever CreateFileW is called for an HID device my program returns a custom dummy pointer and the corresponding hooked hid.dll methods which are called afterwards are acting like this dummy pointer is a Dualshock gamepad.
So far so good. But recently I got some reports from users of my program that the "emulated" Dualshock is not working and the inputs send to this virtual Duashock are not processed. It took me some time to figure out why. Whenever a user is using a laptop or a PC without an HID device connected, the PS4 Remote Play program never calls the CreateFileW, at least not for creating an HID handle.
I don't want to install a dummy driver with my application for just making Windows think an HID device is connected. Instead I try to hook into the functions of setupapi.dll and only make the PS4 Remote Play program think a random dummy HID device is connected so that it calls the CreateFileW method. I'm already hooked into
SetupDiGetClassDevs
SetupDiEnumDeviceInfo
SetupDiGetDeviceInterfaceDetail
But I have no experience with this API so I have a few questions.
Does the device information set returned by SetupDiGetClassDevs always contain a device information element for HID devices even when no HID device is connected? If not, how can I add a fake device information for HID devices or how can I create a fake device information set in C# and return that instead? Or is this not needed at all, as I can just hook into the other two methods and do something there.
Any advice or hint how I could solve this issue would be great. It is not necessary that Windows think an HID device is connected only the 3rd Party program should act like there is one connected to the PC.

How to implement Continue Reading feature?

I want to implement Continue Reading in my app.
How do I find all the device connected to the computer or the phone?
How do I open the app from the other device?
For Reference
I like the way MSPoweruser app has implemented this feature. In that app If we click Continue Reading button, it will show all the devices connected to this device. So if I click on a device it will open the app on that device.
There is an SDK for that in UWP - Project Rome.
In many ways.
For example:
Your server where to register the user's avaliable devices. If user push "Continue" button device get all avaliable devices and show list. After user choice, device send state to new device.
Or
You send state to server after push button and new device get state from server.

Connect and disconnect USB programmaticaly "WITHOUT UNPLUG AND REPLUG"

I need to connect and disconnect USB programmatically. That is, I have inserted the USB device. I need to transfer the file using C#, .NET application. The application will watch the particular folder and transfer the file from that folder to a USB drive. I need to disconnect the USB device after the file is transfered and connect the USB when needed - without unplug and replug.
What would be some code to do it or is there any DLL file available?
Main thing: NOTE, NOTE: Without unplugging and replugging the USB device.
If your goal is to make a certain disk volume unavailable while you're not using it, a more sensible approach might be to use the volume management APIs, e.g. by using the IOCTL_VOLUME_OFFLINE control code. (I'm assuming that you know which drive letter belongs to your USB disk.)
Alternatively, you can disable and enable the volume device programmatically with the CfgMgr / SetupAPI -- the same as right-clicking the volume in Device Manager and choosing Disable would do. (For information about using SetupAPI, please review the DevCon sample code provided with the Windows WDK, and see MSDN for functions such as SetupDiChangeState.)
The latter option might require a privileged user account.
I'm not exactly sure, but it can be done. In Linux, I've experienced certain situations where power is disconnected to a device programmatically. The kernel usually does this if the connected USB device is exhibiting too many errors. So, it should be possible to do this even in Windows. You may need to write your own external DLL to do it though.

Categories