PDA

View Full Version : Harddrive Id



sujittalukde
06-28-2007, 01:25 AM
Can a macro be provided to get hard drive id to get a message "Your computer id is 1235645698". Is it posiible?

RichardSchollar
06-28-2007, 02:03 AM
Hi

Here's one way using an API function - but note it returns the volume serial number of the hard-drive (and this is generated each time the drive is formatted, so could change in the future). It needs to be copied into a standard module, and can then be called from your code:



Private Declare Function GetVolumeInformation Lib "kernel32" Alias _
"GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal _
lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Long, _
lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, _
lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, ByVal _
nFileSystemNameSize As Long) As Long
Public Function DriveSerialNo(myDrive As String)
'Adapted from http://www.vba-programmer.com/Snippets/Code_VB/Volume_Info_from_Disk_Drive.html
Dim drvserialno As Long
Dim mydrvlabel As String * 256
Dim myfilesys As String * 256
Dim i As Long
Dim j As Long
Dim x As Long

myDrive = Left$(myDrive, 1) & ":\"

x = GetVolumeInformation(myDrive, mydrvlabel, 256, drvserialno, i, j, _
myfilesys, 256)
DriveSerialNo = Hex(drvserialno)
End Function

Use in your code like:


Msgbox "My hard drive serial number is " & DriveSerialNo("C")

Richard

johnske
06-28-2007, 02:21 AM
MsgBox CreateObject("Scripting.FileSystemObject").GetDrive("C:\").SerialNumber

RichardSchollar
06-28-2007, 02:28 AM
Now that looks an awful lot simpler :-)

I found some code to get the actual manufacturer's ID (at least I am presuming that's what it's returning) that seems fairly straightforward - but note I presume it's very OS specific (it works on Windows XP):


Function ManufacturesID()
'adapted from http://www.vbcode.com/asp/showsn.asp?theID=10879
'original code by:
'Karthikeyan
'Homepage: http://www.geocities.com/marskarthik
Dim WMIService As Object, Items As Object, SubItems As Object, temp
On Error Resume Next
Set WMIService = GetObject("winmgmts:\\" & "." & "\root\cimv2")
Set Items = WMIService.ExecQuery("Select * from Win32_PhysicalMedia", , 48)
For Each SubItems In Items
temp = SubItems.SerialNumber
If LenB(temp) Then Exit For
Next
ManufacturesID = temp
End Function

Richard

sujittalukde
06-28-2007, 03:15 AM
Thanks Richard, Johnske
Dear Richard,
I have pated your second code into a std module but not finding anyway to run the same.
How should I use the same?

RichardSchollar
06-28-2007, 03:51 AM
Hi

You would use it like:


Sub test()
Msgbox ManufacturesID()
End Sub

Richard

sujittalukde
06-28-2007, 04:03 AM
Thanks Richard, both of the codes are working now. But one thing-
I have an excel file, VBA is protected. It shows the message "Your computer ID is 149******41" (asterisk are in place of numbers actually shown.) But none of these codes are showing this ID What is this then?

Ebrow
06-28-2007, 04:41 AM
you must be looking for your computer name then:


MsgBox CreateObject("WScript.Network").ComputerName

sujittalukde
06-28-2007, 04:50 AM
Thanks for the reply, your code is giving computer name but not the ID I am looking for.

I am attaching the file wherein the ID message is showing. Please see and tell what is this ID?

johnske
06-28-2007, 05:20 AM
Note: The difference between the serial numbers shown in post #2 and post #3 is that post 2 uses a hexadecimal return, to get post 3 to give the same number use...

MsgBox Hex(CreateObject("Scripting.FileSystemObject").GetDrive("C").serialnumber)

sujittalukde
06-28-2007, 05:33 AM
But your code is also not giving me the ID no. as shows by the file attached by me i

johnske
06-28-2007, 05:54 AM
You wanted the harddrive ID, which is its serial number (Posts 2,3 and 10).

I had a look at the attachment (twice now) and can see nothing like a computerID or serial number in there - what number are you referring to?

sujittalukde
06-28-2007, 06:03 AM
Sorry I forget to tell the procedure to get the same. After opening the file (enabling macros), you can see a new menu at the menu bar as "Tax challan" then click on first sub menu, OK & then you will be shown a Computer ID.

RichardSchollar
06-28-2007, 06:10 AM
It's the DOS volume number on my computer (in decimal) ie exactly what Johnske's code returned (my first code too in a much more complicated manner ;) and in hex of course). If this isn't the number you're getting, then maybe it's because you have two harddrives on your computer? Or a partitioned harddrive?

sujittalukde
06-28-2007, 06:14 AM
Yeah, I dont have two hard drives But my hard disk is partitioned to two parts C: & D:. But I am running the above codes and the attached file on the same computer. then why the number in the attached file is not coming while running the codes?

RichardSchollar
06-28-2007, 06:45 AM
You you are running the following codes and neither returns the number you get when you run the menu item in the sheet you posted?


MsgBox CreateObject("Scripting.FileSystemObject").GetDrive("C:\").SerialNumber
MsgBox CreateObject("Scripting.FileSystemObject").GetDrive("D:\").SerialNumber

?

sujittalukde
06-28-2007, 10:01 PM
Thanks Richard, now I am getting the result. Please tell me what is this number ie its nomenclature!

RichardSchollar
06-29-2007, 12:29 AM
Well, as far as I know it is simply a number generated when the harddrive is formatted. I am afraid I can't tell you what determines what the number will be (potentially format type (ie FAT32/NTFS), time/date of formatting, formatted size, OS doing the formatting could all come into play). I don't think the resulting number means anything either - it is just an ID (I could be wrong here though). One thing to be wary of is that if you were to purchase that program, I presume the vendor of the program ties the code behind the workbook to your hard-drive serial number - if you ever re-formatted the drive, the serial number would change, and therefore you wouln't be able to run the program. Also, were you to swap the hard-drive out for another, you wouldn't be able to run the program. You wouldn't be able to run the program on a second computer in your organisation either.

These are things worth bearing in mind.

Hope this helps!

Richard

sujittalukde
06-29-2007, 12:37 AM
Glad to receive the reply and Thanks for the great hard work.

Ivan F Moala
06-29-2007, 04:45 PM
If you are considering using this for Security then you are better off using WMI to get the Hard Drive Serial Number given by the Manufacturer.

sujittalukde
06-30-2007, 01:59 AM
How can I use the same ie WMI Would you throw some light on it?

johnske
06-30-2007, 03:21 AM
Post #4 is WMI