The question is similar to How do I read a disk directly with .Net?, except that the drive that requires direct access is a CD-ROM or DVD-ROM drive. These drives are not including as a physical drive and cannot be accessed using the DeviceStream class or CreateFile Win32 API method as cited in the topic above.
The Windows kernel automatically creates the internal device markers for various attached block devices. The first CD-ROM drive appears as \Device\CdRom0, though attempting to open this via CreateFile() will fail (apparently per http://support.microsoft.com/kb/235128).
Considering unmanaged applications can access these block devices natively (any CD-burning software, DD for Windows, Cygwin [via /dev/scdN]), is there a programmatic method available to access these block devices (preferably usable by .NET)?
You may want to look at this article
This is what I used when faced with an issue of making backups to CD's
Related
I have a use-case where I need to make a block of storage (think any cloud based storage provider or a Database) available as a Windows drive.
I am happy to do the coding but haven't got a clue where to start.
you have to work with Windows Drivers SDK, there are some samples online about this, something "similar" but not 100% saem as what you need is teh RAM drive implementation whcih creates a new drive which uses RAM memory as storage,
have a look here for some source code you can compile in Visual Studio and to see how you have to setup/prepare your machine for Windows drivers development.
https://github.com/Microsoft/Windows-driver-samples/tree/master/storage/ramdisk
See this question:
creating virtual hard Drive
You would need to create a folder location, probably somewhere in the user's folder tree and map the drive as in the answer in the above link. Then use that folder to sync files with your cloud storage. I doubt you'd want to have the files in memory as you may end up with a drive containing gigabytes of data.
Would it be possible to create a program (.NET preferably) to create a virtual drive letter, but when it is read, written to, or browsed an independent program deals with what is returned?
Although you could do it by mapping a drive to a TCP server, webDAV or something like that, I'm wondering if it could be done with internal links.
This would be used for protected storage. The program does stream encryption and decryption of all the files in the drive (as they're read by all kinds of programs) if the program has had a password put into it.
What you're talking about is a storage device driver, which is how programs such as Daemon Tools and TrueCrypt accomplish such "virtual" drives.
You may not have to delve into the kernel to accomplish this, though. Microsoft supply a Windows User-Mode Driver Framework, which is designed to simplify the development of certain common Windows driver types. From what I can tell, you should be able to develop a virtual storage driver using the user-mode driver framework. As long as you're not directly interacting with hardware (like a kernel-mode device driver does), you should be fine. However, you won't be able to do this in C#. You'd probably have to use C, though you might get away with C++.
PowerShell already supports something like this with its Provider model. Certificate store, Active Directory, IIS configuration, SharePoint, ... are all made to look file system like, using the same commands to query and update.
This is at the heart of PowerShell. $foo is the value of variable foo, ${c:\foo.txt} is the content of file C:\foo.txt but used just like a variable. Equally dir HKLM:\Software lists the child keys of that registry key.
You can write you own providers.
There is an open source program, WinCDEmu that can mount an ISO image to a virtual drive. I suppose you'll be able to examine the sources to figure out how to provide a virtual drive that does what you want. The project is written in C++.
It is based on BazisLib, which is a framework for simplifying windows driver development.
I have a USB device which which uses the libusb WIN32 drivers and
Interrupt data is available from the
accelerometer through the USB-HID
interface endpoint 83 (in EP83). Data
is in little end-in format with the
following fields (x,y,z,Vbat,CpuTemp.)
Data are acquired every 62.5ms (16Hz).
There a number of USB HID "get" and
"set" Reports available (through ep0)
How can I access this data via .NET and C#?
libusb32 is c/c++ library of generic usb driver. it comes in 2 layers. low layer in kernel mode is generic client driver libusb0.sys + .inf file that you change and it tells to what device to upload this generic usb driver.They have inf-wizard.exe tool that helps for you to make this .inf file for your device. Upper layer in user space is libusb.lib (you can make also static link) that talks to libusb driver. You can find in sources usb.h that actually defines interface between you code and usb (usb driver). To access unmanaged code or you should write your layer of interop in c# or in c++/cli or use ready layer that been made by somebody. Here the link to one of the projects, http://sourceforge.net/projects/libusbdotnet/
How to use libusb library i advice you to see some example from them. Usually it like you open handle to usb bus, then find there your device by VendorId & ProductId, get it's handle. Then make write/read to endpoints of device.
If it is HID device I recommend you to use Windows's default driver - hid.dll, I used it and it was ok. This way you should not care about deploying also a driver, it's there anyway and you just have to understand the API and use it. For this I recommend you Jan Axelson book USB Complete, she has pretty good explanation and samples there, it is a mixture of C/C++ and C# but the trend is for .net Also she has a HID Page on her website and there you find the code samples you just need.
I have a requirement to implement a feature (in C#) to lock a folder on a USB drive. After locking, in any PC without my software, this folder is inaccessible (or better invisible). I have tried using ACL (Access Control List) but it does not work on USB.
My original requirement is that "Do not allow people to see the content of a folder on a USB drive without my software. When the user logs into my software, this folder is accessible and when the user logs out, my software has to do something to make sure this folder becomes inaccessible on other PCs". This leads me to think about locking the USB folder.
I already tried a work around to zip the folder (not compress) with a password but zipping takes time (about 2 minutes for 1 Gb). So if I have a large amount of data (100 GB) it takes too long to process (The software has to process this when doing logging out).
For the data to be inaccessible, you would need to create an encrypted container which your program would represent as a virtual folder on existing disk or a virtual disk (drive letter). The first is possible with Pismo File Mount, the second - with our product (Solid File System OS edition). Both approaches require installation of the kernel-mode driver to the system. This is not an application requirement but OS architecture requirement.
From technical point of view our approach is more robust because Pismo File Mount uses a filter driver, while SolFS uses a file system driver and filter drivers are more complicated and more prone to compatibility issues (we have filter-based product as well, so I have experience with both approaches).
(Note: TrueCrypt has nothing to do with C#. So that isn't applicable here.)
Have you had a look at TrueCrypt?
In general you shouldn't try to reinvent the wheel, especially with security requirements. This is a heavy topic and messing around with the file system on your own should be the last option to consider.
TrueCrypt uses two different modes of operation:
File-hosted (container)
Partition/device-hosted
Note: In addition to creating the above types of virtual volumes,
TrueCrypt can encrypt a physical
partition/drive where Windows is
installed (for more information, see
the chapter System Encryption).
A TrueCrypt file-hosted volume is a
normal file, which can reside on any
type of storage device. It contains
(hosts) a completely independent
encrypted virtual disk device.
A TrueCrypt partition is a hard disk
partition encrypted using TrueCrypt.
You can also encrypt entire hard
disks, USB hard disks, USB memory
sticks, and other types of storage
devices.
I have used this software before, which is open source, so you might get something out of it.
http://www.truecrypt.org/
However, I think it works at the partition level rather than the drive level (I think it formats the partition using its own file system).
How do I get the size (free, total) of any drive on a Windows Mobile phone using C#?
I need to do this with code running on the device (not on a connected PC).
I've rewritten the answer based on a better understanding of the question, but without losing any of my original answer for those who find this question.
Getting the size from code running on the device
The answer is simple: P/Invoke GetDiskFreeSpaceEx directly (example here) or use a third-party library that does it for you.
I'm also going to edit the post to make it seem like my assumptions are correct - change them to read otherwise if you need to.
You have a P/Invoke definition. This is simply a static method call. You put it in a class. Something like this:
public class MyClass
{
[DllImport("coredll.dll", SetLastError=true, CharSet=CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
out ulong lpFreeBytesAvailable,
out ulong lpTotalNumberOfBytes,
out ulong lpTotalNumberOfFreeBytes);
}
Then you call it (no idea what you're trying to do with all of your marshaling stuff):
ulong GetDiskSize(string volumeName)
{
ulong avail;
ulong total;
ulong totalfree;
MyClass.GetDiskFreeSpaceEx(volumeName, out avail, out total, out totalFree);
return total;
// return others as desired
}
Then to use it it's something like this:
ulong diskSize = GetDiskSize("\\Storage Card");
Getting the size from code running on the PC
While the device shows up in Explorer like a "drive", it is not a drive. A shell extension is used to get it to appear in Explorer. You cannot use any drive APIs, in the framework or via P/Invoke, to get information about the device.
If you need to get information about a device connected via ActiveSync (XP and earlier) or WMDC (Vista) then you have to use the Remote API, or RAPI. The specific API is CeGetStoreInformation, but you have to do some initialization before you can call it.
RAPI is a bit too complex to cover in an answer here, but it's well documented online, and there is a ready-made managed wrapper for it (free and open source) here. The specific call you are after in that library is RAPI.GetDeviceStoreInformation.
Assuming the phone's drive is mounted as a regular volume in Windows (i.e., it has a drive letter in Windows Explorer), you can obtain this information using the System.IO.DriveInfo class.
Next problem, of course, would be to determine which drive letter to use, as that will be different on just about every PC the phone is plugged into.
A couple of options for that:
Scanning all available drives on the system for a special file that's guaranteed to be present on the phone. Environment.GetLogicalDrives will give you the list of all available drives on a system; some of these may be removable or otherwise inaccessible, though, which can cause some undesirable side-effects during your scan...
A more advanced approach would be to enumerate all available USB devices, and find your phone using its manufacturer/device ID. Although written for a slightly different purpose, this CodeProject article should point you in the right direction...
And in case you want to run your code directly on the phone, and not on the PC it's attached to (your question doesn't elaborate on that...), this StackOverflow question shows how to obtain free space information on the Compact Framework.
EDIT, in response to the elaboration that "when i use DriveInfo and give it the drive symbol it gives me the size of the local drive " My C: drive " not the C drive exists on the mobile": the C: drive of the phone will have a different drive letter (for example, F:) on the PC it's mounted on -- therefore the part of my answer that talks about finding the right drive letter. This does, of course, assume that your phone's drive is made available in its entirety on the host PC: depending on the phone model, this may or may not be the case. If not, running some code on your phone is the only viable solution for getting this information...