Jump to content

How to get serial number of hard drive by lisp?


nataca

Recommended Posts

Please try,

(vl-load-com)
(vlax-dump-Object (vlax-invoke (vlax-create-object "Scripting.FileSystemObject") 'getdrive "c:")t)

Link to comment
Share on other sites

;;;                                                  				*
;;; PART OF 'ASMILIB' LIBRUARY  						*
;;; Created: 07.10.2007         						*
;;; Last modyfied: 07.10.2007   						*
;;; © Alexanders Smirnovs (ASMI)						*
;;;                                                  				*


;;; *********************************
;;; **** PC Hardware Functions ******
;;; *********************************


;;;                                                  				*
;;; Retrieves Hard Drive serial number                         		        *
;;;                                                  				*
;;; Arguments:                                       				*
;;; 		Path - Path of Hard Drive, for example "C:" (string)		*
;;;                                                  				*
;;; Output:                                          				*
;;;		Hard Drive serial number (integer) or NIL in case of error. 	*
;;;                                                  				*


(defun #Asmi_Get_Drive_Serial(Path / fsObj hSn abPth cDrv)
 (vl-load-com)
 (if
   (and
     (setq fsObj(vlax-create-object "Scripting.FileSystemObject"))
     (not
(vl-catch-all-error-p
  (setq abPth(vl-catch-all-apply 'vlax-invoke-method
	       (list fsObj 'GetAbsolutePathName Path))
	       ); end setq
	   ); end vl-catch-all-error-p
	); end not
  ); end and
   (progn
     (setq cDrv(vlax-invoke-method fsObj 'GetDrive
       (vlax-invoke-method fsObj 'GetDriveName abPth
       ); end vlax-invoke-method
     );end vlax-invoke-method
    ); end setq
    (if
      (vl-catch-all-error-p
  (setq hSn(vl-catch-all-apply 'vlax-get-property
    (list cDrv 'SerialNumber))))
    (progn
      (vlax-release-object cDrv)
      (setq hSn nil)
    ); end progn
      ); end if
   (vlax-release-object fsObj)
   ); end progn
  ); end if
 hSn
 ); end of #Asmi_Get_Drive_Serial

  • Thanks 1
Link to comment
Share on other sites

  • 9 years later...
Great !!! thanks. Can I get serial number of Mainboad, VGA, ...?

 

(defun Get_BaseBoardSerialNumber (/ LocatorObj ServiceObj ObjectSetObj SerialNumber)
 (setq LocatorObj
        (vlax-create-object "WbemScripting.SWbemLocator")
 )
 (setq ServiceObj
        (vlax-invoke
          LocatorObj 'ConnectServer nil nil nil nil nil nil nil nil)
 )
 (setq ObjectSetObj
        (vlax-invoke
          ServiceObj
          'ExecQuery
          "Select * from Win32_BaseBoard"
        )
 )
 (vlax-for Obj ObjectSetObj
   (setq SerialNumber
          (vlax-get Obj 'SerialNumber)
   )
 )
 (foreach Obj
              (list LocatorObj ServiceObj ObjectSetObj)
   (and Obj (vlax-release-object Obj))
 )
 SerialNumber
)

  • Thanks 1
Link to comment
Share on other sites

(defun Get_ProcessorId (/               LocatorObj      SecurityObj
                       SecurityObj     ObjectSetObj    Processor_Id
                      )
 (setq LocatorObj
        (vlax-create-object "WbemScripting.SWbemLocator")
 )
 (setq ServiceObj
        (vlax-invoke
          LocatorObj 'ConnectServer nil nil nil nil nil nil nil nil)
 )
 (setq ObjectSetObj
        (vlax-invoke
          ServiceObj
          'ExecQuery
          "Select * from Win32_Processor"
        )
 )
 (vlax-for Obj ObjectSetObj
   (setq Processor_Id
          (vlax-get Obj 'ProcessorId)
   )
 )
 (foreach Obj
              (list LocatorObj ServiceObj SecurityObj ObjectSetObj)
   (and Obj (vlax-release-object Obj))
 )
 Processor_Id
)

  • Thanks 1
Link to comment
Share on other sites

(defun Get_UUID (/ LocatorObj ServiceObj ObjectSetObj UUID)
 (setq LocatorObj
        (vlax-create-object "WbemScripting.SWbemLocator")
 )
 (setq ServiceObj
        (vlax-invoke
          LocatorObj 'ConnectServer nil nil nil nil nil nil nil nil)
 )
 (setq ObjectSetObj
        (vlax-invoke
          ServiceObj
          'ExecQuery
          "SELECT UUID FROM Win32_ComputerSystemProduct"
        )
 )
 (vlax-for Obj ObjectSetObj
   (setq UUID
          (vlax-get Obj 'UUID)
   )
 )
 (foreach Obj
              (list LocatorObj ServiceObj ObjectSetObj)
   (and Obj (vlax-release-object Obj))
 )
 UUID
)

  • Thanks 1
Link to comment
Share on other sites

(defun c:SerialInfo_ BIOS(/ WMI meth1 meth2 serial)
(vl-load-com)
(cond ((and (setq WMI (vlax-create-object "WbemScripting.SWbemLocator"))
(setq meth1 (vlax-invoke WMI 'ConnectServer nil nil nil nil nil nil nil nil))
(setq meth2 (vlax-invoke meth1 'ExecQuery (strcat "Select * from Win32_" "BIOS")))
(vlax-for itm (vlax-get (vlax-invoke meth2 'ItemIndex 0) 'Properties_)
(if (eq (vlax-get itm 'name) "SerialNumber")
(setq serial (vlax-get itm 'value)))))))
(mapcar 'vlax-release-object (list meth1 meth2 wmi))
serial)

  • Thanks 1
Link to comment
Share on other sites

(defun get_macaddress  (/ Locator Server Query ret)
 (if (and (setq Locator (vlax-create-object "WbemScripting.SWbemLocator"))
          (setq Server (vlax-invoke Locator 'ConnectServer "." "root\\cimv2"))
          (setq Query (vlax-invoke Server 'ExecQuery "select * from Win32_NetworkAdapterConfiguration where IPEnabled = True")))
   (vlax-for item Query (setq ret (vlax-get item 'MacAddress))))
 (foreach obj '(Locator Server Query)
   (vl-catch-all-apply 'vlax-release-object (list obj)))
 ret)

  • Thanks 1
Link to comment
Share on other sites

Nice one dilan! Now I'm realising that SQL can be used in lisp.. :cool:

And a little remark:

use

(foreach obj (list Locator Server Query) ...

instead of

(foreach obj '(Locator Server Query) ...

 

to release the actual objects and not the symbols that are holding the variables for the objects.

Link to comment
Share on other sites

Nice one dilan! Now I'm realising that SQL can be used in lisp.. :cool:

And a little remark:

use

(foreach obj (list Locator Server Query) ...

instead of

(foreach obj '(Locator Server Query) ...

 

to release the actual objects and not the symbols that are holding the variables for the objects.

 

 

good catch , missed that one in the last function.

Link to comment
Share on other sites

  • 3 years later...
(defun c:SYSINFO ( / getiplist iplist ipv6list  driveindex drivetxt noofdrive drivesr)

;https://www.theswamp.org/index.php?topic=42276.0
(defun getip ( / WMI CSERV EXQ gip)
 (vl-load-com)
  (setq WMI (vlax-create-object "WbemScripting.SWbemLocator"))
  (setq CSERV (VLAX-INVOKE WMI 'ConnectServer "." "\\root\\cimv2" nil nil nil nil nil nil))
  (setq EXQ (vlax-invoke CSERV 'ExecQuery "Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = true")) 
  (vlax-for item EXQ
  (setq gip (vlax-get item 'IPAddress))
  )   
(vlax-release-object wmi)
(vlax-release-object CSERV)
(vlax-release-object EXQ)
gip
)

;https://www.cadtutor.net/forum/topic/9848-how-to-get-serial-number-of-hard-drive-by-lisp/?do=findComment&comment=540967
(defun Get_BaseBoardSerialNumber (/ LocatorObj ServiceObj ObjectSetObj SerialNumber)
 (setq LocatorObj
        (vlax-create-object "WbemScripting.SWbemLocator")
 )
 (setq ServiceObj
        (vlax-invoke
          LocatorObj 'ConnectServer nil nil nil nil nil nil nil nil)
 )
 (setq ObjectSetObj
        (vlax-invoke
          ServiceObj
          'ExecQuery
          "Select * from Win32_BaseBoard"
        )
 )
 (vlax-for Obj ObjectSetObj
   (setq SerialNumber
          (vlax-get Obj 'SerialNumber)
   )
 )
 (foreach Obj
              (list LocatorObj ServiceObj ObjectSetObj)
   (and Obj (vlax-release-object Obj))
 )
 SerialNumber
)


;https://www.cadtutor.net/forum/topic/9848-how-to-get-serial-number-of-hard-drive-by-lisp/?do=findComment&comment=540992
(defun Get_ProcessorId (/               LocatorObj      SecurityObj
                       SecurityObj     ObjectSetObj    Processor_Id
                      )
 (setq LocatorObj
        (vlax-create-object "WbemScripting.SWbemLocator")
 )
 (setq ServiceObj
        (vlax-invoke
          LocatorObj 'ConnectServer nil nil nil nil nil nil nil nil)
 )
 (setq ObjectSetObj
        (vlax-invoke
          ServiceObj
          'ExecQuery
          "Select * from Win32_Processor"
        )
 )
 (vlax-for Obj ObjectSetObj
   (setq Processor_Id
          (vlax-get Obj 'ProcessorId)
   )
 )
 (foreach Obj
              (list LocatorObj ServiceObj SecurityObj ObjectSetObj)
   (and Obj (vlax-release-object Obj))
 )
 Processor_Id
)




;https://www.cadtutor.net/forum/topic/9848-how-to-get-serial-number-of-hard-drive-by-lisp/?do=findComment&comment=540993
(defun Get_UUID (/ LocatorObj ServiceObj ObjectSetObj UUID)
 (setq LocatorObj
        (vlax-create-object "WbemScripting.SWbemLocator")
 )
 (setq ServiceObj
        (vlax-invoke
          LocatorObj 'ConnectServer nil nil nil nil nil nil nil nil)
 )
 (setq ObjectSetObj
        (vlax-invoke
          ServiceObj
          'ExecQuery
          "SELECT UUID FROM Win32_ComputerSystemProduct"
        )
 )
 (vlax-for Obj ObjectSetObj
   (setq UUID
          (vlax-get Obj 'UUID)
   )
 )
 (foreach Obj
              (list LocatorObj ServiceObj ObjectSetObj)
   (and Obj (vlax-release-object Obj))
 )
 UUID
)

;https://www.cadtutor.net/forum/topic/9848-how-to-get-serial-number-of-hard-drive-by-lisp/?do=findComment&comment=540994
(defun SerialInfo_BIOS (/ WMI meth1 meth2 serial)
(vl-load-com)
(cond ((and (setq WMI (vlax-create-object "WbemScripting.SWbemLocator"))
(setq meth1 (vlax-invoke WMI 'ConnectServer nil nil nil nil nil nil nil nil))
(setq meth2 (vlax-invoke meth1 'ExecQuery (strcat "Select * from Win32_" "BIOS")))
(vlax-for itm (vlax-get (vlax-invoke meth2 'ItemIndex 0) 'Properties_)
(if (eq (vlax-get itm 'name) "SerialNumber")
(setq serial (vlax-get itm 'value)))))))
(mapcar 'vlax-release-object (list meth1 meth2 wmi))
serial)

;https://www.cadtutor.net/forum/topic/9848-how-to-get-serial-number-of-hard-drive-by-lisp/?do=findComment&comment=540995
(defun get_macaddress  (/ Locator Server Query ret)
 (if (and (setq Locator (vlax-create-object "WbemScripting.SWbemLocator"))
          (setq Server (vlax-invoke Locator 'ConnectServer "." "root\\cimv2"))
          (setq Query (vlax-invoke Server 'ExecQuery "select * from Win32_NetworkAdapterConfiguration where IPEnabled = True")))
   (vlax-for item Query (setq ret (vlax-get item 'MacAddress))))
 (foreach obj (list Locator Server Query)
   (vl-catch-all-apply 'vlax-release-object (list obj)))
 ret)



;https://www.cadtutor.net/forum/topic/9848-how-to-get-serial-number-of-hard-drive-by-lisp/?do=findComment&comment=79829
;;;                                                  				*
;;; PART OF 'ASMILIB' LIBRUARY  						*
;;; Created: 07.10.2007         						*
;;; Last modyfied: 07.10.2007   						*
;;; ⓒ Alexanders Smirnovs (ASMI)						*
;;;                                                  				*


;;; *********************************
;;; **** PC Hardware Functions ******
;;; *********************************


;;;                                                  				*
;;; Retrieves Hard Drive serial number                         		        *
;;;                                                  				*
;;; Arguments:                                       				*
;;; 		Path - Path of Hard Drive, for example "C:" (string)		*
;;;                                                  				*
;;; Output:                                          				*
;;;		Hard Drive serial number (integer) or NIL in case of error. 	*
;;;                                                  				*


(defun #Asmi_Get_Drive_Serial (Path / fsObj hSn abPth cDrv)
 (vl-load-com)
 (if
   (and
     (setq fsObj(vlax-create-object "Scripting.FileSystemObject"))
     (not
(vl-catch-all-error-p
  (setq abPth(vl-catch-all-apply 'vlax-invoke-method
	       (list fsObj 'GetAbsolutePathName Path))
	       ); end setq
	   ); end vl-catch-all-error-p
	); end not
  ); end and
   (progn
     (setq cDrv(vlax-invoke-method fsObj 'GetDrive
       (vlax-invoke-method fsObj 'GetDriveName abPth
       ); end vlax-invoke-method
     );end vlax-invoke-method
    ); end setq
    (if
      (vl-catch-all-error-p
  (setq hSn(vl-catch-all-apply 'vlax-get-property
    (list cDrv 'SerialNumber))))
    (progn
      (vlax-release-object cDrv)
      (setq hSn nil)
    ); end progn
      ); end if
   (vlax-release-object fsObj)
   ); end progn
  ); end if
 hSn
 ); end of #Asmi_Get_Drive_Serial


;https://www.theswamp.org/index.php?topic=44425.0
(defun _ping (address / out ws)
  (if (setq ws (vlax-get-or-create-object "WScript.Shell"))
    (progn (setq out (vlax-invoke ws 'run (strcat "ping.exe -n 1 " address) 0 :vlax-true))
	   (and ws (vlax-release-object ws))
	   (zerop out)
    )
  )
)
(if (_ping "google.com") (setq internetping "Yes") (setq internetping "No"))


(setq getiplist (getip))
(setq iplist (LM:str->lst (vl-princ-to-string (car getiplist)) " "))
(setq ipv6list (LM:str->lst (vl-princ-to-string (cadr getiplist)) " "))


;https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/hard-drive-number-in-lisp/m-p/902962/highlight/true#M128620
(defun vl-finddrive (/ DriveList)
(foreach Item '("Z" "X" "Y" "V" "W" "U" "T" "S" "R"
"Q" "P" "O" "N" "M" "L" "K" "J" "I"
"H" "G" "F" "E" "D" "C" "B" "A")
(if (= (vl-file-size (strcat Item ":/")) 0.0)
(setq DriveList (cons (strcat Item ":/") DriveList))
);end if
);end foreach
DriveList
);end vl-finddrive


;https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/decimal-to-hexadecimal/m-p/2874070/highlight/true#M293991
(defun STD-NUM->HEX	(i / s a)
	(setq s "")
	(while (> i 0)
		(setq a	(rem i 16)
			  i	(lsh i -4)
		) ;_  setq
		(setq s	(strcat
					(if	(< a 10)
						(chr (+ 48 a))	; 48: (ascii "0")
						(chr (+ 55 a))
					) ;_  if
					s
				) ;_  strcat
		) ;_  setq
	) ;_  while
) ;_  defun

;https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/list-to-string/m-p/830687/highlight/true#M56345
(defun strlcat (delim lst)
(apply 'strcat
(cons
(substr (car lst) 1 1)
(mapcar
'(lambda (x)
(strcat delim (substr x 1 1))
)
(cdr lst)
)
)
)
)



(setq noofdrive (length (vl-finddrive)))
(setq drivetxt "")
(setq driveindex 0)
(repeat noofdrive
  (setq drivesr (strcat (substr (setq sn (dos_serialno (nth driveindex (vl-finddrive)) )) 1 4) "-" (substr (setq sn (dos_serialno (nth driveindex (vl-finddrive)) )) 5 4)))
  (setq drivetxt (strcat drivetxt "\n " (substr (nth driveindex (vl-finddrive)) 1 1) " Drive Serial = " (vl-princ-to-string drivesr) ))
  (setq driveindex (+ driveindex 1))
)
  
;(princ drivetxt)

(setq infomsg (strcat	" Internet Connection = "
			(vl-princ-to-string internetping)
			"\n IP = "
			(vl-princ-to-string (caddr iplist))
			"\n IPv6 = "
			(vl-princ-to-string (caddr ipv6list))
			"\n Log-on Server = "
			(vl-princ-to-string (getenv "LOGONSERVER"))
			"\n Computer Name = "
			(vl-princ-to-string (getenv "COMPUTERNAME"))
			"\n User Name = "
			(vl-princ-to-string (getenv "USERNAME"))
			"\n MainBoard Serial = "
			(vl-princ-to-string (get_baseboardSerialNumber))
			"\n Processor ID = "
			(vl-princ-to-string (Get_ProcessorId))
			"\n UUID = "
			(vl-princ-to-string (Get_UUID))
			"\n BIOS Serial = "
			(vl-princ-to-string (SerialInfo_BIOS))
			"\n MAC Address = "
			(vl-princ-to-string (get_macaddress))
			"\n Connected Drive = "
			(vl-princ-to-string (strlcat ", " (vl-finddrive)))
                                    drivetxt
		)
)
(princ infomsg)

;; Popup  -  Lee Mac
;; A wrapper for the WSH popup method to display a message box prompting the user.
;; ttl - [str] Text to be displayed in the pop-up title bar
;; msg - [str] Text content of the message box
;; bit - [int] Bit-coded integer indicating icon & button appearance
;; Returns: [int] Integer indicating the button pressed to exit

(defun LM:popup ( ttl msg bit / wsh rtn )
    (if (setq wsh (vlax-create-object "wscript.shell"))
        (progn
            (setq rtn (vl-catch-all-apply 'vlax-invoke-method (list wsh 'popup msg 0 ttl bit)))
            (vlax-release-object wsh)
            (if (not (vl-catch-all-error-p rtn)) rtn)
        )
    )
)

(LM:popup "PC System Info"  infomsg (+ 0 64 4096))
(princ)
)

 

I've put together some nice code written by dilan, for beginners like me.

Others have also been collected and links have been attached.

 

command is SYSINFO

 

- Drive serial is not manufacturer's unique serial number. this can be change with just formatting

 

 

Edited by exceed
  • Like 2
  • Thanks 1
Link to comment
Share on other sites

  • 1 year later...
On 8/8/2018 at 2:32 PM, dilan said:
(defun Get_ProcessorId (/               LocatorObj      SecurityObj
                       SecurityObj     ObjectSetObj    Processor_Id
                      )
 (setq LocatorObj
        (vlax-create-object "WbemScripting.SWbemLocator")
 )
 (setq ServiceObj
        (vlax-invoke
          LocatorObj 'ConnectServer nil nil nil nil nil nil nil nil)
 )
 (setq ObjectSetObj
        (vlax-invoke
          ServiceObj
          'ExecQuery
          "Select * from Win32_Processor"
        )
 )
 (vlax-for Obj ObjectSetObj
   (setq Processor_Id
          (vlax-get Obj 'ProcessorId)
   )
 )
 (foreach Obj
              (list LocatorObj ServiceObj SecurityObj ObjectSetObj)
   (and Obj (vlax-release-object Obj))
 )
 Processor_Id
)
 

 

how to use (princ) for show to user ?

Link to comment
Share on other sites

Add something like (princ (strcat "\nThe Processor ID is: " Processor_Id)) right before the return value of Processor_Id at the  end of the routine.

Link to comment
Share on other sites

On 12/18/2023 at 5:56 PM, pkenewell said:

Add something like (princ (strcat "\nThe Processor ID is: " Processor_Id)) right before the return value of Processor_Id at the  end of the routine.

i tried but not worked !!

 

Link to comment
Share on other sites

(defun Get_BaseBoardSerialNumber (/ LocatorObj ServiceObj ObjectSetObj SerialNumber)
  (setq LocatorObj (vlax-create-object "WbemScripting.SWbemLocator"))
  (setq	ServiceObj
	 (vlax-invoke
	   LocatorObj 'ConnectServer nil nil nil nil nil nil nil nil)
  )
  (setq	ObjectSetObj
	 (vlax-invoke
	   ServiceObj
	   'ExecQuery
	   "Select * from Win32_BaseBoard"
	 )
  )
  (vlax-for Obj	ObjectSetObj
    (setq SerialNumber (vlax-get Obj 'SerialNumber))
  )
  (foreach Obj (list LocatorObj ServiceObj ObjectSetObj)
    (and Obj (vlax-release-object Obj))
  )
  SerialNumber
  (princ (strcat "\nThe SerialNumber is: " SerialNumber))
)

 

Edited by mohammadreza
Link to comment
Share on other sites

@mohammadreza

1) Please post your code in code tags See the "< >" in the toolbar when posting. You can edit your post and correct this. Select the "..." in the upper right corner of your post and select "Edit".

2) Are you getting an error? What does the error report? I would think maybe you are missing the function to load COM at the beginning of the function, i.e. (vl-load-com).

3) The line I suggested should be added before the return value, so that the function can still return a value to whatever called it.

4) The code is working for me with (vl-load-com) at the top. See below:

(defun Get_BaseBoardSerialNumber (/ LocatorObj ServiceObj ObjectSetObj SerialNumber)
   (vl-load-com)
   (setq LocatorObj (vlax-create-object "WbemScripting.SWbemLocator") )
   (setq ServiceObj (vlax-invoke LocatorObj 'ConnectServer nil nil nil nil nil nil nil nil) )
   (setq ObjectSetObj (vlax-invoke ServiceObj 'ExecQuery "Select * from Win32_BaseBoard" ) )
   (vlax-for Obj ObjectSetObj
      (setq SerialNumber (vlax-get Obj 'SerialNumber) )
   )
   (foreach Obj (list LocatorObj ServiceObj ObjectSetObj) 
      (and Obj (vlax-release-object Obj))
   )
   (princ (strcat "\nThe SerialNumber is: " SerialNumber))
   SerialNumber
)

 

Link to comment
Share on other sites

1 hour ago, pkenewell said:

@mohammadreza

1) Please post your code in code tags See the "< >" in the toolbar when posting. You can edit your post and correct this. Select the "..." in the upper right corner of your post and select "Edit".

2) Are you getting an error? What does the error report? I would think maybe you are missing the function to load COM at the beginning of the function, i.e. (vl-load-com).

3) The line I suggested should be added before the return value, so that the function can still return a value to whatever called it.

4) The code is working for me with (vl-load-com) at the top. See below:


(defun Get_BaseBoardSerialNumber (/ LocatorObj ServiceObj ObjectSetObj SerialNumber)
   (vl-load-com)
   (setq LocatorObj (vlax-create-object "WbemScripting.SWbemLocator") )
   (setq ServiceObj (vlax-invoke LocatorObj 'ConnectServer nil nil nil nil nil nil nil nil) )
   (setq ObjectSetObj (vlax-invoke ServiceObj 'ExecQuery "Select * from Win32_BaseBoard" ) )
   (vlax-for Obj ObjectSetObj
      (setq SerialNumber (vlax-get Obj 'SerialNumber) )
   )
   (foreach Obj (list LocatorObj ServiceObj ObjectSetObj) 
      (and Obj (vlax-release-object Obj))
   )
   (princ (strcat "\nThe SerialNumber is: " SerialNumber))
   SerialNumber
)

 

 I used this code but show me this👇
 

baseboardserial.png

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...