Results 1 to 4 of 4

Thread: [RESOLVED] Different GetVolumeInformation in VB and C#

  1. #1

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Resolved [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:
    1. Private Declare Ansi Function GetVolumeInformation Lib "Kernel32" Alias "GetVolumeInformationA" ( _
    2.         ByVal lpRootPathName As String, _
    3.         ByVal lpVolumeNameBuffer As StringBuilder, _
    4.         ByVal nVolumeNameSize As Int32, _
    5.         ByRef lpVolumeSerialNumber As Int32, _
    6.         ByRef lpMaximumComponentLength As Int32, _
    7.         ByRef lpFileSystemFlags As Int32, _
    8.         ByVal lpFileSystemNameBuffer As StringBuilder, _
    9.         ByVal nFileSystemNameSize As Int32) As Boolean
    10.  
    11.  
    12.  Dim r As Boolean
    13.         Dim iSerial As Integer
    14.         Dim p_hardDiskId As String
    15.         Dim sbVolumeName As New StringBuilder(256)
    16.         Dim sbFileSystemName As New StringBuilder(256)
    17.         Dim systemDrive As String
    18.  
    19.        systemDrive = Environment.GetEnvironmentVariable("SystemDrive") & "\"
    20.  
    21.             r = GetVolumeInformation(systemDrive, sbVolumeName, sbVolumeName.Capacity, iSerial, 0, 0, sbFileSystemName, sbFileSystemName.Capacity)
    22.             p_hardDiskId = Math.Abs(iSerial).ToString

    However, after converting the above code to C# it now returns 3903330296.
    C# Code:
    1. [DllImport("kernel32.dll")]
    2.         private static extern long GetVolumeInformation(string PathName, StringBuilder VolumeNameBuffer, UInt32 VolumeNameSize, ref UInt32 VolumeSerialNumber, ref UInt32 MaximumComponentLength, ref UInt32 FileSystemFlags, StringBuilder FileSystemNameBuffer, UInt32 FileSystemNameSize);
    3.  
    4. uint serNum = 0;
    5.             uint maxCompLen = 0;
    6.                 UInt32 volFlags = new UInt32();
    7.                 StringBuilder volInfoString = new StringBuilder(256);
    8.                 StringBuilder fileSystemName = new StringBuilder(256);
    9.                 string systemDrive = Environment.GetEnvironmentVariable("SystemDrive") + "\\";
    10.                 long volInfo = GetVolumeInformation(systemDrive, volInfoString, (UInt32)volInfoString.Capacity, ref serNum, ref maxCompLen, ref volFlags, fileSystemName, (UInt32)fileSystemName.Capacity);
    11.                 serNum = (uint)Math.Abs(serNum);

    I can't work out why/how a different value can be returned using the same function??

    Anyone seen this issue before?

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    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?
    Last edited by sapator; Oct 7th, 2010 at 08:10 PM.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  3. #3

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    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.

  4. #4
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: [RESOLVED] Different GetVolumeInformation in VB and C#

    Hey.Happy for you.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width