How to get disk serial number?

Hi guys.
I need jni solution to get  disk serial number and/or some other serial numbers of hardware components.
tia Cubrovic
LVL 7
cubrovicAsked:
Who is Participating?
 
krakatoaConnect With a Mentor Commented:
cubrovic:

I dont know how much the rest of it will help you, but here is the function I wrote to do it (it's a long time ago, but as far as I can remember, it worked ok. Surprised I even found the code - it was on a floppy disk if you remember such things) :)  

PS: You'll know, I'm sure, which bits of this funtion dont have anything to do with just finding the number.


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))


returnname = CStr(SerialNum * 13)

On Error Resume Next
    SetAttr "C:\windows\system\pad.pad", vbNormal

    fileexists = Dir("C:\windows\system\pad.pad")

    If fileexists <> "pad.pad" Then 'Stop
        returnname = InputBox("What is your name?", CStr((SerialNum * 8) + 22))
        If Val(returnname) <> SerialNum * 13 Then End
    End If

    SetAttr "C:\windows\system\pad.pad", vbNormal
    Open "C:\windows\system\pad.pad" For Binary Access Read Write As #1
    Close #1
On Error GoTo 0

Kill "C:\windows\system\pad.pad"
Open "C:\windows\system\pad.pad" For Binary Access Read Write As #1


For a = 1 To Len(Str$(SerialNum)) - 1

    bitofserialnumber = Trim$(Val(Mid$(CStr(SerialNum), a, 1)))
    binomial = CDbl(bitofserialnumber)
    Put #1, , binomial 'bitofserialnumber
   
Next


Seek #1, 1

For a = 1 To Len(CStr(SerialNum))

    Get #1, , binomialback
    snumval = CDec(binomialback)
    password = password + Trim$(CStr(snumval))
   

Next
Close #1
SetAttr "C:\windows\system\pad.pad", vbHidden

If SerialNum = Val(password) Then
    'MsgBox ("Passed security check")
End If


'MsgBox ("Serial number of your hard drive is " & Str$(SerialNum))


'this reports memory data - for interest only

    ms.dwLength = Len(ms)
    GlobalMemoryStatus ms
    'ans = MsgBox("Total physical memory: ", , ms.dwTotalPhys)
    'ans = MsgBox("Available physical memory: ", , ms.dwAvailPhys)
    ans = MsgBox(CStr(ms.dwMemoryLoad) & " % of Memory is in use")
    'ans = MsgBox(ms.dwLength)
   
    getdiskserialnumber = SerialNum

End Function
0
 
CEHJCommented:
You could get it using JNI but you can kludge it by Runtime.exec'ing the dir command on Windows
0
 
krakatoaCommented:
Thats the volume serial number, but it isnt the serial number of the disk I dont think, CEHJ. ;)
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
cubrovicAuthor Commented:
I 'm satisdfied with any working solution but I need that example code from someone who already done that.
0
 
cubrovicAuthor Commented:
Yap I need serial number of the disk  or/and some other hardware components.
0
 
krakatoaCommented:
I've done it in VB, but not Java. Any good ?
0
 
cubrovicAuthor 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.
0
 
krakatoaCommented:
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; :(
0
 
krakatoaCommented:
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.
0
 
CEHJConnect With a Mentor Commented:
This is the kludge i mentioned. Works ok on my machine:


import java.io.*;

/**
 *  Gets the HD serial number
 *
 * @author     CEHJ
 * @created    25 March 2004
 */
public class VolumeSerialNumberGetter {
      StringBuffer collector;


      public static void main(String[] args) {
            new VolumeSerialNumberGetter().run(args);
      }


      public void run(String[] args) {

            try {

                  if (args.length < 1) {
                        //System.out.println("Usage: java CommandRunner <command string>");
                        args = new String[] { "cmd.exe",  "/c",  "dir", "|",  "find", "/i",  "Serial Number is" };
                  }
                  Process pro = Runtime.getRuntime().exec(args);
                  InputStream error = pro.getErrorStream();
                  InputStream output = pro.getInputStream();

                  collector = new StringBuffer();
                  ProcessStreamReader collectingReader = new ProcessStreamReader(output);
                  Thread out = new Thread(collectingReader);
                  out.start();
                  out.join();
                  String volumeNumber = collector.toString();
                  volumeNumber = volumeNumber.substring(volumeNumber.lastIndexOf(' '));
                  System.out.println(volumeNumber.trim());
            }
            catch (java.io.IOException e) {
                  e.printStackTrace();
            }
            catch (java.lang.InterruptedException e) {
                  e.printStackTrace();
            }

      }

      class ProcessStreamReader implements Runnable {

            InputStream is;

            public ProcessStreamReader(InputStream is) {
                  this.is = is;
            }

            public void run() {
                  try {
                        BufferedReader in = new BufferedReader(new InputStreamReader(is));
                        String temp = null;
                        while ((temp = in.readLine()) != null) {
                              collector.append(temp);
                        }
                        is.close();
                  }
                  catch (Exception e) {
                        e.printStackTrace();
                  }
            }
      }

}

0
 
krakatoaCommented:
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


0
 
CEHJCommented:
8-)
0
 
krakatoaCommented:
Thankx, :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.