需要一个 C# 函数来获取 .NET CF 中的 SDCard 序列号

发布于 2024-09-09 03:08:17 字数 76 浏览 1 评论 0原文

我见过用 C++ 写的,我不知道。我需要一个 C# 函数来返回 .NET CF 中 SDCard 的序列号。有人可以帮我用它写一个函数吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无人问我粥可暖 2024-09-16 03:08:17

首先,我要说的是,如果您购买 SDF(50 美元),您将获得此函数的源代码,然后您可以将其拉入您的项目并进行混淆。

但是,如果您有更多的时间而不是金钱并且想自己实施,我可以为您指出正确的方向。我让您将所有这些部分串在一起,但步骤如下:

首先,您必须像这样打开卷:

IntPtr hFile = NativeMethods.CreateFile(
      string.Format("{0}\\Vol:", driveName), 
      NativeMethods.GENERIC_READ, 0, 0, NativeMethods.OPEN_EXISTING, 0, 0);

这将为您提供驱动器的句柄。然后,您只需调用 IOCTL 即可获取存储 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);
}

这将为您提供 STORAGE_IDENTIFICATION 结构。请注意,该结构实际​​上只是数据头的描述。它包含实际序列号数据的偏移量,该数据是一个 ASCII 字符串。提取它看起来像这样:

int snoffset = BitConverter.ToInt32(data, 12);

string sn = Encoding.ASCII.GetString(data, snoffset, data.Length - snoffset);
return sn.TrimEnd(new char[] { '\0' });

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' });
dawn曙光 2024-09-16 03:08:17

智能设备框架将使您能够执行此操作:

foreach(var info in DriveInfo.GetDrives())
{
   string manufacturer = info.ManufacturerID ?? "[Not available]");
   string serial = info.SerialNumber ?? "[Not available]");
}

请参阅此处。去年我自己也问过同样的问题

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文