Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBS - How to get HD serial number

Status
Not open for further replies.

Boga03

IS-IT--Management
Oct 15, 2009
1
BE
Hi there,
Here is my vbs code to read HD serial number. The WMI return only a unintelligible byte value. So I've written a conversion function.

----------------------------------------

strComputer = "."

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
str = ""

Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PhysicalMedia")
For Each objItem In colItems
str = objItem.SerialNumber & vbCrlf & vbCrlf
msgbox( "Serial Number : " & ConvertSN(Str))
next


Function ConvertSN(Str)
DIM Hxstr
DIM i
Hxstr = ""
for i=0 to (Len(str)/4)-2
Hxstr = Hxstr & chr("&H" & mid(str,4*i+3,2)) & chr("&H" & mid(str,4*i+1,2))
next
Hxstr = trim(Hxstr)
ConvertSN = Hxstr
end function
------------------------------------------
 
watch out for runtimes with regards the length of Str, option explicit would be nice as would Set colItems = Nothing, Set objWMIService = Nothing etc
 
>would be nice as would Set colItems = Nothing, Set objWMIService = Nothing

But not necessary. At all.
 
strongm, i presume you were a messy teenage and you mum always cleaned up after you, lol.
you also presume that garbage collection works on 100% of objects 100% or time, a few years back i noted a WMI related object which didnt, and being lazy led to a leak.
memory isnt really of great interest me, its still more a question of style, neatness, attention to detail. you arent american by any chance?
 
Set object = Nothing does []not[/b] do garbage collection, contrary to anything you may have read, been told or simply guessed. It reduces a reference count. When, and only when, that count reaches 0 the object is destroyed. And when an object variable goes out of scope that also reduces the reference count. i.e it does exactly the same thiing

So putting

Set object = Nothing

just before the object goes out of scope is redundant, since going out of scope has exactly* the same affect as

Set object = Nothing

You are not cleaning up anything, you are not (necessarily) garbage collecting, you are not somehow being safer with the COM object.

In other words it is meaningless code, serving no purpose.

* Not quite true. If there are some dependencies between objects that results in the order in which objects are dereferenced becoming important (normally the result of a bug; some of the objects in older ADO libraries suffered from this), we cannot tell the scope finalizer which order to do this - and in those highly unusual circumstances you might need to explicitly set the objects to Nothing in the right order.
 
i do tend to waffle so i am sure if you read what i say for long enough i will say something which is <bold></bold> in wrong in your mind and would give you ammunition to flame me.

i appreaciate your last post as my knowledge of Set = Nothing is now better, thank you for that.

however, one must take you post first post in its rightful context. i see it as having no purpose other than to provoke me. i say this for a number of reasons...

Option Explicit can be seen as serving no purpose in vbscript, so does the use of Dim, i am sure i can think of other programming techniques or letter which one can write in a script, 'comments, these could be included in the list, white space (i have to admit i always comment white space now. oh, you could have functions which dont actually return anyway, setting variables = "" before their use, blaa blaa blaa. Lots of things can be seen as serving no legitimate purpose (me writing this post for instance), that does not mean that they are not recommended, best practice and should be followed.

I set my objects to nothing when i am finished with them, i do so in the reverse order in which they were created, i comment my code (to the amusement of my chums), i set my string variables = "", my num = 0, i defensive program to the extreme, i Dim my variables, i always use option explicit, i use am consistant with case etc etc and I am a better programmer for it.

Your statement that setting objects to nothing as being " not necessary. At all. " only shows that you are a messy programmer.

you take rock logic to dizzy new heights.
 
>Option Explicit can be seen as serving no purpose in vbscript

It has a purpose, just not quite as much purpose as in VB or VBA. And if you use Option Explicit you have to use Dim, it isn't a question of style.

>shows that you are a messy programmer

A rather large and fanciful mental leap on your behalf. It simply means I don't adhere to your particular style guidelines. Nor your apparant equating of style with correctness.

And given the choice between a program that works but which might be clumsily laid, and one that does not but is aesthetically pleasing I know which I'd choose.




 
only in strongm's world does aesthetically pleasing code = non functioning

large and fanciful? nope, that would be your ego
 
mrmovie: If it was worth mentioning that "set obj = Nothing" is "nice", then it was ALSO worth mentioning what "set obj = Nothing" actually does (and doesn't) do.

strongm's posts were accurate, polite, and professional. Your replies were bordering on the childish (and I'm being kind).

Boga03: see what you started??? Just kidding, thanks for the contribution!
 
>only in strongm's world does aesthetically pleasing code = non functioning

That's not what I said.

>nope, that would be your ego

Nor at any time have I personally attacked you, as you have now done to me on three occasions is just this one thread. I have merely disagreed with your repeated assertion and inference that people's code should explicitly set objects to nothing. This is a canard and a myth. And one that keeps propagating because people keep repeating it.

I do not disagree with your (or anyone's) right to adhere to a particular programming style of their own choosing. As a result I wouldn't quibble if your advice was something along the lines of "You might want to use Set object = Nothing because your code would look nicer" or "You've explicitly created an object in your function, you might want to consider balancing that with a Set object = Nothing to make things nice and symmetric
 
Talk on set obj=nothing is trite. It does not need to wait for 10+ years to repeat. To the limit, you should even erase an array if necessity calls. In particular in the web app environment. Same option explicit. All sorts of rubbish and pretention. It is just like saying document.getElementById() is not in the standard. It is boring. It sounds deep, it is superficial. You should know under what circumstances certain lines have an impact and in what sense. I can only suppose a capacity-proven members would know the often repeated discourse even by heart. If certain construction is used, it is because of it being a choice made, not by caprice.
 
a previous post by strongm
strongm (MIS)
The following example assumes that you have a reference set to Edanmo's OLE Interfaces and Functions library. As with the previous example, you need a form with a picturebox and a command button:

Public Function LoadImage( _
ImageBytes() As Byte) As StdPicture
Dim oPersist As IPersistStream
Dim oStream As IStream
Dim lSize As Long

...blaa blaa


' Release the streamobject
Set oStream = Nothing

End Function


i said 'nice to' in my first post on the subject Set = Nothing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top