diff options
author | Jeremy Kuhne <jeremy.kuhne@microsoft.com> | 2017-05-16 18:20:39 +0300 |
---|---|---|
committer | Stephen Toub <stoub@microsoft.com> | 2017-05-16 18:20:39 +0300 |
commit | ac3d3ba3b4097a5626055447b39535e4ee67d9cb (patch) | |
tree | a470b54cea680866b272223f48522d5273451e82 /src/System.IO.FileSystem.DriveInfo | |
parent | 36137caa97d2d1d0db2a1e526e7784ae3f97f8e0 (diff) |
Change CoreFX to use SetThreadErrorMode (#19801)
* Change CoreFX to use SetThreadErrorMode
See #19738. SetErrorMode isn't thread safe- we use SetThreadErrorMode
in NetFX.
* Add P/Invoke exceptions
Diffstat (limited to 'src/System.IO.FileSystem.DriveInfo')
3 files changed, 26 insertions, 15 deletions
diff --git a/src/System.IO.FileSystem.DriveInfo/src/PinvokeAnalyzerExceptionList.analyzerdata b/src/System.IO.FileSystem.DriveInfo/src/PinvokeAnalyzerExceptionList.analyzerdata index 885626aff7..4a86b6a2a7 100644 --- a/src/System.IO.FileSystem.DriveInfo/src/PinvokeAnalyzerExceptionList.analyzerdata +++ b/src/System.IO.FileSystem.DriveInfo/src/PinvokeAnalyzerExceptionList.analyzerdata @@ -1,5 +1,5 @@ kernel32.dll!GetDriveTypeW kernel32.dll!GetLogicalDrives kernel32.dll!GetVolumeInformationW -kernel32.dll!SetErrorMode +kernel32.dll!SetThreadErrorMode kernel32.dll!SetVolumeLabelW diff --git a/src/System.IO.FileSystem.DriveInfo/src/System.IO.FileSystem.DriveInfo.csproj b/src/System.IO.FileSystem.DriveInfo/src/System.IO.FileSystem.DriveInfo.csproj index d9bfdecee0..975c560b38 100644 --- a/src/System.IO.FileSystem.DriveInfo/src/System.IO.FileSystem.DriveInfo.csproj +++ b/src/System.IO.FileSystem.DriveInfo/src/System.IO.FileSystem.DriveInfo.csproj @@ -49,8 +49,8 @@ <Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.SetVolumeLabel.cs"> <Link>Common\Interop\Windows\Interop.SetVolumeLabel.cs</Link> </Compile> - <Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.SetErrorMode.cs"> - <Link>Common\Interop\Windows\Interop.SetErrorMode.cs</Link> + <Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.SetThreadErrorMode.cs"> + <Link>Common\Interop\Windows\Interop.SetThreadErrorMode.cs</Link> </Compile> <Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.SecurityOptions.cs"> <Link>Common\Interop\Windows\Interop.SecurityOptions.cs</Link> diff --git a/src/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Windows.cs b/src/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Windows.cs index 03d5e1933b..f329edaf52 100644 --- a/src/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Windows.cs +++ b/src/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Windows.cs @@ -67,7 +67,8 @@ namespace System.IO StringBuilder fileSystemName = new StringBuilder(fileSystemNameLen); int serialNumber, maxFileNameLen, fileSystemFlags; - uint oldMode = Interop.Kernel32.SetErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS); + uint oldMode; + bool success = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode); try { bool r = Interop.Kernel32.GetVolumeInformation(Name, volumeName, volNameLen, out serialNumber, out maxFileNameLen, out fileSystemFlags, fileSystemName, fileSystemNameLen); @@ -78,7 +79,8 @@ namespace System.IO } finally { - Interop.Kernel32.SetErrorMode(oldMode); + if (success) + Interop.Kernel32.SetThreadErrorMode(oldMode, out oldMode); } return fileSystemName.ToString(); } @@ -90,7 +92,8 @@ namespace System.IO get { long userBytes, totalBytes, freeBytes; - uint oldMode = Interop.Kernel32.SetErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS); + uint oldMode; + bool success = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode); try { bool r = Interop.Kernel32.GetDiskFreeSpaceEx(Name, out userBytes, out totalBytes, out freeBytes); @@ -99,7 +102,8 @@ namespace System.IO } finally { - Interop.Kernel32.SetErrorMode(oldMode); + if (success) + Interop.Kernel32.SetThreadErrorMode(oldMode, out oldMode); } return userBytes; } @@ -111,7 +115,8 @@ namespace System.IO get { long userBytes, totalBytes, freeBytes; - uint oldMode = Interop.Kernel32.SetErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS); + uint oldMode; + bool success = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode); try { bool r = Interop.Kernel32.GetDiskFreeSpaceEx(Name, out userBytes, out totalBytes, out freeBytes); @@ -120,7 +125,8 @@ namespace System.IO } finally { - Interop.Kernel32.SetErrorMode(oldMode); + if (success) + Interop.Kernel32.SetThreadErrorMode(oldMode, out oldMode); } return freeBytes; } @@ -134,7 +140,8 @@ namespace System.IO // Don't cache this, to handle variable sized floppy drives // or other various removable media drives. long userBytes, totalBytes, freeBytes; - uint oldMode = Interop.Kernel32.SetErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS); + uint oldMode; + bool success = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode); try { bool r = Interop.Kernel32.GetDiskFreeSpaceEx(Name, out userBytes, out totalBytes, out freeBytes); @@ -143,7 +150,7 @@ namespace System.IO } finally { - Interop.Kernel32.SetErrorMode(oldMode); + Interop.Kernel32.SetThreadErrorMode(oldMode, out oldMode); } return totalBytes; } @@ -174,7 +181,8 @@ namespace System.IO StringBuilder fileSystemName = new StringBuilder(fileSystemNameLen); int serialNumber, maxFileNameLen, fileSystemFlags; - uint oldMode = Interop.Kernel32.SetErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS); + uint oldMode; + bool success = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode); try { bool r = Interop.Kernel32.GetVolumeInformation(Name, volumeName, volNameLen, out serialNumber, out maxFileNameLen, out fileSystemFlags, fileSystemName, fileSystemNameLen); @@ -190,14 +198,16 @@ namespace System.IO } finally { - Interop.Kernel32.SetErrorMode(oldMode); + if (success) + Interop.Kernel32.SetThreadErrorMode(oldMode, out oldMode); } return volumeName.ToString(); } [System.Security.SecuritySafeCritical] // auto-generated set { - uint oldMode = Interop.Kernel32.SetErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS); + uint oldMode; + bool success = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode); try { bool r = Interop.Kernel32.SetVolumeLabel(Name, value); @@ -212,7 +222,8 @@ namespace System.IO } finally { - Interop.Kernel32.SetErrorMode(oldMode); + if (success) + Interop.Kernel32.SetThreadErrorMode(oldMode, out oldMode); } } } |