TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: laidbacklarry on May 28, 2011, 11:20:07 AM

Title: Function to get hard drive serial number
Post by: laidbacklarry on May 28, 2011, 11:20:07 AM
Is there an ACAD equivalent for Bricscad (get_diskserialid)?
Title: Re: Function to get hard drive serial number
Post by: Keith™ on May 28, 2011, 11:27:31 AM
I don't think so, but there is the ability to use scripting to get the serial number. There was an example somewhere within these forums. I'll see if I can find it.
Title: Re: Function to get hard drive serial number
Post by: Lee Mac on May 28, 2011, 11:30:20 AM
Perhaps?

Code: [Select]
(defun HDSerial ( drive / fso drv ser )
  (vl-catch-all-apply
    (function
      (lambda nil
        (setq fso (vlax-create-object "Scripting.FileSystemObject")
              drv (vlax-invoke-method fso 'getdrive drive)
              ser (vlax-get-property drv 'serialnumber)
        )
      )
    )
  )
  (if drv (vlax-release-object drv))
  (if fso (vlax-release-object fso))
  ser
)
Title: Re: Function to get hard drive serial number
Post by: Keith™ on May 28, 2011, 11:35:35 AM
Here is the one that worked for me.

http://www.theswamp.org/index.php?topic=29556.msg396282#msg396282
Title: Re: Function to get hard drive serial number
Post by: Lee Mac on May 28, 2011, 11:47:38 AM
Another, but I think this method only works on Vista OS onwards:

Code: [Select]
(defun HDSerial ( / wmi srv drv ser )
  (vl-catch-all-apply
    (function
      (lambda ( )
        (if
          (setq wmi (vlax-create-object "WbemScripting.SWbemLocator")
                srv (vlax-invoke wmi 'connectserver nil nil nil nil nil nil nil nil)
                drv (vlax-invoke srv 'execquery "Select SerialNumber from Win32_DiskDrive")
          )
          (vlax-for item drv
            (vlax-for prop (vlax-get item 'Properties_)
              (if (eq "SERIALNUMBER" (strcase (vlax-get prop 'name)))
                (setq ser (vl-string-trim " " (vlax-get prop 'value)))
              )
            )
          )
        )
      )
    )
  )
  (if drv (vlax-release-object drv))
  (if srv (vlax-release-object srv))
  (if wmi (vlax-release-object wmi))
  ser
)
Title: Re: Function to get hard drive serial number
Post by: laidbacklarry on May 28, 2011, 05:47:51 PM
Keith, Lee Mac - Thanks. I'll start testing these to see what works best. Our user base ranges from XP thru Windows7, so I may have to test for OS platform to decide which to use. But, thanks again.
Title: Re: Function to get hard drive serial number
Post by: Lee Mac on May 28, 2011, 05:55:18 PM
Keith, Lee Mac - Thanks. I'll start testing these to see what works best. Our user base ranges from XP thru Windows7, so I may have to test for OS platform to decide which to use. But, thanks again.

You're welcome :-)

If available to you, another option would be to use DosLib's dos_serialno function (will return the same as the function utilising the FSO method).

Also, bear in mind that the function I posted using WMI will return the serial number allocated by the manufacturer, which is different from the serial assigned to a disk volume as returned by the SerialNumber Property of a Drive Object.
Title: Re: Function to get hard drive serial number
Post by: laidbacklarry on May 28, 2011, 06:07:28 PM
Keith, Lee Mac - Thanks. I'll start testing these to see what works best. Our user base ranges from XP thru Windows7, so I may have to test for OS platform to decide which to use. But, thanks again.

You're welcome :-)

If available to you, another option would be to use DosLib's dos_serialno function.
DosLib is a great program...  we used it from its first release until about 2-1/2 years ago. Our user base stretches from Acad 2002 through 2012 and with the advent of both 32-bit and 64-bit versions of DosLib, I decided to write my own Lisp versions of the 20 or so DosLib functions we use so I could stop revising our startup routine to figure out which version of DosLib to load. And, as you may know, until recently DosLib wasn't available for Bricscad.
Title: Re: Function to get hard drive serial number
Post by: Lee Mac on May 28, 2011, 06:08:40 PM
Edited above  :wink: