Old 5th September 2017, 13:51   #1
r2du-soft
Senior Member
 
r2du-soft's Avatar
 
Join Date: Nov 2013
Location: Iran
Posts: 242
Physical serial number of Windows hard disk

how can get hard disk physical serial number?

HTML Code:
!include WMI.nsh
!include StrRep.nsh

Section

#All physical drives
${WMIGetInfo} root\CIMV2 win32_physicalmedia SerialNumber callback_Function


Quit
SectionEnd

Function callback_Function

${StrRep} '$2' '$2' '$\n$\r' ''
${StrRep} '$2' '$2' '$\n' ''
${StrRep} '$2' '$2' '$\r' ''
${StrRep} '$2' '$2' ' ' ''

${IF} $2 != ""
MessageBox MB_OK "[$2]"
${EndIF}

FunctionEnd 

this code show all disk drives serial number (Hard Disk 0,Hard Disk 1,Hard Disk 2,Flash Disk,...)
i just want get Physical serial number of Windows installed on that hard disk

Last edited by r2du-soft; 5th September 2017 at 15:03.
r2du-soft is offline   Reply With Quote
Old 6th September 2017, 22:40   #2
r2du-soft
Senior Member
 
r2du-soft's Avatar
 
Join Date: Nov 2013
Location: Iran
Posts: 242
is possible convert Batch to nsis

code:

@echo off &setlocal
for /f tokens^=2^ delims^=^" %%i in ('wmic path Win32_LogicalDiskToPartition get^|findstr "\"%SystemDrive%\""') do (
for /f "tokens=2 delims=#," %%j in ("%%i") do (
for /f "tokens=2 delims== " %%k in ('wmic path Win32_PhysicalMedia where "Tag='\\\\.\\PHYSICALDRIVE%%j'" get SerialNumber /value') do (
for /f "delims=" %%l in ("%%k") do set "SerialNumber=%%l"
)
)
)
echo %SerialNumber%
pause


r2du-soft is offline   Reply With Quote
Old 6th September 2017, 23:30   #3
Nutzzz
Junior Member
 
Join Date: May 2007
Location: Orange County, CA, U.S.A.
Posts: 11
You could just use nsExec::ExecToStack to call your batch file rather than using the WMI.nsh macros...
Nutzzz is offline   Reply With Quote
Old 7th September 2017, 01:10   #4
r2du-soft
Senior Member
 
r2du-soft's Avatar
 
Join Date: Nov 2013
Location: Iran
Posts: 242
Yes i know but i do not want open this command as batch fule to stack,i want convert to nsis code...
r2du-soft is offline   Reply With Quote
Old 7th September 2017, 11:51   #5
r2du-soft
Senior Member
 
r2du-soft's Avatar
 
Join Date: Nov 2013
Location: Iran
Posts: 242
Find the hard drive that windows has installed on it by PHYSICAL DRIVE number from among the hard disks and the flash disks connected to the computer and find Serial Number For making software locks

HTML Code:
!include WMI.nsh
!include StrRep.nsh
!include "x64.nsh"
!include "StrCount.nsh"

Var StackS1
Var LineReaded
Var FindWord



Section
${WMIGetInfo} root\CIMV2 Win32_LogicalDiskToPartition /value HDDPhysicalDiskP1
Call HDDPhysicalDiskP2
nsExec::ExecToStack "wmic path Win32_PhysicalMedia where Tag='\\\\.\\PHYSICALDRIVE$3' get SerialNumber /value"
	Pop $0 # return value/error/timeout
	Pop $1 # printed text, up to ${NSIS_MAX_STRLEN}
;MessageBox MB_OK "$1"
${StrRep} '$1' '$1' 'SerialNumber=' ''
${StrRep} '$1' '$1' ' ' ''
${StrRep} '$1' '$1' '$\n$\r' ''
${StrRep} '$1' '$1' '$\n' ''
${StrRep} '$1' '$1' '$\r' ''
MessageBox MB_OK "Physical serial number of Windows installed on that hard disk is:$\n$\r[$1]"
SectionEnd



Function HDDPhysicalDiskP2
StrCpy $FindWord "$%HomeDrive%" #Searching String In Variable
;------------------------------------------------------------
StrCpy $1 0
StrCpy $5 0
StrCpy $6 0
;------
loop:
IntOp $6 $6 + 1
StrCpy $2 $StackS1 1 $1
;-------------------
IntOp $1 $1 + 1
StrCmp $2 '$\n' found
StrCmp $2 '' found loop
IntOp $5 $5 + 1
;------
found:
IntOp $3 $3 + 1
StrCpy $LineReaded $StackS1 $6 $5
StrCpy $5 ""
StrCpy $6 ""
;------------------------------------------------------------
${StrCount} "$LineReaded" "$FindWord" "i"
Pop $0
;--------
${If} $0 != "0"
StrCpy $4 "$LineReaded" 1
${else}
StrCpy $5 $1
Goto loop #read Next Line
${endIf}
;------------------------------------------------------------

;------------------------------------------------------------
IntOp $3 $3 - 1

StrCpy $1 "0"
loop20:
IntOp $1 $1 + 1
StrCpy $2 $3 1 $1
;MessageBox MB_OK "$2"
StrCmp $2 ',' found20
StrCmp $2 '' found20 loop20
	found20:
		StrCpy $3 $3 $1 ""
		;MessageBox MB_OK "Windows Installed On PhysicalDrive $3"
		DetailPrint "Windows Installed On PhysicalDrive $3"
;------------------------------------------------------------
FunctionEnd


Function HDDPhysicalDiskP1
${StrRep} '$2' '$2' '$\n$\r' ''
${StrRep} '$2' '$2' '$\n' ''
${StrRep} '$2' '$2' '$\r' ''
${StrRep} '$2' '$2' ' ' ''
${IF} $2 != ""
;MessageBox MB_OK "[$2]"
StrCpy $StackS1 "$StackS1$2$\n$\r"
${EndIF}
FunctionEnd 
But a question:
this way is standard?
I mean, is there a better way to do this?

Last edited by r2du-soft; 7th September 2017 at 13:07.
r2du-soft is offline   Reply With Quote
Old 9th September 2017, 00:07   #6
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 4,590
You could maybe do it with DeviceIoControl or the SetupAPI but the code would be longer and more complicated...

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Reply
Go Back   Winamp & SHOUTcast Forums > Developer Center > NSIS Discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump