Skip to content
This repository was archived by the owner on Jul 26, 2023. It is now read-only.

Add kernel32!SetErrorMode #447

Merged
merged 2 commits into from
Feb 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Kernel32.Tests/Kernel32Facts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public void SetLastError_ImpactsMarshalGetLastWin32Error()
Assert.Equal(2, Marshal.GetLastWin32Error());
}

[Fact]
public void SetErrorMode_Works()
{
ErrorModes oldMode = SetErrorMode(ErrorModes.SEM_DEFAULT);
}

[Fact]
public unsafe void GetStartupInfo_Title()
{
Expand Down
58 changes: 58 additions & 0 deletions src/Kernel32/Kernel32+ErrorModes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) All contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace PInvoke
{
using System;

/// <content>
/// Contains the <see cref="ErrorModes"/> nested type.
/// </content>
public partial class Kernel32
{
[Flags]
public enum ErrorModes : uint
{
/// <summary>
/// Use the system default, which is to display all error dialog boxes.
/// </summary>
SEM_DEFAULT = 0x00000000,

/// <summary>
/// <para>
/// The system does not display the critical-error-handler message box.
/// Instead, the system sends the error to the calling process.
/// </para>
/// <para>
/// Best practice is that all applications call the process-wide <see cref="SetErrorMode"/> function
/// with a parameter of <see cref="SEM_FAILCRITICALERRORS"/> at startup.
/// This is to prevent error mode dialogs from hanging the application.
/// </para>
/// </summary>
SEM_FAILCRITICALERRORS = 0x0001,

/// <summary>
/// <para>
/// The system automatically fixes memory alignment faults and makes them invisible to the application.
/// It does this for the calling process and any descendant processes.
/// This feature is only supported by certain processor architectures.
/// </para>
/// <para>
/// After this value is set for a process, subsequent attempts to clear the value are ignored.
/// </para>
/// </summary>
SEM_NOALIGNMENTFAULTEXCEPT = 0x0004,

/// <summary>
/// The system does not display the Windows Error Reporting dialog.
/// </summary>
SEM_NOGPFAULTERRORBOX = 0x0002,

/// <summary>
/// The OpenFile function does not display a message box when it fails to find a file.
/// Instead, the error is returned to the caller. This error mode overrides the OF_PROMPT flag.
/// </summary>
SEM_NOOPENFILEERRORBOX = 0x8000,
}
}
}
10 changes: 10 additions & 0 deletions src/Kernel32/Kernel32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,16 @@ public static extern unsafe int CompareFileTime(
[Friendly(FriendlyFlags.In)] FILETIME* lpFileTime1,
[Friendly(FriendlyFlags.In)] FILETIME* lpFileTime2);

/// <summary>
/// Controls whether the system will handle the specified types of serious errors or whether the process will handle them.
/// </summary>
/// <param name="uMode">The process error mode.</param>
/// <returns>
/// The return value is the previous state of the error-mode bit flags.
/// </returns>
[DllImport(api_ms_win_core_errorhandling_l1_1_1)]
public static extern ErrorModes SetErrorMode(ErrorModes uMode);

/// <summary>
/// Closes a file search handle opened by the FindFirstFile, FindFirstFileEx, FindFirstFileNameW,
/// FindFirstFileNameTransactedW, FindFirstFileTransacted, FindFirstStreamTransactedW, or FindFirstStreamW functions.
Expand Down
7 changes: 7 additions & 0 deletions src/Kernel32/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
PInvoke.Kernel32.ErrorModes
PInvoke.Kernel32.ErrorModes.SEM_DEFAULT = 0 -> PInvoke.Kernel32.ErrorModes
PInvoke.Kernel32.ErrorModes.SEM_FAILCRITICALERRORS = 1 -> PInvoke.Kernel32.ErrorModes
PInvoke.Kernel32.ErrorModes.SEM_NOALIGNMENTFAULTEXCEPT = 4 -> PInvoke.Kernel32.ErrorModes
PInvoke.Kernel32.ErrorModes.SEM_NOGPFAULTERRORBOX = 2 -> PInvoke.Kernel32.ErrorModes
PInvoke.Kernel32.ErrorModes.SEM_NOOPENFILEERRORBOX = 32768 -> PInvoke.Kernel32.ErrorModes
PInvoke.Kernel32.FILETIME.FILETIME(System.DateTime dateTime) -> void
PInvoke.Kernel32.HandleFlags
PInvoke.Kernel32.HandleFlags.HANDLE_FLAG_INHERIT = 1 -> PInvoke.Kernel32.HandleFlags
Expand Down Expand Up @@ -90,6 +96,7 @@ static extern PInvoke.Kernel32.GetProcessId(System.IntPtr Process) -> int
static extern PInvoke.Kernel32.GetProcessTimes(PInvoke.Kernel32.SafeObjectHandle handle, out PInvoke.Kernel32.FILETIME creation, out PInvoke.Kernel32.FILETIME exit, out PInvoke.Kernel32.FILETIME kernel, out PInvoke.Kernel32.FILETIME user) -> bool
static extern PInvoke.Kernel32.GetStartupInfo(PInvoke.Kernel32.STARTUPINFO* lpStartupInfo) -> void
static extern PInvoke.Kernel32.ReadProcessMemory(System.IntPtr hProcess, void* lpBaseAddress, void* lpBuffer, System.UIntPtr nSize, out System.UIntPtr lpNumberOfBytesRead) -> bool
static extern PInvoke.Kernel32.SetErrorMode(PInvoke.Kernel32.ErrorModes uMode) -> PInvoke.Kernel32.ErrorModes
static extern PInvoke.Kernel32.SetHandleInformation(System.Runtime.InteropServices.SafeHandle hObject, PInvoke.Kernel32.HandleFlags dwMask, PInvoke.Kernel32.HandleFlags dwFlags) -> bool
static extern PInvoke.Kernel32.SetLastError(uint dwErrCode) -> void
static extern PInvoke.Kernel32.SystemTimeToFileTime(PInvoke.Kernel32.SYSTEMTIME* lpSystemTime, PInvoke.Kernel32.FILETIME* lpFileTime) -> bool
Expand Down