Changeset 2178


Ignore:
Timestamp:
6/18/2010 6:44:31 AM (4 years ago)
Author:
lowjoel
Message:

Fixed error in marshalling managed to unmanaged -- the mount points for any volume would only return a maximum of 1 regardless of how many mountpoints there really were due to the array being null-terminated.

Location:
trunk/eraser/Eraser.Util
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/eraser/Eraser.Util/NativeMethods/Kernel.cs

    r2174 r2178  
    11671167        [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] 
    11681168        [return: MarshalAs(UnmanagedType.Bool)] 
    1169         public static extern bool GetVolumePathNamesForVolumeName( 
    1170             string lpszVolumeName, StringBuilder lpszVolumePathNames, uint cchBufferLength, 
    1171             out uint lpcchReturnLength); 
     1169        public static extern bool GetVolumePathNamesForVolumeName(string lpszVolumeName, 
     1170            [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)] char[] lpszVolumePathNames, 
     1171            uint cchBufferLength, out uint lpcchReturnLength); 
    11721172 
    11731173        public const int MaxPath = 260; 
  • trunk/eraser/Eraser.Util/VolumeInfo.cs

    r2177 r2178  
    9898            { 
    9999                uint returnLength = 0; 
    100                 StringBuilder pathNamesBuffer = new StringBuilder(); 
    101                 pathNamesBuffer.EnsureCapacity(NativeMethods.MaxPath); 
     100                char[] pathNamesBuffer = new char[NativeMethods.MaxPath]; 
    102101                while (!NativeMethods.GetVolumePathNamesForVolumeName(VolumeId, 
    103                     pathNamesBuffer, (uint)pathNamesBuffer.Capacity, out returnLength)) 
     102                    pathNamesBuffer, (uint)pathNamesBuffer.Length, out returnLength)) 
    104103                { 
    105104                    int errorCode = Marshal.GetLastWin32Error(); 
     
    110109                            return result; 
    111110                        case Win32ErrorCode.MoreData: 
    112                             pathNamesBuffer.EnsureCapacity((int)returnLength); 
     111                            pathNamesBuffer = new char[pathNamesBuffer.Length * 2]; 
    113112                            break; 
    114113                        default: 
     
    117116                } 
    118117 
    119                 if (pathNamesBuffer.Length < returnLength) 
    120                     pathNamesBuffer.Length = (int)returnLength; 
    121                 pathNames = pathNamesBuffer.ToString().Substring(0, (int)returnLength); 
     118                pathNames = new string(pathNamesBuffer, 0, (int)returnLength); 
    122119            } 
    123120 
Note: See TracChangeset for help on using the changeset viewer.