PDA

View Full Version : how to get harddisk serial?



cutie.monkey
2nd February 2009, 09:23
hi all,

is there any function in qt that will get the hard disk serial number as well as its mac address? thnks a lot..

wysota
2nd February 2009, 10:53
No, there are no such functions in Qt. Use the native API.

cutie.monkey
3rd February 2009, 02:16
thnks for the response.. can you give me any link or example on how to use the native API?

wysota
3rd February 2009, 02:44
http://msdn.com

cutie.monkey
3rd February 2009, 04:04
ok.. i'l check for it.. thnks..

cutie.monkey
5th February 2009, 04:23
hi all,

i have here a simple code to get the hard disk serial, using a function from msdn.

heres my code:


#include <stdio.h>
#include <windows.h>

DWORD dwVolSerial;
BOOL bIsRetrieved;

int main() {

bIsRetrieved = GetVolumeInformation("C:\\", NULL, NULL, &dwVolSerial, NULL, NULL, NULL, NULL);


if (bIsRetrieved) {
printf("Serial number of drive C is %X\n",dwVolSerial);
} else {
printf("Could not retrieve\n");
}

return 0;
}

when i build my code an error occured..


src\main.cpp:19: error: cannot convert `const char*' to `const WCHAR*' for argument `1' to `BOOL GetVolumeInformationW(const WCHAR*, WCHAR*, DWORD, DWORD*, DWORD*, DWORD*, WCHAR*, DWORD)'

is there anybody here knows how to solve this error?
thnks:confused:

Ginsengelf
5th February 2009, 09:19
Hi, try
L"C:\\" or
TEXT("C:\\") to convert to WCHAR.

Ginsengelf

cutie.monkey
5th February 2009, 10:07
thnks.. its working..
another question, how can I convert DWORD dwVolSerial to string?

jpn
5th February 2009, 10:10
QString::number()

cutie.monkey
5th February 2009, 10:37
DWORD dwVolSerial;
printf("Serial number of drive C is %X\n",dwVolSerial); //returns Serial number of drive C is 5OB43BE

QString::number(dwVolSerial) //returns 1353987262


how can i get the exact return (5OB43BE)?

jpn
5th February 2009, 11:51
Please take a closer look at QString::number() docs. It takes an optional second parameter.

jbiloba
5th February 2009, 11:53
Change the base to 16 (hex)

QString QString::number ( int n, int base = 16 )

default is decimal (10)

cutie.monkey
6th February 2009, 02:05
thnks. its finally working.. ;)

lucian
24th February 2014, 08:12
it's work for me

#include <qt_windows.h>

QString tdrive = "C:\\";
DWORD dwSerialNumber = 0;
bool ret = GetVolumeInformation((WCHAR *)tdrive.utf16(),NULL,NULL,&dwSerialNumber,NULL,NULL,NULL,NULL);
if (ret) {
qDebug()<<"dwSerialNumber " << dwSerialNumber;
}

ChrisW67
24th February 2014, 09:53
Waking up a four years dormant thread to provide an answer that was given a couple of times in the thread already. Some kind of record?

lucian
24th February 2014, 13:23
Waking up a four years dormant thread to provide an answer that was given a couple of times in the thread already. Some kind of record?

if you carefully read the topic, might notice that my solution is slightly different from the one proposed, see header file

wysota
25th February 2014, 11:39
And how does this include file change the solution? You are effectively calling the same function. I don't know what qt_windows.h is but it probably contains a #include <windows.h> line.

mugi
28th April 2014, 15:13
The above code returns the HDD serial number assigned by the OS ! Is there another function to retrieve the physical one assigned by the manufacturer ?
msdn suggests using the Windows Management Instrumentation: http://msdn.microsoft.com/en-us/library/windows/desktop/aa364993(v=vs.85).aspx
Any ideas how to do it without this service ?

Thanks a lot !

PS: I hope it is ok using this old post, I figured it is best than opening a new one with the same topic

ChrisW67
29th April 2014, 03:03
From the GetVolumeInformation() (http://msdn.microsoft.com/en-us/library/windows/desktop/aa364993%28v=vs.85%29.aspx) docs:


This function returns the volume serial number that the operating system assigns when a hard disk is formatted. To programmatically obtain the hard disk's serial number that the manufacturer assigns, use the Windows Management Instrumentation (WMI) Win32_PhysicalMedia property SerialNumber.

Win32_PhysicalMedia class (http://msdn.microsoft.com/en-us/library/aa394346%28v=vs.85%29.aspx)
WMI C++ Application Examples (http://msdn.microsoft.com/en-us/library/aa394558%28v=vs.85%29.aspx)