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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting the Disk Serial Number

Status
Not open for further replies.

Alt255

Programmer
May 14, 1999
1,846
US
The FSO returns some good information about the disks on a system, including the disk serial numbers. Can anybody tell me how to set the serial number with VB?
Such a thing was very simple using earlier versions of BASIC. In Quick Basic, or even VB for DOS (if I remember correctly), you could set the serial number or volume label with just a few lines of code:
[tt]
DIM InRegs AS RegTypeX, OutRegs AS RegTypeX
InfoBuffer$ = STRING$(25, 0)[/tt]

' Get current info for a disk
' (BX=1 points to the A: drive)
' and place it in a buffer.[tt]
InRegs.Ds = VARSEG(InfoBuffer$)
InRegs.DX = SADD(InfoBuffer$)
InRegs.AX = &H6900
InRegs.BX = &H1
CALL INTERRUPTX(&H21, InRegs, OutRegs)[/tt]

' Set a new serial number in the buffer
' and write it to the disk.[tt]
NewSerial& = 9112001
MID$(InfoBuffer$, 3, 4) = MKL$(NewSerial&)
InRegs.Ds = VARSEG(InfoBuffer$)
InRegs.DX = SADD(InfoBuffer$)
InRegs.AX = &H6901
InRegs.BX = &H1
CALL INTERRUPTX(&H21, InRegs, OutRegs)
[/tt]

Does anybody know a way, with or without the API, to do this from VB6? (Naturally, I'm not interested in shelling to a 16-bit app or trying to write a C++ device driver. LOL)
VCA.gif
 
Function:
Private Declare Function SetVolumeLabel Lib "kernel32.dll" Alias "SetVolumeLabelA" (ByVal lpRootPathName As String, ByVal lpVolumeName As String) As Long

Usage:
Dim liRetval As Long
liRetval = SetVolumeLabel("C:\", "Drive1")


Remedy
 
Interesting... SetVolumeLabel sets the volume label field in the bootsector and creates a new root directory entry to match. It appears to have the same effect as the DOS "LABEL" command.

Regarding my original question: how can I use this function to set the disk serial number?

VCA.gif
 
Alt255 -

I've been giving this some thought, and I'm drawing a blank. One idea was to write a C dll that would embed the needed ASM statements, but then I realized that under NT/2000 the OS would block attempts to write to that address.

Chip H.
 
ChipH, ahhh, the things that NT will allow....

It really didn't occur to me that the preceding calls to Int21h wouldn't work under NT (it turns out that they don't) or that there might not be a way to get the same result with the API. I thought, perhaps, NT would be a bit more forgiving since it allows the following (using Int13h):
[tt]
NewSerial& = &H9112001
Sbuffer$ = STRING$(512, 0)[/tt]

' Read the disk boot sector into a buffer.[tt]
FOR Re = 1 TO 2
InRegs.DX = 0
InRegs.AX = 0
CALL INTERRUPTX(&H13, InRegs, OutRegs)
InRegs.ES = VARSEG(Sbuffer$)
InRegs.BX = SADD(Sbuffer$)
InRegs.AX = &H201
InRegs.CX = &H1
CALL INTERRUPTX(&H13, InRegs, OutRegs)
NEXT[/tt]

' Set the new serial number in the buffer[tt]
MID$(Sbuffer$, 40, 4) = MKL$(NewSerial&)[/tt]

' and overwrite the original boot sector.[tt]
FOR Re = 1 TO 2
InRegs.DX = 0
InRegs.AX = 0
CALL INTERRUPTX(&H13, InRegs, OutRegs)
InRegs.ES = VARSEG(Sbuffer$)
InRegs.BX = SADD(Sbuffer$)
InRegs.AX = &H301
InRegs.CX = &H1
CALL INTERRUPTX(&H13, InRegs, OutRegs)
NEXT
[/tt]

I wouldn't consider modifying a fixed disk with Int13h under NT but it works quite well on removable media (...the only place where I need it to work). I had hoped to find a way to coerce VB and the API into performing the same task.

Thanks for the reply. Let me know if you can think of a way to do this.
VCA.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top