Solved

VB code "Get HDD serial"

Posted on 2004-04-22
24
4,033 Views
Last Modified: 2011-07-12
Hi Ark and/or other experts,
 
two months ago I asked a question "Unique Identification number"
(http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20874711.html)
to which I received many answers. Among them I really liked Ark´s suggestion  -  code "Get HDD serial (REAL vendor serial)"
http://www.freevbcode.com/ShowCode.Asp?ID=3380
 
This program is really nice. But I have one overwhelming problem - I´m absolut beginner and unfortunately I´m not able to use parts of the code to fulfil my needs (see below). Would Ark or someone else be so king and could make for me a modified code which:
 
1) retrieves only following informations of PC-drives - especially IDE drives (Ark´s suggested code was only for IDE) but if it would recognize SCSI and SATA drives, it would be much better
    - model
    - serial number
 
2) inserts these information (about all drives) to some *.xls file (e.g. PCdrives.xls) which would be located in the same directory.
 
I would appreciate some really simple code - just one form with only two buttons - first to start retrieving informations and to write it to PCdrives.xls which hould be located in the same directory; second to end the program.
 
I hope you will help me.
 
Best regards

Miua
miua@centrum.cz
0
Comment
Question by:miua
  • 10
  • 10
  • 3
  • +1
24 Comments
 
LVL 2

Expert Comment

by:pitmanromford
ID: 10891362
Unfortunatly I dont know of any simple code, it should however be able to be applied fairly simply, see what the code on first comment on question:
http://www.experts-exchange.com/Programming/Q_20698469.html
does for you, hopefully it'll return the serial yo want.

If this works, you'll need to add references to microsoft excel,
Tools/References/Microsoft Excel #.# Object Librabry
and then play about with the code there (I dont have vb on this machine, so I or another expert will be able to help you out later :)

you'll be looking at something like
set xlswrk = new Excel.Workbook
and then using xlswrk to refer to cells.value from there

Hope this Helps
Lee
0
 
LVL 2

Expert Comment

by:pitmanromford
ID: 10906817
Possibly a simpler solution - If it returns the serial you're looking for is by using the File System Object.  First add references to Microsoft scripting Runtime and then paste in the following code: (Source: http://www.sloppycode.net/fso/?m=88)

Set fso = CreateObject("Scripting.FileSystemObject")
Set driveObject = fso.GetDrive("C")
MsgBox driveObject.serialnumber
Set driveObject = Nothing
Set fso = Nothing
0
 

Author Comment

by:miua
ID: 10921551
Thanks guys for comments, but as I wrote I´m an absolut beginner - I really need help in the way I wrote.

Ark, if you are reading this question, could you please write me (miua@centrum.cz) if you are able to help me... (I´m increasing point...)

Thanks Miua
0
 
LVL 2

Expert Comment

by:pitmanromford
ID: 10926827
Ok, we'll work with my second comment:

I'm assuming you know how to start a project and create buttons, assign properties etc...  If not then i'll be happy to explain it to you.

First we'll see if this serial number code works for you, and then we'll move onto placing that serial in an excel worksheet.

Keep things simple to start off with and return the serial number of just one drive:

>Create a command button and name it: cmdSerial, you can set the caption to whatever you like
>Right click on the form in the project explorer and select View Code
>Now type into the code window:

Private Sub cmdSerial_Click()
Set fso = CreateObject("Scripting.FileSystemObject")
Set driveObject = fso.GetDrive("C")
MsgBox driveObject.serialnumber
Set driveObject = Nothing
Set fso = Nothing
End Sub

>Go back to viewing the form (right click in project explorer | View Object)
>Select the form (not a control on it) and hit the play looking button ">" to test the code
>Click on the button and (fingers crossed) the serial number for that drive will pop up

Hope this works
Lee
0
 

Author Comment

by:miua
ID: 10929439
Thanks Lee,

I know how to start a project, create buttons and some other simplest actions.
Even I´m able to understand some simple codes. But I´m not able to simplify difficult codes.

What your suggestion concerns, I´m afraid your code would provide me only with serial number assigned to the drive by operating system - but this serial changes whenever the drive is formatted. I need a code that reads the real serial number provided by manufacturer of the drive that doesn´t change - please go through following site http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20874711.html  to see differences.

In that question Ark showed me a code (application - http://www.freevbcode.com/ShowCode.Asp?ID=3380 ) that after a small overprogramming could (partially) fulfill my needs - but the code is too difficult for me - I need to use only part of it and to think out the way how to write retrieved data to MS Excel file.

Yould you help me in this way?

Bye miua
0
 
LVL 2

Expert Comment

by:pitmanromford
ID: 10938268
I'm very new to API calls and therefore cannot probably improve on Ark's code, I could give a shot at removing unnecessary statements and getting it to work.  (Writing the data to an excel file shouldnt be a problem :)

I supose the question is, is arks code adaquate or should we wait for another expert to enter this question?  (so i'm not running round in circles).

Lee
0
 

Author Comment

by:miua
ID: 10958162
Hi Lee,

I think if another expert would like to enter this question, he/she would have already done it. So if you are able to simplify ark´s code ( http://www.freevbcode.com/ShowCode.Asp?ID=3380 ) and to enhance it with writing retrieved data to excel file, I´ll be pleased to give you the points.

Could you write me an email (miua@centrum.cz), I would send you excel file (example with required information) so that you can fulfill my needs..

Thanks a lot

miua

0
 
LVL 2

Expert Comment

by:pitmanromford
ID: 11029398
Are you writing the program in VB, or in VBA within the excel file you want to put the data into?

You can send the excel file to A554551N@yahoo.com (I have a max of 4mb of space on there)

I have tested arks code and it appears to work
It retreives the Hard Drive's serial number and the Vendor

I look forward to receiving your e-mail
Lee
0
 
LVL 2

Expert Comment

by:pitmanromford
ID: 11100900
Keeping EE-cleanup volunteers from closing this question

A solution was created, but for the wrong question, I used the CPU API calls, not the hard drive API calls.  (Explaining why only one "Hard Drive" was found :=) - entirely my mistake.

I'm now working with the Hard Drive API calls.

Lee
0
 

Author Comment

by:miua
ID: 11100974
Yes I confirm :-)))

miua
0
 
LVL 2

Expert Comment

by:pitmanromford
ID: 11295999
Many many apologies for taking so long to respond.  I've been revising for and doing exams :(  Got a break till friday.

I'm looking at your problem now to see if i can shed any light on it.  I hope it's not too late!

Lee
0
 

Author Comment

by:miua
ID: 11296704
Hi Lee!

It´s not too late, keep working on the problem...

Miua
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 27

Expert Comment

by:Ark
ID: 11356602
Hi
Soryy, I missed this question. I already have code for SCSI drive SN recognition, but it only works with NT system. If you're using w2k/XP, much more easy way is to use WMI object, which is included into windows pakage for these platforms (BTW, WMI object is free redistributable from MS for early versions).

   Dim WMI As Object
   Dim wmiCollection As Object
   Dim wmiMember As Object
   Dim oTemp As Object
   Dim sOut As String, sTemp As String
   Dim i As Integer
   Set WMI = GetObject("winmgmts:\\")
   Set wmiCollection = WMI.InstancesOf("Win32_DiskDrive")

   For Each wmiMember In wmiCollection
       sOut = "Description: " & CheckValue(wmiMember.Description)
       sOut = sOut & vbCrLf & "DeviceID: " & CheckValue(wmiMember.DeviceID)
       sOut = sOut & vbCrLf & "Install date: " & CheckValue(wmiMember.InstallDate)
       sOut = sOut & vbCrLf & "Model: " & CheckValue(wmiMember.Model)
       sOut = sOut & vbCrLf & "Signature: 0x" & Hex(wmiMember.Signature)
       Set oTemp = WMI.Get("Win32_PhysicalMedia.Tag='\\.\PHYSICALDRIVE0'")
       sOut = sOut & vbCrLf & "Serial number: " & CheckValue(oTemp.SerialNumber)
       MsgBox sOut, vbInformation, wmiMember.Caption & " info"
   Next

Regards
Ark
0
 
LVL 2

Expert Comment

by:pitmanromford
ID: 11357105
Thanks Ark, i've been totally caught up in exams (got one in 2 hours, how fun :)
0
 

Author Comment

by:miua
ID: 11357806
Hi Ark and pitmanromford,

