A "simple" API call, but no response (Disk Serial

Started by Dwight Scoles, November 24, 2006, 08:24:29 PM

Previous topic - Next topic

Dwight Scoles

I have attempted to use the WIN32API.inc file to retrieve the serial number of the C drive.  It looks like the call should be straight forward.  However, I've never coded for any of these API calls before so am feeling a little lost.

The function I'm attempting to use is GetVolumeInformation()
The return parameter returns a zero (no error?).
But the passed parameters all return blanks or zero.  Here is the code

'------------------------------------------------------------------------------------------------------------------------
' Return the volume name, size and ID (serial number)
'------------------------------------------------------------------------------------------------------------------------
Function ReturnVolume(DriveLetter As String, VolumeSize As String, VolumeID As String, ErrMsg As String) As String
' for a drive letter, return the volume name, serial number, and maximum size in bytes (as a string)

Local lpRootPathName As Asciiz * 20
Local lpVolumeNameBuffer As Asciiz * 20
Local nVolumeNameSize As Dword
Local lpVolumeSerialNumber As Dword
Local lpMaximumComponentLength As Dword
Local lpFileSystemFlags As Dword
Local lpFileSystemNameBuffer As Asciiz * 20
Local nFileSystemNameSize As Dword
Local RetVal As Long

Local VolName As String
Local NameBuffer As String
Local VolSize As Dword
Local NameAttrib As Long



lpRootPathName = "c:\"     ' tried c:, c:/ as well
RetVal = GetVolumeInformation(lpRootPathName, lpVolumeNameBuffer, _
                      ByVal nVolumeNameSize, _
                      lpVolumeSerialNumber, lpMaximumComponentLength, _
                      lpFileSystemFlags, lpFileSystemNameBuffer, _
                      ByVal nFileSystemNameSize)

' Above parameters return zilch
' i've resorted to these PB calls which do provide me the correct results
' However, I need the serial number which I can not find with PB calls (as far as I know)
VolSize = DiskSize(DriveLetter)
VolumeSize = Str$(VolSize)
NameAttrib = 8
VolName = Dir$(DriveLetter , NameAttrib)
ReturnVolume = VolName
END FUNCTION


As the first line in my code I include WIN32API.inc
One of the first lines included in my application is:
LOADLIBRARY "KERNEL32.DLL"   ' Tried with and without this line

Any suggestions?

What I would have done is execute the DOS command DIR C:\ and parse the output, but could not figure out how to get SHELL to return the output in a variable.  I tried adding " > C:/output.txt" to the end of the SHELL command but output.txt was zero length when done.  In a different language I use I would code that as:
PCPERFORM "DIR C:\" CAPTURING OUTPUT.VAR
but I do not seem to find any equivalent PB coding.

Dwight (wandering in the wilderness again)

Jose Roca

> The return parameter returns a zero (no error?).

If all the requested information is retrieved, the return value is nonzero.
If not all the requested information is retrieved, the return value is zero. To get extended error information, call GetLastError.

You are not filling nVolumeNameSize and nFileSystemNameSize with the sizes of the buffers, so you are saying to the function that its length is zero.

This works:


#COMPILE EXE
#DIM ALL
#INCLUDE "WIN32API.INC"

FUNCTION PBMAIN () AS LONG

Local lpRootPathName As Asciiz * 20
Local lpVolumeNameBuffer As Asciiz * 20
Local nVolumeNameSize As Dword
Local lpVolumeSerialNumber As Dword
Local lpMaximumComponentLength As Dword
Local lpFileSystemFlags As Dword
Local lpFileSystemNameBuffer As Asciiz * 20
Local nFileSystemNameSize As Dword
Local RetVal      As Long

Local VolName       As String
Local NameBuffer   As String
Local VolSize      As Dword
Local NameAttrib   As Long

nVolumeNameSize = SIZEOF(lpVolumeNameBuffer)
nFileSystemNameSize = SIZEOF(lpFileSystemNameBuffer)

lpRootPathName = "c:\"
RetVal = GetVolumeInformation(lpRootPathName, lpVolumeNameBuffer, _
                      ByVal nVolumeNameSize, _
                      lpVolumeSerialNumber, lpMaximumComponentLength, _
                      lpFileSystemFlags, lpFileSystemNameBuffer, _
                      ByVal nFileSystemNameSize)
IF RetVal THEN
  MSGBOX LEFT$(HEX$(lpVolumeSerialNumber), 4) & "-" & MID$(HEX$(lpVolumeSerialNumber), 5)
END IF

END FUNCTION

Dwight Scoles

Thank you Jose!  I had looked in the documentation on MSDN and did not see this.  Of course, it makes sense.  And cured the ills of my routine.

I was thinking of implementing your Drive Object from the PB forum, but I found that pasting the code into my editor resulted in a single line containing the entire code.  So, I gave up after editing for a while on it.

The best to you.

John Montenigro

Dwight,
Search POFFS (or Google the PB website) for "WebClip".  I don't remember who contributed it.

After you boot your machine, you run WebClip once. When you copy code from the PB website, WebClip reformats it so that you can paste it into the IDE or Notebook, etc.

-John

Dwight Scoles

Hi John,

I found the link on the weblcip in POFFS.  

:idea:  I also read in one of the emails related to webclip that the QUOTE button brings up an edit box that the code can be cut from.  It works quite well.  You just click on
Quoteat the top margin, right corner, of any message and the raw message is reformatted for editing.

Thanks for the note on POFFS.  I've downloaded the file.  It should be very helpful.

Dwight