Reputation: 11
I want to protect my application. So I read hard drive serial number and compare. The application has good result in administrator user mode but it has bad result in standard user mode.
I wrote my application with C#. But for reading hard drive serial number I used a dll file that I wrote in Delphi.
hDevice := CreateFile( '\\.\PhysicalDrive0:', GENERIC_READ or GENERIC_WRITE ,
FILE_SHARE_READ or FILE_SHARE_WRITE , nil, OPEN_EXISTING, 0, 0 );
I try using NET so I used WMI class Win32_DiskDrive
but this method has bad results in standard user mode too.
private string getserial()
{
string SerialNumber = "";
string dataForSerial = string.Empty;
ManagementObjectSearcher Finder = new ManagementObjectSearcher("Select * from Win32_OperatingSystem");
string Name = "";
foreach (ManagementObject OS in Finder.Get()) Name = OS["Name"].ToString();
// Name = "Microsoft Windows XP Professional|C:\WINDOWS|\Device\Harddisk0\Partition1"
int ind = Name.IndexOf("Harddisk") + 8;
int HardIndex = Convert.ToInt16(Name.Substring(ind, 1));
Finder = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE Index=" + HardIndex);
foreach (ManagementObject HardDisks in Finder.Get())
foreach (ManagementObject HardDisk in HardDisks.GetRelated("Win32_PhysicalMedia"))
SerialNumber = HardDisk["SerialNumber"].ToString();
// SerialNumber = dataForSerial;
return SerialNumber;
}
In standard user mode:
Note: this problem (NO.2) is only in Windows 7.
Upvotes: 0
Views: 1526
Reputation: 6289
Please see this Link. Results are varying depending on Windows version, on whether the code is run as admin or not, and whether the Win32_PhysicalMedia
class is used or the Win32_DiskDrive
class. Seems pretty unreliable, you may have to write your own abstraction layer to handle it yourself, as described in these forum posts.
I tried it myself and found I got two different serial numbers depending on admin vs normal and Win32_PhysicalMedia
vs Win32_DiskDrive
:
VB38bb50ab-0de50c12
and
42563833626230356261302d6564303531632032
Notice that the second string is actually a hex-encoded
and byte-reversed
version of the first string!
Upvotes: 1
Reputation: 1483
Please use the following code when calling CreateFile
to access the physical disk. It works without admin rights and allows one to read the drive's properties:
hDisk := CreateFile ('\\.\PHYSICALDRIVE0', 0, FILE_SHARE_WRITE, NIL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL or FILE_FLAG_NO_BUFFERING, 0);
Please also take note that the name of the drive passed as the first parameter to CreateFile does not include a colon ":" at the end.
Upvotes: 1