Friday, July 23, 2010

How to get hard drive serial number with Powerbuilder

Here's the code and trick to get the hard drive informations in Powerbuilder, including hard drive serial number.

First, create the External Function called GetVolumeInformation, which taken from Kernel32.dll

FUNCTION long GetVolumeInformation & 
  (string lpRootPathName, REF string lpVolumeNameBuffer, &
   long nVolumeNameSize, & 
   REF long lpVolumeSerialNumber, REF long lpMaximumComponentLength, & 
   REF long lpFileSystemFlags, REF string lpFileSystemNameBuffer, & 
   long nFileSystemNameSize) & 
   LIBRARY "Kernel32.dll" ALIAS FOR "GetVolumeInformationA"

Create the Window Function called of_long2hex with al_number and ai_digit as parameters, and the function will returns string. al_number is long type and ai_digit is integer type. The function is for converting Long value to Hex Value.

Type the script below:

long ll_temp0, ll_temp1
char lc_ret

IF ai_digit > 0 THEN
    ll_temp0 = abs(al_number / (16 ^ (ai_digit - 1)))
    ll_temp1 = ll_temp0 * (16 ^ (ai_digit - 1))
    IF ll_temp0 > 9 THEN
        lc_ret = char(ll_temp0 + 55)
        lc_ret = char(ll_temp0 + 48)
    END IF
    RETURN lc_ret + of_long2hex(al_number - ll_temp1 , ai_digit - 1) 

Finally, create another Window Function called of_getserialnumber with strrootpath as parameter, and the function will returns string. I believe strrootpath is a string variable type. Beside the hard drive serial number, this function will return another variables, like Volume Name of the hard drive (ls_volbuffer) and File system name (ls_fsname) like FAT32 or NTFS.
Next, type this script below:

String ls_volbuffer, ls_fsname, sReturn
Long  ll_serial, ll_MaxCompLength, ll_FileSystemFlags, ll_rtn

ls_volbuffer = Space(255)
ls_fsname = Space(255)
ll_maxCompLength = 0
ll_FileSystemFlags = 0

ll_rtn = GetVolumeinformation(strrootpath , ls_volbuffer, 255, ll_serial, & 
                 ll_MaxCompLength, ll_FileSystemFlags , ls_fsname, 255)
sReturn = of_long2hex(abs(ll_serial),8)
return left(sReturn,4)+"-"+right(sReturn,4)

To use the script, just create a simple script like below

String ls_SN
ls_SN = of_getserialnumber("C:\")
MessageBox("Your Hard Drive SN",ls_SN) 

No comments:

Post a Comment