473,809 Members | 2,775 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting HDD Serial of C drive.

Below is the code in Managed C++. It was originally written in C, and
posted somewhere around the Internet (I forgot where I copied this
code), and I changed it into Managed C++. This code returnes the
serial string like "WD-WC1234567" of the hard disk where C drive is
on.

Now, I would like to change this code into C#, but there are so many
API's, structures and macroes. I've searched the Internet for
equivalent C# code but I couldn't find a working one. Is there any
existing C# equivalent code, or do I need to stick to this MC++ code?
Thank you.

static String^ GetHddSerial()
{
String^ test=System::St ring::Empty;
LPCWSTR damn=L"\\\\.\\c :";
HANDLE hPhysicalDriveI OCTL = CreateFile(damn , GENERIC_READ |
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRIT E, NULL, OPEN_EXISTING, 0,
NULL);
if(hPhysicalDri veIOCTL != INVALID_HANDLE_ VALUE)
{
DWORD dwBytesReturned = 0;
//Get Physical Drive Information
VOLUME_DISK_EXT ENTS vdExtents;
ZeroMemory(&vdE xtents, sizeof(vdExtent s));
if(!DeviceIoCon trol(hPhysicalD riveIOCTL,
IOCTL_VOLUME_GE T_VOLUME_DISK_E XTENTS, NULL, 0,
&vdExtents, sizeof(vdExtent s), &dwBytesReturne d, NULL))
{
CloseHandle(hPh ysicalDriveIOCT L);
throw gcnew System::Applica tionException(" Error volumes that
span multiple disks are not supported");
}

//Get SMART version information
GETVERSIONINPAR AMS gvopVersionPara ms;
ZeroMemory(&gvo pVersionParams, sizeof(gvopVers ionParams));
if(!DeviceIoCon trol(hPhysicalD riveIOCTL, SMART_GET_VERSI ON,
NULL, 0,
&gvopVersionPar ams, sizeof(gvopVers ionParams),
&dwBytesReturne d, NULL))
{
CloseHandle(hPh ysicalDriveIOCT L);
throw gcnew System::Applica tionException(" Error cannot get
SMART version information from device");
}

if(gvopVersionP arams.bIDEDevic eMap 0)
{
//Setup SMART request
SENDCMDINPARAMS InParams = {
IDENTIFY_BUFFER _SIZE, { 0, 1, 1, 0, 0,
((vdExtents.Ext ents[0].DiskNumber & 1) ? 0xB0 : 0xA0),
((gvopVersionPa rams.bIDEDevice Map >>
vdExtents.Exten ts[0].DiskNumber & 0x10) ? ATAPI_ID_CMD : ID_CMD) },
(BYTE)vdExtents .Extents[0].DiskNumber
};

DWORD dwBufSize = sizeof(SENDCMDO UTPARAMS) +
IDENTIFY_BUFFER _SIZE;
PSENDCMDOUTPARA MS pbtIDOutCmd = (PSENDCMDOUTPAR AMS) new
BYTE[dwBufSize];
ZeroMemory(pbtI DOutCmd, dwBufSize);

//Get SMART information
if(DeviceIoCont rol(hPhysicalDr iveIOCTL, SMART_RCV_DRIVE _DATA,
&InParams, sizeof(SENDCMDI NPARAMS),
pbtIDOutCmd, dwBufSize, &dwBytesReturne d, NULL))
{
//Little Endian To Big Endian
USHORT *pIDSector = (USHORT*)pbtIDO utCmd->bBuffer;
for(int nShort = 10; nShort < 21; nShort++)
pIDSector[nShort] = (((pIDSector[nShort] & 0x00FF) << 8) +
((pIDSector[nShort] & 0xFF00) >8));

//Get Drive Serial Number
LPSTR lpszSerialNumbe r1 = new CHAR[21];
ZeroMemory(lpsz SerialNumber1, 21);
RtlCopyMemory(l pszSerialNumber 1, &pIDSector[10], 20);

//Remove those horrible spaces caused because of endianess
//and print out the serial
LPSTR lpszSerialNumbe r2 = lpszSerialNumbe r1;
while(*lpszSeri alNumber2 == ' ') lpszSerialNumbe r2++;
test= gcnew System::String( lpszSerialNumbe r2);
delete lpszSerialNumbe r1;
}
delete pbtIDOutCmd;
}
else
{
CloseHandle(hPh ysicalDriveIOCT L);
throw gcnew System::Applica tionException(" Unknown error");
}
CloseHandle (hPhysicalDrive IOCTL);
}
return test;
}
Feb 4 '08 #1
2 10839

"Sin Jeong-hun" <ty*******@gmai l.comwrote in message
news:39******** *************** ***********@e6g 2000prf.googleg roups.com...
Below is the code in Managed C++. It was originally written in C, and
posted somewhere around the Internet (I forgot where I copied this
code), and I changed it into Managed C++. This code returnes the
serial string like "WD-WC1234567" of the hard disk where C drive is
on.

Now, I would like to change this code into C#, but there are so many
API's, structures and macroes. I've searched the Internet for
equivalent C# code but I couldn't find a working one. Is there any
existing C# equivalent code, or do I need to stick to this MC++ code?
Thank you.
That's C++/CLI, not Managed Extensions for C++, and you are much better off
using that for Windows API calls than C#. Not only can you use the C
headers intact, avoiding p/invoke declaration bugs, but it's faster as well.
>
static String^ GetHddSerial()
{
String^ test=System::St ring::Empty;
LPCWSTR damn=L"\\\\.\\c :";
HANDLE hPhysicalDriveI OCTL = CreateFile(damn , GENERIC_READ |
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRIT E, NULL, OPEN_EXISTING, 0,
NULL);
if(hPhysicalDri veIOCTL != INVALID_HANDLE_ VALUE)
{
DWORD dwBytesReturned = 0;
//Get Physical Drive Information
VOLUME_DISK_EXT ENTS vdExtents;
ZeroMemory(&vdE xtents, sizeof(vdExtent s));
if(!DeviceIoCon trol(hPhysicalD riveIOCTL,
IOCTL_VOLUME_GE T_VOLUME_DISK_E XTENTS, NULL, 0,
&vdExtents, sizeof(vdExtent s), &dwBytesReturne d, NULL))
{
CloseHandle(hPh ysicalDriveIOCT L);
throw gcnew System::Applica tionException(" Error volumes that
span multiple disks are not supported");
}

//Get SMART version information
GETVERSIONINPAR AMS gvopVersionPara ms;
ZeroMemory(&gvo pVersionParams, sizeof(gvopVers ionParams));
if(!DeviceIoCon trol(hPhysicalD riveIOCTL, SMART_GET_VERSI ON,
NULL, 0,
&gvopVersionPar ams, sizeof(gvopVers ionParams),
&dwBytesReturne d, NULL))
{
CloseHandle(hPh ysicalDriveIOCT L);
throw gcnew System::Applica tionException(" Error cannot get
SMART version information from device");
}

if(gvopVersionP arams.bIDEDevic eMap 0)
{
//Setup SMART request
SENDCMDINPARAMS InParams = {
IDENTIFY_BUFFER _SIZE, { 0, 1, 1, 0, 0,
((vdExtents.Ext ents[0].DiskNumber & 1) ? 0xB0 : 0xA0),
((gvopVersionPa rams.bIDEDevice Map >>
vdExtents.Exten ts[0].DiskNumber & 0x10) ? ATAPI_ID_CMD : ID_CMD) },
(BYTE)vdExtents .Extents[0].DiskNumber
};

DWORD dwBufSize = sizeof(SENDCMDO UTPARAMS) +
IDENTIFY_BUFFER _SIZE;
PSENDCMDOUTPARA MS pbtIDOutCmd = (PSENDCMDOUTPAR AMS) new
BYTE[dwBufSize];
ZeroMemory(pbtI DOutCmd, dwBufSize);

//Get SMART information
if(DeviceIoCont rol(hPhysicalDr iveIOCTL, SMART_RCV_DRIVE _DATA,
&InParams, sizeof(SENDCMDI NPARAMS),
pbtIDOutCmd, dwBufSize, &dwBytesReturne d, NULL))
{
//Little Endian To Big Endian
USHORT *pIDSector = (USHORT*)pbtIDO utCmd->bBuffer;
for(int nShort = 10; nShort < 21; nShort++)
pIDSector[nShort] = (((pIDSector[nShort] & 0x00FF) << 8) +
((pIDSector[nShort] & 0xFF00) >8));

//Get Drive Serial Number
LPSTR lpszSerialNumbe r1 = new CHAR[21];
ZeroMemory(lpsz SerialNumber1, 21);
RtlCopyMemory(l pszSerialNumber 1, &pIDSector[10], 20);

//Remove those horrible spaces caused because of endianess
//and print out the serial
LPSTR lpszSerialNumbe r2 = lpszSerialNumbe r1;
while(*lpszSeri alNumber2 == ' ') lpszSerialNumbe r2++;
test= gcnew System::String( lpszSerialNumbe r2);
delete lpszSerialNumbe r1;
}
delete pbtIDOutCmd;
}
else
{
CloseHandle(hPh ysicalDriveIOCT L);
throw gcnew System::Applica tionException(" Unknown error");
}
CloseHandle (hPhysicalDrive IOCTL);
}
return test;
}

Feb 4 '08 #2
This article is quite old, but it looks like it has what you want (also
CPUId, MACaddress, etc.)
http://www.eggheadcafe.com/articles/20030511.asp
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
MetaFinder: http://www.blogmetafinder.com
"Sin Jeong-hun" wrote:
Below is the code in Managed C++. It was originally written in C, and
posted somewhere around the Internet (I forgot where I copied this
code), and I changed it into Managed C++. This code returnes the
serial string like "WD-WC1234567" of the hard disk where C drive is
on.

Now, I would like to change this code into C#, but there are so many
API's, structures and macroes. I've searched the Internet for
equivalent C# code but I couldn't find a working one. Is there any
existing C# equivalent code, or do I need to stick to this MC++ code?
Thank you.

static String^ GetHddSerial()
{
String^ test=System::St ring::Empty;
LPCWSTR damn=L"\\\\.\\c :";
HANDLE hPhysicalDriveI OCTL = CreateFile(damn , GENERIC_READ |
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRIT E, NULL, OPEN_EXISTING, 0,
NULL);
if(hPhysicalDri veIOCTL != INVALID_HANDLE_ VALUE)
{
DWORD dwBytesReturned = 0;
//Get Physical Drive Information
VOLUME_DISK_EXT ENTS vdExtents;
ZeroMemory(&vdE xtents, sizeof(vdExtent s));
if(!DeviceIoCon trol(hPhysicalD riveIOCTL,
IOCTL_VOLUME_GE T_VOLUME_DISK_E XTENTS, NULL, 0,
&vdExtents, sizeof(vdExtent s), &dwBytesReturne d, NULL))
{
CloseHandle(hPh ysicalDriveIOCT L);
throw gcnew System::Applica tionException(" Error volumes that
span multiple disks are not supported");
}

//Get SMART version information
GETVERSIONINPAR AMS gvopVersionPara ms;
ZeroMemory(&gvo pVersionParams, sizeof(gvopVers ionParams));
if(!DeviceIoCon trol(hPhysicalD riveIOCTL, SMART_GET_VERSI ON,
NULL, 0,
&gvopVersionPar ams, sizeof(gvopVers ionParams),
&dwBytesReturne d, NULL))
{
CloseHandle(hPh ysicalDriveIOCT L);
throw gcnew System::Applica tionException(" Error cannot get
SMART version information from device");
}

if(gvopVersionP arams.bIDEDevic eMap 0)
{
//Setup SMART request
SENDCMDINPARAMS InParams = {
IDENTIFY_BUFFER _SIZE, { 0, 1, 1, 0, 0,
((vdExtents.Ext ents[0].DiskNumber & 1) ? 0xB0 : 0xA0),
((gvopVersionPa rams.bIDEDevice Map >>
vdExtents.Exten ts[0].DiskNumber & 0x10) ? ATAPI_ID_CMD : ID_CMD) },
(BYTE)vdExtents .Extents[0].DiskNumber
};

DWORD dwBufSize = sizeof(SENDCMDO UTPARAMS) +
IDENTIFY_BUFFER _SIZE;
PSENDCMDOUTPARA MS pbtIDOutCmd = (PSENDCMDOUTPAR AMS) new
BYTE[dwBufSize];
ZeroMemory(pbtI DOutCmd, dwBufSize);

//Get SMART information
if(DeviceIoCont rol(hPhysicalDr iveIOCTL, SMART_RCV_DRIVE _DATA,
&InParams, sizeof(SENDCMDI NPARAMS),
pbtIDOutCmd, dwBufSize, &dwBytesReturne d, NULL))
{
//Little Endian To Big Endian
USHORT *pIDSector = (USHORT*)pbtIDO utCmd->bBuffer;
for(int nShort = 10; nShort < 21; nShort++)
pIDSector[nShort] = (((pIDSector[nShort] & 0x00FF) << 8) +
((pIDSector[nShort] & 0xFF00) >8));

//Get Drive Serial Number
LPSTR lpszSerialNumbe r1 = new CHAR[21];
ZeroMemory(lpsz SerialNumber1, 21);
RtlCopyMemory(l pszSerialNumber 1, &pIDSector[10], 20);

//Remove those horrible spaces caused because of endianess
//and print out the serial
LPSTR lpszSerialNumbe r2 = lpszSerialNumbe r1;
while(*lpszSeri alNumber2 == ' ') lpszSerialNumbe r2++;
test= gcnew System::String( lpszSerialNumbe r2);
delete lpszSerialNumbe r1;
}
delete pbtIDOutCmd;
}
else
{
CloseHandle(hPh ysicalDriveIOCT L);
throw gcnew System::Applica tionException(" Unknown error");
}
CloseHandle (hPhysicalDrive IOCTL);
}
return test;
}
Feb 4 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
43081
by: Gavin | last post by:
Hi, I'm a newbie to programming of any kind. I have posted this to other groups in a hope to get a response from anyone. Can any one tell me how to make my VB program read the Bios serial number (or would HDD be better, or both?) and put that info into VB prog so the program won't work on another computer. My program uses an MSAccess table. Much appreciated if you can help! Thanks
15
9697
by: tom | last post by:
Hi, How do I get the serial number of the harddisk in .NET? I want this to be the same number even if the user has reformatted, so I do not want the volume serial number. Thanx, t
3
1692
by: Evgeny Zoldin | last post by:
Hi ALL, how can I get serial number of harddrive using "pure" .NET, without Win API? Thanx
5
2698
by: | last post by:
Hi, Do memory sticks have serial numbers like harddrives? If so how can I get this, I want to uniquely identify a memory stick (removable drive) for authentication. Thanks
14
28204
by: Lauren Wilson | last post by:
Discovered this interesting comment on MSDN: "To programmatically obtain the hard disk's serial number that the manufacturer assigns, use the Windows Management Instrumentation (WMI) Win32_PhysicalMedia (a class) property SerialNumber." I'm sorry to admit it bit I am really undereducated on how to incorporate some of the Windows SDK stuff into VBA apps. Anyone know of some sample code that will allow us to read the C drive hardware...
9
20735
by: Nebojsa4 | last post by:
Hi. First, sorry on my weak English to all. Qusetion: How to read (in VB) Manufacturer serial number of Hard disk drive? Not volume/serial number of C:, D:, etc. partitons. For reading volume/serial number of hard disk C: etc, You can use Microsoft Scripting Runtime (in VB):
7
1734
by: Adele le Roux | last post by:
Hi All, How can I get the hard disk serial number of a remote computer's C:? The drive will NOT be mapped as a network drive. Thanks, Adele
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10379
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7660
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3014
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.