We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

How to get disk serial number?

cubrovic
cubrovic asked
on
Medium Priority
479 Views
Last Modified: 2012-06-27
Hi guys.
I need jni solution to get  disk serial number and/or some other serial numbers of hardware components.
tia Cubrovic
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
You could get it using JNI but you can kludge it by Runtime.exec'ing the dir command on Windows
CERTIFIED EXPERT

Commented:
Thats the volume serial number, but it isnt the serial number of the disk I dont think, CEHJ. ;)

Author

Commented:
I 'm satisdfied with any working solution but I need that example code from someone who already done that.

Author

Commented:
Yap I need serial number of the disk  or/and some other hardware components.
CERTIFIED EXPERT

Commented:
I've done it in VB, but not Java. Any good ?

Author

Commented:
Yes if I can put it in dll with and give me specification of this and I can use it from my java code.
I think that would be a good solution for me.
CERTIFIED EXPERT

Commented:
cubrovic:

I'll post the API call anyway, (VB) and tell us if that helps any or not.


Declare Function GetVolumeInformation Lib _
"kernel32.dll" Alias "GetVolumeInformationA" (ByVal _
lpRootPathName As String, ByVal lpVolumeNameBuffer As _
String, ByVal nVolumeNameSize As Integer, _
lpVolumeSerialNumber As Long, lpMaximumComponentLength _
As Long, lpFileSystemFlags As Long, ByVal _
lpFileSystemNameBuffer As String, ByVal _
nFileSystemNameSize As Long) As Long


this is just the header, but .... I am sure (although this is way back in the mists of time for me), that there is an internet site with bits like this on. Cant remember where I saw it tho; :(
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
CERTIFIED EXPERT

Commented:
LOL! Now I read over it again, I can see that *most* of it has nothing to do with getting the sernum - it was a function I was trying to write for passworded access, based on the machine the user was using.! ;)

The sernum bit is right at the top as you can see.
CERTIFIED EXPERT
Top Expert 2016
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
CERTIFIED EXPERT

Commented:
cubrovic:

You can do it wiht this. You can then hack this around to get what you want, and maybe even do a small exe from it then java to it via Runtime.exec() call:




'Attribute VB_Name = "Module1"
Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long
Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" (ByVal lpFileName As String) As Long
Declare Function MoveFile Lib "kernel32" Alias "MoveFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String) As Long
Declare Function FlushFileBuffers Lib "kernel32" (ByVal hFile As Long) As Long
Declare Function MoveFileEx Lib "kernel32" Alias "MoveFileExA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal dwFlags As Long) As Long
Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Declare Sub GlobalMemoryStatus Lib "kernel32" (lpmstMemStat As MEMORYSTATUS)

Declare Function GetVolumeInformation Lib _
"kernel32.dll" Alias "GetVolumeInformationA" (ByVal _
lpRootPathName As String, ByVal lpVolumeNameBuffer As _
String, ByVal nVolumeNameSize As Integer, _
lpVolumeSerialNumber As Long, lpMaximumComponentLength _
As Long, lpFileSystemFlags As Long, ByVal _
lpFileSystemNameBuffer As String, ByVal _
nFileSystemNameSize As Long) As Long

Public Const MOVEFILE_DELAY_UNTIL_REBOOT = &H4
Public Const MAX_PATH = 260

Type MEMORYSTATUS
    dwLength As Long         ' 32
    dwMemoryLoad As Long     ' percent of memory in use
    dwTotalPhys As Long      ' bytes of physical memory
    dwAvailPhys As Long      ' free physical memory bytes
    dwTotalPageFile As Long  ' bytes of paging file
    dwAvailPageFile As Long  ' free bytes of paging file
    dwTotalVirtual As Long   ' user bytes of address space
    dwAvailVirtual As Long   ' free user bytes
End Type


Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
End Type


Type WIN32_FIND_DATA
        dwFileAttributes As Long
        ftCreationTime As FILETIME
        ftLastAccessTime As FILETIME
        ftLastWriteTime As FILETIME
        nFileSizeHigh As Long
        nFileSizeLow As Long
        dwReserved0 As Long
        dwReserved1 As Long
        cFileName As String * MAX_PATH
        cAlternate As String * 14
End Type




Public Function getdiskserialnumber() As Long

Dim ms As MEMORYSTATUS
Dim binomial As Double
Dim binomialback As Double
Dim bitofserialnumber As String * 1
Dim returnname As String
Dim a As Integer
Dim snum As String * 1
Dim SerialNum As Long
Dim result As Long
Dim Temp1 As String
Dim Temp2 As String
Dim strDrive As String
Dim password As String

Temp1 = String$(255, Chr$(0))
Temp2 = String$(255, Chr$(0))
strDrive = "C:\"

result = Module1.GetVolumeInformation(strDrive, Temp1, _
Len(Temp1), SerialNum, 0, 0, Temp2, Len(Temp2))

Stop


End Function


CERTIFIED EXPERT
Top Expert 2016

Commented:
8-)
CERTIFIED EXPERT

Commented:
Thankx, :)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.