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::String::Empty;
LPCWSTR damn=L"\\\\.\\c:";
HANDLE hPhysicalDriveIOCTL = CreateFile(damn, GENERIC_READ |
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0,
NULL);
if(hPhysicalDriveIOCTL != INVALID_HANDLE_VALUE)
{
DWORD dwBytesReturned = 0;
//Get Physical Drive Information
VOLUME_DISK_EXTENTS vdExtents;
ZeroMemory(&vdExtents, sizeof(vdExtents));
if(!DeviceIoControl(hPhysicalDriveIOCTL,
IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, NULL, 0,
&vdExtents, sizeof(vdExtents), &dwBytesReturned, NULL))
{
CloseHandle(hPhysicalDriveIOCTL);
throw gcnew System::ApplicationException("Error volumes that
span multiple disks are not supported");
}
//Get SMART version information
GETVERSIONINPARAMS gvopVersionParams;
ZeroMemory(&gvopVersionParams, sizeof(gvopVersionParams));
if(!DeviceIoControl(hPhysicalDriveIOCTL, SMART_GET_VERSION,
NULL, 0,
&gvopVersionParams, sizeof(gvopVersionParams),
&dwBytesReturned, NULL))
{
CloseHandle(hPhysicalDriveIOCTL);
throw gcnew System::ApplicationException("Error cannot get
SMART version information from device");
}
if(gvopVersionParams.bIDEDeviceMap 0)
{
//Setup SMART request
SENDCMDINPARAMS InParams = {
IDENTIFY_BUFFER_SIZE, { 0, 1, 1, 0, 0,
((vdExtents.Extents[0].DiskNumber & 1) ? 0xB0 : 0xA0),
((gvopVersionParams.bIDEDeviceMap >>
vdExtents.Extents[0].DiskNumber & 0x10) ? ATAPI_ID_CMD : ID_CMD) },
(BYTE)vdExtents.Extents[0].DiskNumber
};
DWORD dwBufSize = sizeof(SENDCMDOUTPARAMS) +
IDENTIFY_BUFFER_SIZE;
PSENDCMDOUTPARAMS pbtIDOutCmd = (PSENDCMDOUTPARAMS) new
BYTE[dwBufSize];
ZeroMemory(pbtIDOutCmd, dwBufSize);
//Get SMART information
if(DeviceIoControl(hPhysicalDriveIOCTL, SMART_RCV_DRIVE_DATA,
&InParams, sizeof(SENDCMDINPARAMS),
pbtIDOutCmd, dwBufSize, &dwBytesReturned, NULL))
{
//Little Endian To Big Endian
USHORT *pIDSector = (USHORT*)pbtIDOutCmd->bBuffer;
for(int nShort = 10; nShort < 21; nShort++)
pIDSector[nShort] = (((pIDSector[nShort] & 0x00FF) << 8) +
((pIDSector[nShort] & 0xFF00) >8));
//Get Drive Serial Number
LPSTR lpszSerialNumber1 = new CHAR[21];
ZeroMemory(lpszSerialNumber1, 21);
RtlCopyMemory(lpszSerialNumber1, &pIDSector[10], 20);
//Remove those horrible spaces caused because of endianess
//and print out the serial
LPSTR lpszSerialNumber2 = lpszSerialNumber1;
while(*lpszSerialNumber2 == ' ') lpszSerialNumber2++;
test= gcnew System::String(lpszSerialNumber2);
delete lpszSerialNumber1;
}
delete pbtIDOutCmd;
}
else
{
CloseHandle(hPhysicalDriveIOCTL);
throw gcnew System::ApplicationException("Unknown error");
}
CloseHandle (hPhysicalDriveIOCTL);
}
return test;
} 2 10790
"Sin Jeong-hun" <ty*******@gmail.comwrote in message
news:39**********************************@e6g2000p rf.googlegroups.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::String::Empty;
LPCWSTR damn=L"\\\\.\\c:";
HANDLE hPhysicalDriveIOCTL = CreateFile(damn, GENERIC_READ |
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0,
NULL);
if(hPhysicalDriveIOCTL != INVALID_HANDLE_VALUE)
{
DWORD dwBytesReturned = 0;
//Get Physical Drive Information
VOLUME_DISK_EXTENTS vdExtents;
ZeroMemory(&vdExtents, sizeof(vdExtents));
if(!DeviceIoControl(hPhysicalDriveIOCTL,
IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, NULL, 0,
&vdExtents, sizeof(vdExtents), &dwBytesReturned, NULL))
{
CloseHandle(hPhysicalDriveIOCTL);
throw gcnew System::ApplicationException("Error volumes that
span multiple disks are not supported");
}
//Get SMART version information
GETVERSIONINPARAMS gvopVersionParams;
ZeroMemory(&gvopVersionParams, sizeof(gvopVersionParams));
if(!DeviceIoControl(hPhysicalDriveIOCTL, SMART_GET_VERSION,
NULL, 0,
&gvopVersionParams, sizeof(gvopVersionParams),
&dwBytesReturned, NULL))
{
CloseHandle(hPhysicalDriveIOCTL);
throw gcnew System::ApplicationException("Error cannot get
SMART version information from device");
}
if(gvopVersionParams.bIDEDeviceMap 0)
{
//Setup SMART request
SENDCMDINPARAMS InParams = {
IDENTIFY_BUFFER_SIZE, { 0, 1, 1, 0, 0,
((vdExtents.Extents[0].DiskNumber & 1) ? 0xB0 : 0xA0),
((gvopVersionParams.bIDEDeviceMap >>
vdExtents.Extents[0].DiskNumber & 0x10) ? ATAPI_ID_CMD : ID_CMD) },
(BYTE)vdExtents.Extents[0].DiskNumber
};
DWORD dwBufSize = sizeof(SENDCMDOUTPARAMS) +
IDENTIFY_BUFFER_SIZE;
PSENDCMDOUTPARAMS pbtIDOutCmd = (PSENDCMDOUTPARAMS) new
BYTE[dwBufSize];
ZeroMemory(pbtIDOutCmd, dwBufSize);
//Get SMART information
if(DeviceIoControl(hPhysicalDriveIOCTL, SMART_RCV_DRIVE_DATA,
&InParams, sizeof(SENDCMDINPARAMS),
pbtIDOutCmd, dwBufSize, &dwBytesReturned, NULL))
{
//Little Endian To Big Endian
USHORT *pIDSector = (USHORT*)pbtIDOutCmd->bBuffer;
for(int nShort = 10; nShort < 21; nShort++)
pIDSector[nShort] = (((pIDSector[nShort] & 0x00FF) << 8) +
((pIDSector[nShort] & 0xFF00) >8));
//Get Drive Serial Number
LPSTR lpszSerialNumber1 = new CHAR[21];
ZeroMemory(lpszSerialNumber1, 21);
RtlCopyMemory(lpszSerialNumber1, &pIDSector[10], 20);
//Remove those horrible spaces caused because of endianess
//and print out the serial
LPSTR lpszSerialNumber2 = lpszSerialNumber1;
while(*lpszSerialNumber2 == ' ') lpszSerialNumber2++;
test= gcnew System::String(lpszSerialNumber2);
delete lpszSerialNumber1;
}
delete pbtIDOutCmd;
}
else
{
CloseHandle(hPhysicalDriveIOCTL);
throw gcnew System::ApplicationException("Unknown error");
}
CloseHandle (hPhysicalDriveIOCTL);
}
return test;
}
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::String::Empty;
LPCWSTR damn=L"\\\\.\\c:";
HANDLE hPhysicalDriveIOCTL = CreateFile(damn, GENERIC_READ |
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0,
NULL);
if(hPhysicalDriveIOCTL != INVALID_HANDLE_VALUE)
{
DWORD dwBytesReturned = 0;
//Get Physical Drive Information
VOLUME_DISK_EXTENTS vdExtents;
ZeroMemory(&vdExtents, sizeof(vdExtents));
if(!DeviceIoControl(hPhysicalDriveIOCTL,
IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, NULL, 0,
&vdExtents, sizeof(vdExtents), &dwBytesReturned, NULL))
{
CloseHandle(hPhysicalDriveIOCTL);
throw gcnew System::ApplicationException("Error volumes that
span multiple disks are not supported");
}
//Get SMART version information
GETVERSIONINPARAMS gvopVersionParams;
ZeroMemory(&gvopVersionParams, sizeof(gvopVersionParams));
if(!DeviceIoControl(hPhysicalDriveIOCTL, SMART_GET_VERSION,
NULL, 0,
&gvopVersionParams, sizeof(gvopVersionParams),
&dwBytesReturned, NULL))
{
CloseHandle(hPhysicalDriveIOCTL);
throw gcnew System::ApplicationException("Error cannot get
SMART version information from device");
}
if(gvopVersionParams.bIDEDeviceMap 0)
{
//Setup SMART request
SENDCMDINPARAMS InParams = {
IDENTIFY_BUFFER_SIZE, { 0, 1, 1, 0, 0,
((vdExtents.Extents[0].DiskNumber & 1) ? 0xB0 : 0xA0),
((gvopVersionParams.bIDEDeviceMap >>
vdExtents.Extents[0].DiskNumber & 0x10) ? ATAPI_ID_CMD : ID_CMD) },
(BYTE)vdExtents.Extents[0].DiskNumber
};
DWORD dwBufSize = sizeof(SENDCMDOUTPARAMS) +
IDENTIFY_BUFFER_SIZE;
PSENDCMDOUTPARAMS pbtIDOutCmd = (PSENDCMDOUTPARAMS) new
BYTE[dwBufSize];
ZeroMemory(pbtIDOutCmd, dwBufSize);
//Get SMART information
if(DeviceIoControl(hPhysicalDriveIOCTL, SMART_RCV_DRIVE_DATA,
&InParams, sizeof(SENDCMDINPARAMS),
pbtIDOutCmd, dwBufSize, &dwBytesReturned, NULL))
{
//Little Endian To Big Endian
USHORT *pIDSector = (USHORT*)pbtIDOutCmd->bBuffer;
for(int nShort = 10; nShort < 21; nShort++)
pIDSector[nShort] = (((pIDSector[nShort] & 0x00FF) << 8) +
((pIDSector[nShort] & 0xFF00) >8));
//Get Drive Serial Number
LPSTR lpszSerialNumber1 = new CHAR[21];
ZeroMemory(lpszSerialNumber1, 21);
RtlCopyMemory(lpszSerialNumber1, &pIDSector[10], 20);
//Remove those horrible spaces caused because of endianess
//and print out the serial
LPSTR lpszSerialNumber2 = lpszSerialNumber1;
while(*lpszSerialNumber2 == ' ') lpszSerialNumber2++;
test= gcnew System::String(lpszSerialNumber2);
delete lpszSerialNumber1;
}
delete pbtIDOutCmd;
}
else
{
CloseHandle(hPhysicalDriveIOCTL);
throw gcnew System::ApplicationException("Unknown error");
}
CloseHandle (hPhysicalDriveIOCTL);
}
return test;
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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
|
by: Evgeny Zoldin |
last post by:
Hi ALL,
how can I get serial number of harddrive using "pure" .NET, without Win API?
Thanx
|
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
|
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)...
|
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...
|
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
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |