Skip to content

Commit 029aadd

Browse files
authored
Cleanup Windows error mode setting with media insertion propmt (#121464)
According to https://devblogs.microsoft.com/oldnewthing/20170330-00/ , SEM_NOOPENFILEERRORBOX only has effect on Win16 API.
1 parent a67d388 commit 029aadd

File tree

3 files changed

+9
-33
lines changed

3 files changed

+9
-33
lines changed

src/coreclr/inc/holder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1166,7 +1166,7 @@ class ErrorModeHolder final
11661166
ErrorModeHolder()
11671167
: m_revert{ FALSE }
11681168
{
1169-
DWORD newMode = SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS;
1169+
DWORD newMode = SEM_FAILCRITICALERRORS;
11701170
m_revert = ::SetThreadErrorMode(newMode, &m_oldMode);
11711171
}
11721172
~ErrorModeHolder() noexcept

src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Windows.cs

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,13 @@ public long AvailableFreeSpace
4848
get
4949
{
5050
long userBytes, totalBytes, freeBytes;
51-
uint oldMode;
52-
bool success = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode);
53-
try
51+
52+
using (DisableMediaInsertionPrompt.Create())
5453
{
5554
bool r = Interop.Kernel32.GetDiskFreeSpaceEx(Name, out userBytes, out totalBytes, out freeBytes);
5655
if (!r)
5756
throw Error.GetExceptionForLastWin32DriveError(Name);
5857
}
59-
finally
60-
{
61-
if (success)
62-
Interop.Kernel32.SetThreadErrorMode(oldMode, out _);
63-
}
6458
return userBytes;
6559
}
6660
}
@@ -70,19 +64,13 @@ public long TotalFreeSpace
7064
get
7165
{
7266
long userBytes, totalBytes, freeBytes;
73-
uint oldMode;
74-
bool success = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode);
75-
try
67+
68+
using (DisableMediaInsertionPrompt.Create())
7669
{
7770
bool r = Interop.Kernel32.GetDiskFreeSpaceEx(Name, out userBytes, out totalBytes, out freeBytes);
7871
if (!r)
7972
throw Error.GetExceptionForLastWin32DriveError(Name);
8073
}
81-
finally
82-
{
83-
if (success)
84-
Interop.Kernel32.SetThreadErrorMode(oldMode, out _);
85-
}
8674
return freeBytes;
8775
}
8876
}
@@ -94,18 +82,13 @@ public long TotalSize
9482
// Don't cache this, to handle variable sized floppy drives
9583
// or other various removable media drives.
9684
long userBytes, totalBytes, freeBytes;
97-
uint oldMode;
98-
Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode);
99-
try
85+
86+
using (DisableMediaInsertionPrompt.Create())
10087
{
10188
bool r = Interop.Kernel32.GetDiskFreeSpaceEx(Name, out userBytes, out totalBytes, out freeBytes);
10289
if (!r)
10390
throw Error.GetExceptionForLastWin32DriveError(Name);
10491
}
105-
finally
106-
{
107-
Interop.Kernel32.SetThreadErrorMode(oldMode, out _);
108-
}
10992
return totalBytes;
11093
}
11194
}
@@ -142,9 +125,7 @@ public unsafe string VolumeLabel
142125
[SupportedOSPlatform("windows")]
143126
set
144127
{
145-
uint oldMode;
146-
bool success = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode);
147-
try
128+
using (DisableMediaInsertionPrompt.Create())
148129
{
149130
bool r = Interop.Kernel32.SetVolumeLabel(Name, value);
150131
if (!r)
@@ -156,11 +137,6 @@ public unsafe string VolumeLabel
156137
throw Error.GetExceptionForWin32DriveError(errorCode, Name);
157138
}
158139
}
159-
finally
160-
{
161-
if (success)
162-
Interop.Kernel32.SetThreadErrorMode(oldMode, out _);
163-
}
164140
}
165141
}
166142
}

src/mono/mono/metadata/domain.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ mono_init_internal (const char *root_domain_name)
114114

115115
#if defined(HOST_WIN32) && HAVE_API_SUPPORT_WIN32_SET_ERROR_MODE
116116
/* Avoid system error message boxes. */
117-
SetErrorMode (SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
117+
SetErrorMode (SEM_FAILCRITICALERRORS);
118118
#endif
119119

120120
#ifndef HOST_WIN32

0 commit comments

Comments
 (0)