?
Solved

How to get disk serial number?

Posted on 2004-03-25
13
Medium Priority
?
437 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month11 days, 11 hours left to enroll

752 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