服务器物理硬盘序列号,查看硬盘物理序列号的程序源代码

程序和源代码参见附件,也可以直接从下面链接页面下载:

http://www.codeproject.com/KB/mcpp/DriveInfoEx.aspx

Introduction

Many people looking for a schema to protect their work need to get some information that is hardware specific like the Mac Address or some hard drive serial number.

Background

If you tried other solutions like like this one, it probably did not work for you because it's using the WMI services. I was able to find a solution that worked reasonably well here. It made low level calls to the disk using commands sent by the DeviceIoControlAPI. The code was not very reusable unless you used native C++. Therefore I brushed it a bit and made it look more Object Oriented. Most importantly, I exposed the drive information through a .NET collection.

Using the Code

Since the collection is written in MC++, I've included some Microsoft DLLs from the redistributable pack in the demo zip. Also it's mandatory to use .NET 2.0 since the collection is generic.

The code is very easy to use from any .NET language, like C# for instance:

minus.gifCollapse|Copy Code

m_list = new DriveListEx();

m_list.Load();

//bind to a a grid view m_dataGridView.DataSource = m_list;

Points of Interest

The information about the internal drives is gathered in DiskInfo::LoadDiskInfo();

DiskInfois a native singleton class that wraps the calls to ReadPhysicalDriveInNTWithAdminRights()and ReadIdeDriveAsScsiDriveInNT(). I ignored the ReadPhysicalDriveInNTWithZeroRights()that did not seem to work anyways.

Both functions will call AddIfNew()if they can retrieve the information.

Internally there is a list that holds the raw information about the drives and that is updated when a new drive information was found.

minus.gifCollapse|Copy Code

BOOL DiskInfo::AddIfNew(USHORT *pIdSector)

{

BOOL bAdd = TRUE;

for(UINT i =0; i< m_list.size();i++)

{

if(memcmp(pIdSector,m_list[i],256 * sizeof(WORD)) == 0)

{

bAdd = false;

break;

}

}

if(bAdd)

{

WORD* diskdata = new WORD[256];

::memcpy(diskdata,pIdSector,256*sizeof(WORD));

m_list.push_back(diskdata);

}

return bAdd;

}

If you are stuck with a non .NET compiler, you could still use the source code from UnmanagedCode.cpp, just uncomment the #define NATIVE_CODEline.

This build is for Windows XP 32 bit systems. If you need it for Vista or 64 bit systems, you should select the right include and lib folders when building and should not use the additional DLLs from the release.zip archive, since they are 32 bit for Windows XP.

History

Version 1.1: Added ReadPhysicalDriveInNTUsingSmartfor reading the HDD info.

P.S. I did not get a chance to test it for Windows 95 and alike.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值