Block USB, based on Volume/Capacity - c#

I want to block a USB based on volume using C#. Like I want to block USB stick if capacity is greater than 8GB.
Look there is a method to block USB on PC using registry. but this will make USB undetectable so I can not get volume information.
I want to do that If my client program is running on some machine and I put restriction that USB capacity >=8GB should be restricted, so my C# code should safely remove the USB and should now show any balloon, I mean silently remove it.

If you are disabling the USBSTOR key, then you prevent "ACCESS" to it, that includes gathering information from it.
I suggest you look here: WM_DEVICECHANGE
Using this you can catch when the USB is entered and then get the drive letter and use the DEV_BROADCAST_VOLUME to gather information on it, then you can disable it using your registry method.

Related

What are the registry Device "LocationInformation" key sections for USB Devices?

I am currently iterating through a list of USB Cameras connected to my computer that I retrieved with the following code:
DsDevice[] cams = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
I then determine the HardwareID and InstanceID values from the DevicePath for each device, and I look up the following registry key for all devices:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB{HardwareID}{InstanceID}\
I then read the LocationInformation subkey value. Here are some examples of what I'm receiving based on which USB port the device is plugged in to:
0000.001a.0000.001.003.000.000.000.000
0000.001a.0000.001.004.000.000.000.000
0000.001d.0000.001.008.000.000.000.000
0000.001d.0000.001.007.000.000.000.000
0000.001d.0000.001.005.000.000.000.000
0000.001d.0000.001.006.000.000.000.000
It appears that the second block (index 1 of the returned Split('.') array) is a distinction between the internal USB hub that the USB port is connected to. Is this correct?
I have deduced that the fifth block (index 4) is the USB port number, but on a separate computer I'm using (an Intel Compute Stick), the port number differentiation actually occurs in the fourth block (index 3). Is there a way to know which block I need to check based on the computer? (Third-party libraries are acceptable though I'd much rather use built-in functionality of .NET or straight Windows APIs that I can hook into, if possible.)
Is there any documentation on what each section of the LocationInformation is? I have Googled for information, but I've come up short of what I'm looking for. Perhaps I just don't know the best search terms to find this information. In any case, I'd bet that this documentation could be extremely useful to other StackOverflow users as well.
Thanks!

Identify laptop screen

For my project I need a way to get data regarding screens that are connected.
In specific, I need to identify whether a monitor is a laptop internal screen or an external screen, and get all the screen data.
I need to get this information both in c++ and C#.
I read about Win32_DesktopMonitor, about EnumDisplayDevices and about Screen Class.
I read also some related questions here:
Monitor ID and Serial Number
Find Number and resolution to all monitors
EnumDisplayDevices vs WMI Win32_DesktopMonitor, how to detect active monitors?
I havn't found an answer yet. Any Ideas?
To get the information whether monitor is internal you can use WMI class WmiMonitorConnectionParams from root\wmi namespace.
Code would need to create a CimSession by connecting to WMI either through DCOM or WinRM (with authentication as needed if enumerating remote computer monitors), then call EnumerateInstances(#"root\wmi", "WmiMonitorConnectionParams") on that session.
Resulting collection will contain InstanceName (string) and VideoOutputTechnology (UInt32).
You will need both for each monitor so you can match them with the other stuff you need.
If VideoOutputTechnology is 0xFFFFFFFF, then that's the Default Monitor entry and it can be ignored. If it is 0x80000000, then it is internal. Other types of connection are documented in d3dkmdt.h header file (online documentation at the moment of this writing does not provide correct values for the enum).
The most reliable way to get model, serial number, as well as year and week of manufacture is by reading and parsing the raw EDID block (by calling WmiGetMonitorRawEEdidV1Block).
I hope you can get by on your own from here.
What you can do is query the Windows WMI classes:
http://msdn.microsoft.com/en-us/library/aa394554(v=vs.85).aspx
Those classes allows the user to collect various information about the computer (hardware, os, ...)
I don't know if you'll find the properties you need, but it might be worth a look.
You're looking for this class:
http://msdn.microsoft.com/en-us/library/aa394122(v=vs.85).aspx

Intercept and modify USB packets

The project is as follows;
I would like to create an application that will be able to see packets going to a USB device. When a certain bit of data that is known is sent to the device, the returning data will be intercepted and modified before the application requesting it, gets it. I have used a USB sniffer to see the packets being sent and I know exactly what bits need to be changed. My two questions are;
Is this a possible software solution?
Will this have to be a hardware solution?
Additional Information –
The USB device uses a FTDI245R chip for communication. I know the VID and PID of the device.
I have experience programming in vb.net and C# but I have never done anything with USB
I would like the application to be able to have a number entered and changed to hex data and that is what would be sent to the device. The number being entered would be changed frequently.
Any input is appreciated.
It sounds to me like you want an upper filter driver to the FTDI driver. I don't know what class or type of device you are using or if it has a vendor specific driver or not, but here is a sample that shows how to create an upper filter to a vendor specific driver. This example uses the OSRUSBFX2 device and sample driver in the WDK, you'll want to change the code to work and interface with the FTDI driver instead.

Is there a simple and efficient way to get listed all usb devices in C# or C/C++?

I have to make a program which monitor usb ports and when an usb device is plugged (joypad, flash drives, mouse, ecc...) I get a unique identifier (a deviceid or something else would be good).
At first I tried with C# using the system.management classes and querying the cim_logicaldevice class each second to get the new device plugged.
Some device returned more rows with DeviceID, but this isn't a problem. The problem is that the memory occupied by the program (in task manager) grows up constantly.
This is the source code:
http://pastebin.com/dQv3cMQC
Is there a way to avoid the growing of the memory usege?
I have to do this program in C++ or C# and it has to be the most efficient possible (because it has to be opened forever).
I would recommend looking at the USBView sample in the WDK. If you are unfamiliar with this, simply run it - this tool walks the entire USB tree on the system and prints out information and descriptor listings for each device.
In your case, I'd start at the RefreshTree() function in this sample, you can then follow the code to see how it enumerates the host controllers, hubs and finally devices. For each device that you find you can look at the bInterfaceClass in the interface descriptors to find out what types of interfaces it is advertising.
The easiest way to get the source to this sample is to install the 7.1.0 WDK which is currently available here: http://www.microsoft.com/en-us/download/details.aspx?id=11800

Mounting/Unmounting USB disks on Windows

On Windows Server, by default, external USB disks don't always get mounted. I'd like my program (written in C#) to be able to detect external USB disks, identify them, and then mount them.
When it's finished, it should do whatever the programmatic equivalent of "Safely Remove Hardware" in order to flush and unmount the disk.
Any pointers?
You are searching for the functions SetVolumeMountPointA and DeleteVolumeMountPointW.
In order to find Volumes you could use FindFirstVolumeW and FindNextVolumeW.

Categories