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

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.

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.

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

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).

Custom USB CDC Device Missing After C# GUI Crashes

I'm using win7-64bit
I am in the process of developing a user interface for a USB CDC device (used as a flash programmer) I am adding in exception handlers and constantly testing, but whenever something does go wrong and the program crashes, the USB device disappears from device manager and will not reconnect. I uninstall and reinstall the driver (which I have made automatic in the c# GUI) but this doesn't fix it either. Nor does resetting the computer, changing USB ports, etc.. The only way I know to fix it is to delete the registry entries for the specific PID / VID stuff and then unplug the USB and plug it back in. Windows the reinstalls it (because it still has the inf file) and it works perfectly. This shouldn't be an issue in the release version, but for testing it has been a real problem constantly going through that process.
My question is why is this happening?

C#. Android and NFC

I have a C# app running on my windows machine. The app can detect a specific phone (over bluetooth) and according to the phone's presence or not, it popups a message to the user. The C# app checks for the phone's presence periodically. So far so good.
I want to add a third "player" in the game i.e. an NFC smart card.
Now, I want the Android phone to act as an intermediate; it will receive the periodically sent request from the C# (windows machine) application, will check if a certain smart card is close (over NFC) and get back the answer to the C# application. So, now the C# app checks for both the phone's and (indirectly) the card's presence.
I want the application on the Android to constantly listen to the bluetooth interface and if the request from the C# (windows) application arrives, to check if the card is close. The answer ("yes" the card is here - "no" the card is not here) should be sent back to the C# application. I currently don't care about security or anything else. Only the existence of the card.
Do you think that this can be done?
Thank you!
The problem is, NFC in Android does not work that way. Its not as if the phone is "checking" if there are some cards near. Its event driven, not polling. When a card is put close enough to the phone, an aproriate intent is fired. your app can handle it.
As for your particular problem, the solution would seem to be setting up a flag each time a card is detected and feeding it to C# app via bluetooth. Remember tho, NFC isnt putting a card into a contact reader. The card may have been near the phone a second ago, but it might as well already be gone. Also, eep in mind, that NFC range is small - you have to almost touch the phone with the card.
On handling NFC intents check out http://developer.android.com/guide/topics/connectivity/nfc/nfc.html and http://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc.html
I did not work with NFC-Tech or Android this far, but it should be possible.
Having the C#-Code constantly listening to the Android device is ok, like you said, you will need your android device to route the NFC-Listener.
When the Android device detects an NFC-Card, it should be easily given to the C# Code via some custom method.

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