I've seen it written in C++ which I don't know. I need a C# function that return SDCard's serial in .NET CF. Could anyone kindly help me write a function with it?
The smart device framework will enable you to do this:
foreach(var info in DriveInfo.GetDrives())
{
string manufacturer = info.ManufacturerID ?? "[Not available]");
string serial = info.SerialNumber ?? "[Not available]");
}
First, I'll say that if you buy the SDF ($50) you get the source for this function, which you could then pull into your project and obfuscate.
If, however, you have more time than money and want to implement it yourself, I can point you in the right direction. I leave it to you to string all of these pieces together, but here are the steps:
First, you have to open the Volume like this:
IntPtr hFile = NativeMethods.CreateFile(
string.Format("{0}\\Vol:", driveName),
NativeMethods.GENERIC_READ, 0, 0, NativeMethods.OPEN_EXISTING, 0, 0);
That gives you a handle to the drive. You then simply call an IOCTL to get the storage ID:
byte[] data = new byte[512];
int returned;
int result = NativeMethods.DeviceIoControl(
hFile, IOCTL_DISK_GET_STORAGEID, IntPtr.Zero, 0,
data, data.Length, out returned, IntPtr.Zero);
if (result != 0)
{
m_storageID = STORAGE_IDENTIFICATION.FromBytes(data);
}
What this gives you is a binary representation of a STORAGE_IDENTIFICATION structure. Note that the structure is really only a desciption of the header of the data. It contains an offset to the actual serial number data, which is an ASCII string. Extracting it looks something like this:
int snoffset = BitConverter.ToInt32(data, 12);
string sn = Encoding.ASCII.GetString(data, snoffset, data.Length - snoffset);
return sn.TrimEnd(new char[] { '\0' });