views:

47

answers:

2

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?

+1  A: 

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]");
}

See here. I asked the same question myself last year.

GenericTypeTea
Yes, I know sdf. But due to security concern, I would like a function in my own exe file. So that I can obfuscate.
VOX
Good luck. Be sure to post back here if you get an alternative.
GenericTypeTea
+2  A: 

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' });
ctacke
Using SDF Community edition for testing, I'm getting null in both ManufacturerID and SerialNumber. How could I solve it? I'm on WinCE 6.
VOX
Check the flags in the STORAGE_IDENTIFICATION to see if it thinks teh values are valid. It's possible that your SD driver simply doesn't support retrieving these.
ctacke