Format
#include <stdio.h> int _set_crt_msg_handle(int fh);
Language Level: Extension
_set_crt_msg_handle changes the file handle to
which runtime messages are sent, which is usually file handle 2,
to fh. Runtime messages include exception handling
messages and output from debug memory management routines.
Use _set_crt_msg_handle to trap runtime message
output in applications where handle 2 is not defined, such as
Presentation
Manager applications for OS/2, or
Windows graphical user interface
applications for Windows.
The file handle fh must be a writable file or pipe handle. Set fh only for the current library environment.
Return Value
_set_crt_msg_handle returns the handle to
be used for runtime message output. If an handle that is not
valid is passed as an argument, it is ignored and no change takes
place.
Example
This example causes an exception by
dereferencing a null pointer and uses _set_crt_msg_handle to send
the exception messages to the file _scmhdl.out.
#include <sys\stat.h> #include <io.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h>
int main(void)
{
int fh;
char *p = NULL;
if (-1 == (fh = open("_scmhdl.out", O_CREAT|O_TRUNC|O_RDWR,
S_IREAD|S_IWRITE))) {
perror("Unable to open the file _scmhdl.out.");
exit(EXIT_FAILURE);
}
/* change file handle where messages are sent */
if (fh != _set_crt_msg_handle(fh)) {
perror("Could not change massage output handle.");
exit(EXIT_FAILURE);
}
*p = 'x'; /* cause an exception, output should be in _scmhdl.out */
if (-1 == close(fh)) {
perror("Unable to close _scmhdl.out.");
exit(EXIT_FAILURE);
}
return 0;
/****************************************************************************
Running this program would cause an exception to occur,
the file _scmhdl.out should contain the exception messages similar to :
Exception = c0000005 occurred at EIP = 10068. ****************************************************************************/ }
![]()
open -- Open File
fileno --
Determine File Handle
_sopen -- Open
Shared File
<stdlib.h>