Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29275 Discussions

Trouble getting correct result from Windows API function GetVolumeInformation

Leigh_Wardle
Beginner
1,042 Views

Hi all,

I'm trying to use the Windows API function GetVolumeInformation located in kernel32 to read the Volume Serial Number for drive C:.

For my system it reports the Volume Serial Number = 4937288.
But this is not the correct Volume Serial Number (=72919446) - as given both by an equivalent program in VB6 and also the output from the DOS command VOL C:

My simple program is shown below (adapted from http://www.pgroup.com/userforum/viewtopic.php?p=4180&sid=6d107daf5726667006cde0266a6b2d3a).

Can anyone tell me what I'm doing wrong in Fortran?

Thanks, Leigh

! code adapted from http://www.pgroup.com/userforum/viewtopic.php?p=4180&sid=6d107daf5726667006cde0266a6b2d3a

program main

USE KERNEL32

Integer(BOOL) iret
Character*256 RootPathName
Character*256 VolumeNameBuffer
Integer(DWORD) VolumeNameSize
Integer(HANDLE) VolumeSerialNumber
Integer(HANDLE) MaximumComponentLength
Integer(HANDLE) FileSystemFlags
Character*256 FileSystemNameBuffer
Integer(DWORD) FileSystemNameSize

RootPathName = "c:"
VolumeNameSize = 256
FileSystemNameSize = 256

iret = GetVolumeInformation( &
TRIM(RootPathName), &
VolumeNameBuffer, &
VolumeNameSize, &
VolumeSerialNumber, &
MaximumComponentLength, &
FileSystemFlags, &
FileSystemNameBuffer, &
FileSystemNameSize)

print *,"File system name: ", FileSystemNameBuffer
print *,"Volume serial number: ", VolumeSerialNumber

end program main

0 Kudos
2 Replies
yamajun2
Beginner
1,042 Views
Quoting - Leigh Wardle


Integer(HANDLE) VolumeSerialNumber
Integer(HANDLE) MaximumComponentLength
Integer(HANDLE) FileSystemFlags

These variables are HANDLEs. Add LOC() when you call API.

[cpp]iret = GetVolumeInformation( &
TRIM(RootPathName), &
VolumeNameBuffer, &
VolumeNameSize, &
LOC(VolumeSerialNumber), &
LOC(MaximumComponentLength), &
LOC(FileSystemFlags), &
FileSystemNameBuffer, &
FileSystemNameSize)

print '(a, Z16)',"Volume serial number:  ", VolumeSerialNumber


[/cpp]

0 Kudos
Leigh_Wardle
Beginner
1,042 Views
Quoting - yamajun

These variables are HANDLEs. Add LOC() when you call API.

[cpp]iret = GetVolumeInformation( &
TRIM(RootPathName), &
VolumeNameBuffer, &
VolumeNameSize, &
LOC(VolumeSerialNumber), &
LOC(MaximumComponentLength), &
LOC(FileSystemFlags), &
FileSystemNameBuffer, &
FileSystemNameSize)

print '(a, Z16)',"Volume serial number: ", VolumeSerialNumber


[/cpp]

Thanks, yamajun, for your help. Those changes fixed the problem.

Regards, Leigh

0 Kudos
Reply