Changeset 1530


Ignore:
Timestamp:
1/15/2010 4:19:35 AM (5 years ago)
Author:
lowjoel
Message:

Replaced the call to Marshal.AllocHGlobal to use the managed StringBuilder? class for automatic buffer marshalling. Addresses #284: Eraser.Util rewrite

Location:
branches/eraser6/CodeReview/Eraser.Util
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eraser6/CodeReview/Eraser.Util/KernelApi.cs

    r1360 r1530  
    11521152            [return: MarshalAs(UnmanagedType.Bool)] 
    11531153            public static extern bool GetVolumePathNamesForVolumeName( 
    1154                 string lpszVolumeName, IntPtr lpszVolumePathNames, uint cchBufferLength, 
     1154                string lpszVolumeName, StringBuilder lpszVolumePathNames, uint cchBufferLength, 
    11551155                out uint lpcchReturnLength); 
    11561156 
  • branches/eraser6/CodeReview/Eraser.Util/VolumeInfo.cs

    r1528 r1530  
    4444 
    4545            //Get the paths of the said volume 
    46             IntPtr pathNamesBuffer = IntPtr.Zero; 
    47             string pathNames = string.Empty; 
    48             try 
    49             { 
    50                 uint currentBufferSize = KernelApi.NativeMethods.MaxPath; 
     46            string pathNames; 
     47            { 
    5148                uint returnLength = 0; 
    52                 pathNamesBuffer = Marshal.AllocHGlobal((int)(currentBufferSize * sizeof(char))); 
     49                StringBuilder pathNamesBuffer = new StringBuilder(); 
     50                pathNamesBuffer.EnsureCapacity(KernelApi.NativeMethods.MaxPath); 
    5351                while (!KernelApi.NativeMethods.GetVolumePathNamesForVolumeName(VolumeId, 
    54                     pathNamesBuffer, currentBufferSize, out returnLength)) 
    55                 { 
    56                     if (Marshal.GetLastWin32Error() == 234/*ERROR_MORE_DATA*/) 
    57                     { 
    58                         Marshal.FreeHGlobal(pathNamesBuffer); 
    59                         currentBufferSize *= 2; 
    60                         pathNamesBuffer = Marshal.AllocHGlobal((int)(currentBufferSize * sizeof(char))); 
    61                     } 
     52                    pathNamesBuffer, (uint)pathNamesBuffer.Capacity, out returnLength)) 
     53                { 
     54                    if (Marshal.GetLastWin32Error() == 234 /*ERROR_MORE_DATA*/) 
     55                        pathNamesBuffer.EnsureCapacity((int)returnLength); 
    6256                    else 
    6357                        throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()); 
    6458                } 
    6559 
    66                 pathNames = Marshal.PtrToStringUni(pathNamesBuffer, (int)returnLength); 
    67             } 
    68             finally 
    69             { 
    70                 if (pathNamesBuffer != IntPtr.Zero) 
    71                     Marshal.FreeHGlobal(pathNamesBuffer); 
     60                if (pathNamesBuffer.Length < returnLength) 
     61                    pathNamesBuffer.Length = (int)returnLength; 
     62                pathNames = pathNamesBuffer.ToString().Substring(0, (int)returnLength); 
    7263            } 
    7364 
Note: See TracChangeset for help on using the changeset viewer.