| Store | Cart

SetErrorMode, Win32::API

From: Jeremy A <mail...@telus.net>
Sat, 27 Nov 2004 19:16:49 -0800
hi all,


i want to use Win32's SetErrorMode to disable the error popup (page fault), 
in a perl program which happens from time to time intermitently. I want to 
suppress the error dialog (which halts the process), so i can restart it 
automatically on failure...the program is a server, so this is necessary as 
there is no access to the serving computer, to be able to go and manually 
close the dialog box, hence, i need the code
in perl, so i can suppress the halt, caused by the dialog box, and restart 
the process.

SetErrorMode is a Win32::API function.

thanks in advance for any help.

-Jeremy A.

here is info i found in a forum: how do i do this in perl
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

Disabling the program crash dialog

If you don't want your program to display the standard crash dialog, you 
can disable it by setting the SEM_NOGPFAULTERRORBOX flag in the process 
error mode.

The simple-minded way is just to do

SetErrorMode(SEM_NOGPFAULTERRORBOX);

but this overwrites the previous error mode rather than augmenting it. In 
other words, you inadvertently turned off the other error modes!

Unfortunately, there is no GetErrorMode function, so you have to do a 
double-shuffle.

DWORD dwMode = SetErrorMode(SEM_NOGPFAULTERRORBOX);
SetErrorMode(dwMode | SEM_NOGPFAULTERRORBOX);

This sets the new error mode (possibly disabling some other error modes 
that had been set) and saves the previous mode. Then it sets the error mode 
the right way, adding the SEM_NOGPFAULTERRORBOX flag to the existing error 
modes.

Merging with existing error modes is important. For example, the previous 
error mode may have included SEM_NOALIGNMENTFAULTEXCEPT. If you casually 
turned that off, then the program would not longer receive automatic 
alignment fault fixups and will start crashing.

(But if you read the documentation, you'll see that 
SEM_NOALIGNMENTFAULTEXCEPT is special: The system won't let you turn it off 
once it's been turned on. Why? Because too many people were already making 
this mistake. I remember before this special rule was introduced. Programs 
were crashing left and right because they didn't do the double-set shuffle 
above; as a result, they started crashing on alignment faults. So the 
special rule had to be added. Welcome to the world of compatibility, where 
it is the operating system's duty to cover for other people's mistakes. 
Mind you, the design of the SetErrorMode function makes this mistake a very 
easy one to commit.)

Note that the error mode is a process-wide setting, not a per-thread 
setting. This means that manipulating the process error mode is not 
something you should do lightly, since it may have unintended consequences 
for other threads (which you might not have control over). For safety's 
sake, a program should set its error mode when it starts up and not mess 
with it thereafter.

Of course, if you disable the crash dialog, then you also miss out on the 
opportunity to retrieve crash reports collected by Windows Error Reporting 
so you can see where your program is crashing in the real world.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SetErrorMode

The SetErrorMode function controls whether the system will handle the 
specified types of serious errors or whether the process will handle them.


UINT SetErrorMode(
   UINT uMode
);


Parameters

uMode
[in] Process error mode. This parameter can be one or more of the following 
values.
Value Meaning
0 Use the system default, which is to display all error dialog boxes.
SEM_FAILCRITICALERRORS The system does not display the 
critical-error-handler message box. Instead, the system sends the error to 
the calling process.
SEM_NOALIGNMENTFAULTEXCEPT
64-bit Windows:  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.

After this value is set for a process, subsequent attempts to clear the 
value are ignored.
SEM_NOGPFAULTERRORBOX The system does not display the 
general-protection-fault message box. This flag should only be set by 
debugging applications that handle general protection (GP) faults 
themselves with an exception handler.
SEM_NOOPENFILEERRORBOX The system does not display a message box when it 
fails to find a file. Instead, the error is returned to the calling process.

Return Values

The return value is the previous state of the error-mode bit flags.

Remarks

Each process has an associated error mode that indicates to the system how 
the application is going to respond to serious errors. A child process 
inherits the error mode of its parent process.

Because the error mode is set for the entire process, you must ensure that 
multi-threaded applications do not set different error-mode flags. Doing so 
can lead to inconsistent error handling.

The system does not make alignment faults visible to an application. 
Therefore, specifying SEM_NOALIGNMENTFAULTEXCEPT is not an error, but the 
system is free to silently ignore the request. This means that code 
sequences such as the following are not always valid on x86 computers:
SetErrorMode(SEM_NOALIGNMENTFAULTEXCEPT);
fuOldErrorMode = SetErrorMode(0);
ASSERT(fuOldErrorMode == SEM_NOALIGNMENTFAULTEXCEPT);

Itanium:  An application must explicitly call SetErrorMode with 
SEM_NOALIGNMENTFAULTEXCEPT to have the system automatically fix alignment 
faults. The default setting is for the system to make alignment faults 
visible to an application


Requirements

Client: Requires Windows XP, Windows 2000 Professional, Windows NT 
Workstation, Windows Me, Windows 98, or Windows 95.
Server: Requires Windows Server 2003, Windows 2000 Server, or Windows NT 
Server.
Header: Declared in Winbase.h; include Windows.h.
Library: Link to Kernel32.lib.
DLL: Requires Kernel32.dll.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Recent Messages in this Thread
Jeremy A Nov 28, 2004 03:16 am
Sisyphus Nov 28, 2004 07:30 am
Messages in this thread