Discussion:
Redirect the CRT assertions to a text file
(too old to reply)
Michelle
2010-08-13 21:45:03 UTC
Permalink
I am writing a C program that uses a file to capture the Microsoft CRT
assertions. The related code is like below:

=====================================================
invalid_parameter_handler oldHandler, newHandler;
#if defined(DEBUG)
HANDLE hLogFile;

hLogFile = CreateFile(L"c:\\assertlog.txt", GENERIC_WRITE,
FILE_SHARE_WRITE|FILE_SHARE_READ, NULL, OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
newHandler = invalidParamHandler;
oldHandler = _set_invalid_parameter_handler(newHandler);

_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_ASSERT, hLogFile);
#endif
=====================================================

The above code works when I run my program as a console application. However
when I run my program as a Windows service then nothing is captured in the
c:\assertlog.txt even when an assertion should happen.

Does anyone know what changes should be made for a Windows service to
redirect the CRT assertions to a text file?

Your response will be very appreciated!
Leo Davidson
2010-08-14 06:51:35 UTC
Permalink
Post by Michelle
The above code works when I run my program as a console application. However
when I run my program as a Windows service then nothing is captured in the
c:\assertlog.txt even when an assertion should happen.
Does the account running the service have access to create files in C:
\ ?

If C:\assertlog.txt already exists and was created by you running it
at the console, does the account have access to modify or overwrite
that file? (The required access may be different to the access
required to create a new file.)

Is the handler actually being run in the service? Maybe your service-
startup code doesn't actually install it.

If it is being run, what error code does CreateFile set? You can check
both these things by attaching a debugger to the service.

You have forgotten to close the file after calling _CrtSetReportFile,
which could be part of the problem.
Michelle
2010-08-16 17:13:03 UTC
Permalink
Thanks for the reply, Leo.

I found the problem. In my code when the program is running as a service
then our own debug hood is used by calling _CrtSetReportHook(). If I remove
the _CrtSetReportHook() API call, then CRT asserts are captured in the
C:\asserlog.txt file.

So my question is, does the _CrtSetReportHook() code in my program
overwrites the _CrtSetReportFile() code? I am really new to the debugging
techniques of CRT libraries. But I do need to sort this out.

Loading...