thanks a lot for your comments... As I several times wrote, I´m a total beginner - I´m not a VB programmer and probably never will be. I can do only some simple VBA codes within MS Excel. You may be surprised why I need that slightly difficult code? The answer is that I want to protect my "*.xls application (some huge calculations)" from copying to another computer.
Therefore I would highly appreciate if you could send me the *.xls file to my e-mail address miua@centrum.cz
The *.xls file should contain a VBA code which retrieves the required parametres - model, type (IDE, SCSI, SATA) and serial number of the HDD - ans writes them to specified ranges (I can do only small modifications)

Can I look forward to the e-mail ??? I hope so...
(I intend to increase points and split them between you both)

Thanks a lot, best regards
miua
0
 
LVL 2

Expert Comment

by:pitmanromford
ID: 11359719
Hi miua, could you send me the xls file you're working on? - you sent it before but I think I left it on my school computer...

E-mail address is in my profile
0
 
LVL 27

Accepted Solution

by:
Ark earned 200 total points
ID: 11386941
'=======ThisBook code========
Private Sub Workbook_Open()
   Dim WMI As Object
   Dim wmiCollection As Object
   Dim wmiMember As Object
   Dim oTemp As Object
   Dim i As Integer, n As Integer, count As Integer
   Dim vName As Variant, vValue As Variant
   
   Set WMI = GetObject("winmgmts:\\")
   Set wmiCollection = WMI.InstancesOf("Win32_DiskDrive")
   For Each wmiMember In wmiCollection
       vName = Array("HDD" & CStr(n), "Description :", "DeviceID: ", "Install date :", "Model: ", "Signature: ", "Serial number")
       Set oTemp = WMI.Get("Win32_PhysicalMedia.Tag='" & wmiMember.DeviceID & "'")
       vValue = Array(wmiMember.Caption, CheckValue(wmiMember.Description), CheckValue(wmiMember.DeviceID), _
                      CheckValue(wmiMember.InstallDate), CheckValue(wmiMember.Model), _
                      Hex(wmiMember.Signature), CheckValue(oTemp.SerialNumber))
       For i = 1 To UBound(vName) + 1
          Worksheets(1).Cells(i + count, 1).Value = vName(i - 1)
          Worksheets(1).Cells(i + count, 2).Value = vValue(i - 1)
       Next i
       n = n + 1
       count = count + i + 1
   Next
End Sub

Private Function CheckValue(ByVal s As Variant) As String
   If VarType(s) = vbNull Then s = ""
   s = Trim(s)
   If s = "" Then CheckValue = "Not available" Else CheckValue = s
End Function

0
 

Author Comment

by:miua
ID: 11387389
Ark,

I opened new Excel file, inserted your code to "ThisWorkbook" and saved the file. When I opened it again, it gave me following Runtime error: -2147217392 (80041010)´.
I chose debug and I saw the code stopped at this line:
Set oTemp = WMI.Get("Win32_PhysicalMedia.Tag='" & wmiMember.DeviceID & "'")

Where can be the problem?? Thank you for your patience and help.
miua
0
 
LVL 2

Assisted Solution

by:pitmanromford
pitmanromford earned 200 total points
ID: 11388024
"Hex(wmiMember.Signature)" doesnt seem to work with, either, his version of excel, or his harddrive - Returns runtime error 438.

However both my code (different way of placing the values in excel) and your code Ark works with my hard drives - Excel 2002.
0
 
LVL 27

Expert Comment

by:Ark
ID: 11393855
Hello
WMI object shipped with win2k/XP. Which OS do you use?
0
 

Author Comment

by:miua
ID: 11396144
Hi Ark,
I tried your code on computer with Windows 2000 and Office 2000.
I assume you are really smart programmer and you will find and solve the error. But I would like to ask you if there is a chance to retrieve HDD information (BTW the most important are ATA HDDs, then SATA and the least important is SCSI) in nearly all Windows versions (Win98 and above).
Thanks a lot
miua
0
 

Author Comment

by:miua
ID: 11501534
Hi guys,
I´m waiting :-)))
miua
0
 
LVL 22

Expert Comment

by:DarkoLord
ID: 11977254
No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
I will leave the following recommendation for this question in the Cleanup topic area:
Delete/Refund

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

DarkoLord
EE Cleanup Volunteer
0
 

Author Comment

by:miua
ID: 11984111
I´m afraid they forgot about me... They tried to help me but at one moment they stopped :(
I gave them points even if the problem wasn´t solved completely..
Miua
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now