[RESOLVED] Different GetVolumeInformation in VB and C#
I currently use GetVolumeInformation to store an ID for each user (a quick way to see how many computers the user has installed my app). My issue is I've now converted across to C# from VB.net and I get different values being returned.
This is the old VB.net code that returns 391637000 on my PC.
vb Code:
Private Declare Ansi Function GetVolumeInformation Lib "Kernel32" Alias "GetVolumeInformationA" ( _
ByVal lpRootPathName As String, _
ByVal lpVolumeNameBuffer As StringBuilder, _
ByVal nVolumeNameSize As Int32, _
ByRef lpVolumeSerialNumber As Int32, _
ByRef lpMaximumComponentLength As Int32, _
ByRef lpFileSystemFlags As Int32, _
ByVal lpFileSystemNameBuffer As StringBuilder, _
ByVal nFileSystemNameSize As Int32) As Boolean
Dim r As Boolean
Dim iSerial As Integer
Dim p_hardDiskId As String
Dim sbVolumeName As New StringBuilder(256)
Dim sbFileSystemName As New StringBuilder(256)
Dim systemDrive As String
systemDrive = Environment.GetEnvironmentVariable("SystemDrive") & "\"
r = GetVolumeInformation(systemDrive, sbVolumeName, sbVolumeName.Capacity, iSerial, 0, 0, sbFileSystemName, sbFileSystemName.Capacity)
p_hardDiskId = Math.Abs(iSerial).ToString
However, after converting the above code to C# it now returns 3903330296.
C# Code:
[DllImport("kernel32.dll")]
private static extern long GetVolumeInformation(string PathName, StringBuilder VolumeNameBuffer, UInt32 VolumeNameSize, ref UInt32 VolumeSerialNumber, ref UInt32 MaximumComponentLength, ref UInt32 FileSystemFlags, StringBuilder FileSystemNameBuffer, UInt32 FileSystemNameSize);
uint serNum = 0;
uint maxCompLen = 0;
UInt32 volFlags = new UInt32();
StringBuilder volInfoString = new StringBuilder(256);
StringBuilder fileSystemName = new StringBuilder(256);
string systemDrive = Environment.GetEnvironmentVariable("SystemDrive") + "\\";
long volInfo = GetVolumeInformation(systemDrive, volInfoString, (UInt32)volInfoString.Capacity, ref serNum, ref maxCompLen, ref volFlags, fileSystemName, (UInt32)fileSystemName.Capacity);
serNum = (uint)Math.Abs(serNum);
I can't work out why/how a different value can be returned using the same function?? :confused:
Anyone seen this issue before?
Re: Different GetVolumeInformation in VB and C#
The first thing that hits the eye is that you have int32 on VB and UInt32 on C#.
I believe the correct definition is for UIn32 so try using these on VB.
So probably C# code is displaying the correct results and not the opposite but again is see you use "long volInfo = GetVolumeInformation".Are you sure about this? I think it is better to use UInt32.
Run some tests,ok.
Edit.It may be that Int32 is the correct and not UInt32 so do your tests with caution.Also long will not probably matter in here so ignore.
Edit2.Are you sure your definition is correct?
Re: Different GetVolumeInformation in VB and C#
Thanks sapator. I changed the UInt32 to Int32 in my C3 code and they both now return the same value.
Re: [RESOLVED] Different GetVolumeInformation in VB and C#
Hey.Happy for you. :wave: