I have a USB mass storage device that I encrypt with TrueCrypt. When I connect it to Windows, it asks to format. Using TrueCrypt reveals its contents, as expected.
How can I read the first 100 bytes of that device?
I know the bytes will not make sense because they're encrypted but I want to read them in order to create a checksum.
Did this on the top of my head. But is should work.
public static long getBytes(string letter)
{
ManagementObject disk = new ManagementObject(String.Format("win32_logicaldisk.deviceid=\"{0}:\"", letter));
disk.Get();
return long.Parse(disk["Size"].ToString());
}
EDIT: Tested it and changed int to long. It works.
What solutions have you considered so far? Does your application figure out when the USB device is plugged in or unplugged?
As far as I know, there's no native support in .Net for directly accessing USB devices. I had to use libraries such as LibUsbDotNet (http://sourceforge.net/projects/libusbdotnet/) or SharpUSBLib (http://www.icsharpcode.net/OpenSource/SharpUSBLib/) There were pros and cons to both in terms of samples, documentation etc. I am sure you will be able to find what suits you best.
In one case I was able to connect to the device using WMDC, once the connection was established I used OpenNETCF RAPI library to read from / write to the device.
Here's another excellent resource that I had found useful when I wrote an application that needed to interact with a USB device (a barcode scanner).
http://www.developerfusion.com/article/84338/making-usb-c-friendly/
There was a good resourceful discussion to a similar question here on Stackoverflow : Working with USB devices in .NET
Related
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!
I am trying to write a program that uses dual ported RAM. I have a made a pointer to the memory address listed in the resources for the device in device manager. But every time I try to read from it, I get an Access Violation, what I am doing wrong? According to the device manufacturer, an offset of 0x0800 is open to read and write.
IntPtr ptr = new IntPtr(0xF7E00000);
float value = Marshal.ReadInt32(ptr, 0x0800);
MessageBox.Show(value.ToString());
If this is a physical address, you need a device driver. You can't create a device driver with .NET. If the device comes with a driver, it probably has an API you can call.
If you really need to write a driver, I'd recommend downloading the DDK from Microsoft, and learning C and kernel-mode programming. This is not a simple task.
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.
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
I am working on a project where I need to access some specific addresses of a USB drive (e.g. sector 0), and change some bytes. I have done some parts with C# already that includes user interface, detection of USB drives etc. Can anybody help me providing some links so that I can access specific addresses of USB drives with .NET?
The Framework doesn't support this. If you attempt to create a FileStream on a device it will throw an exception. You will have to use the Windows API methods directly (p/invoke CreateFile, DeviceIoControl, etc). Make sure you read the section on Physical Disks and Volumes here:
http://msdn.microsoft.com/en-us/library/aa363858.aspx