Solved

How to get disk serial number?

Posted on 2004-03-25
13
430 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
0
Comment
Question by:cubrovic
  • 7
  • 3
  • 3
13 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10679898
You could get it using JNI but you can kludge it by Runtime.exec'ing the dir command on Windows
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 10680131
Thats the volume serial number, but it isnt the serial number of the disk I dont think, CEHJ. ;)
0
 
LVL 7

Author Comment

by:cubrovic
ID: 10680134
I 'm satisdfied with any working solution but I need that example code from someone who already done that.
0
 
LVL 7

Author Comment

by:cubrovic
ID: 10680141
Yap I need serial number of the disk  or/and some other hardware components.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 10680149
I've done it in VB, but not Java. Any good ?
0
 
LVL 7

Author Comment

by:cubrovic
ID: 10680227
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 16

Expert Comment

by:krakatoa
ID: 10680253
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
 
LVL 16

Accepted Solution

by:
krakatoa earned 250 total points
ID: 10680611
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
 
LVL 16

Expert Comment

by:krakatoa
ID: 10680645
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 10681346
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
 
LVL 16

Expert Comment

by:krakatoa
ID: 10719718
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 10860250
8-)
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 10861729
Thankx, :)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now