473,387 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

VC++, .NET and unhandled exceptions

I have a native VC++ application which runs differently
(incorrectly) if the .NET framework is installed.

A trimmed version of the app is shown below. The
intention is for the SEHHandler to catch any exceptions
which are not handled by the program. It is enabled using
the SetUnhandledExceptionFilter call.

The Pdh calls are intended to add a counter to enable
retrieval of CPU usage (not included in this trimmed
version)

The problem seems to be that after the call to
PdhAddCounter, the UnhandledExceptionFilter is reset. The
result is:
- without the call to PdhAddCounter, the program
prints 'Caught the exception OK'
- with the call to PdhAddCounter, the program quits with
a Windows dialog box

title: Microsoft Visual C++ Runtime Library
Message: Runtime Error!
Pogram: ...
This application has requested the Runtime to
terminate it in an unusual way. Please contact the
application's support team for more information.
This problem only occurs on computers with .NET installed.
#include <windows.h>
#include <stdexcept>
#include <pdh.h>
#include <tchar.h>
#include <iostream>

#define CNTR_CPU "\\Processor(_Total)\\% Processor
Time" // % of cpu in use

LONG __stdcall SEHHandler(EXCEPTION_POINTERS *pExPtrs)
{
std::cout << "Caught the exception OK" << std::endl;
return (EXCEPTION_EXECUTE_HANDLER);
}

void exceptionTest()
{
SetUnhandledExceptionFilter(SEHHandler);
static HQUERY h;
static HCOUNTER hcounter;
PdhOpenQuery (0, 0, &h) ;
PDH_STATUS pdhStatus = PdhAddCounter (h,_T(CNTR_CPU),
0, &hcounter);
PdhCloseQuery (h);
throw std::runtime_error("bad");
}

int main(int argc, char **argv)
{
exceptionTest();
return 0;
}

Any ideas?

Nov 16 '05 #1
2 3243
That repros for me too - no idea why though. I'll ask someone from MS to
take a look.

-cd

Colin Thomsen wrote:
I have a native VC++ application which runs differently
(incorrectly) if the .NET framework is installed.

A trimmed version of the app is shown below. The
intention is for the SEHHandler to catch any exceptions
which are not handled by the program. It is enabled using
the SetUnhandledExceptionFilter call.

The Pdh calls are intended to add a counter to enable
retrieval of CPU usage (not included in this trimmed
version)

The problem seems to be that after the call to
PdhAddCounter, the UnhandledExceptionFilter is reset. The
result is:
- without the call to PdhAddCounter, the program
prints 'Caught the exception OK'
- with the call to PdhAddCounter, the program quits with
a Windows dialog box

title: Microsoft Visual C++ Runtime Library
Message: Runtime Error!
Pogram: ...
This application has requested the Runtime to
terminate it in an unusual way. Please contact the
application's support team for more information.
This problem only occurs on computers with .NET installed.
#include <windows.h>
#include <stdexcept>
#include <pdh.h>
#include <tchar.h>
#include <iostream>

#define CNTR_CPU "\\Processor(_Total)\\% Processor
Time" // % of cpu in use

LONG __stdcall SEHHandler(EXCEPTION_POINTERS *pExPtrs)
{
std::cout << "Caught the exception OK" << std::endl;
return (EXCEPTION_EXECUTE_HANDLER);
}

void exceptionTest()
{
SetUnhandledExceptionFilter(SEHHandler);
static HQUERY h;
static HCOUNTER hcounter;
PdhOpenQuery (0, 0, &h) ;
PDH_STATUS pdhStatus = PdhAddCounter (h,_T(CNTR_CPU),
0, &hcounter);
PdhCloseQuery (h);
throw std::runtime_error("bad");
}

int main(int argc, char **argv)
{
exceptionTest();
return 0;
}

Any ideas?

Nov 16 '05 #2
I heard back from MS on this issue -

Apparently the phenomenon you're seeing is due to PDH DLL loading second
version of the CRT into the process, and the loading of the CRT causes the
unhandled exception filter to be reset. The variation in behavior from
machine to machine is reportedly due to different versions of PDH.DLL and/or
the CRT on those machines.

At present, it sounds like the only workaround is to be aware of the issue -
either make sure that PDH is loaded before you set the unhandled exception
filter, or call SetUnhandledExceptionFilter again after using PDH functions.

This should be addressed in a future release.

-cd

Colin Thomsen wrote:
I have a native VC++ application which runs differently
(incorrectly) if the .NET framework is installed.

A trimmed version of the app is shown below. The
intention is for the SEHHandler to catch any exceptions
which are not handled by the program. It is enabled using
the SetUnhandledExceptionFilter call.

The Pdh calls are intended to add a counter to enable
retrieval of CPU usage (not included in this trimmed
version)

The problem seems to be that after the call to
PdhAddCounter, the UnhandledExceptionFilter is reset. The
result is:
- without the call to PdhAddCounter, the program
prints 'Caught the exception OK'
- with the call to PdhAddCounter, the program quits with
a Windows dialog box

title: Microsoft Visual C++ Runtime Library
Message: Runtime Error!
Pogram: ...
This application has requested the Runtime to
terminate it in an unusual way. Please contact the
application's support team for more information.
This problem only occurs on computers with .NET installed.
#include <windows.h>
#include <stdexcept>
#include <pdh.h>
#include <tchar.h>
#include <iostream>

#define CNTR_CPU "\\Processor(_Total)\\% Processor
Time" // % of cpu in use

LONG __stdcall SEHHandler(EXCEPTION_POINTERS *pExPtrs)
{
std::cout << "Caught the exception OK" << std::endl;
return (EXCEPTION_EXECUTE_HANDLER);
}

void exceptionTest()
{
SetUnhandledExceptionFilter(SEHHandler);
static HQUERY h;
static HCOUNTER hcounter;
PdhOpenQuery (0, 0, &h) ;
PDH_STATUS pdhStatus = PdhAddCounter (h,_T(CNTR_CPU),
0, &hcounter);
PdhCloseQuery (h);
throw std::runtime_error("bad");
}

int main(int argc, char **argv)
{
exceptionTest();
return 0;
}

Any ideas?

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: terry | last post by:
Hi, Question: When using Framesets and Server.Transfer in the Application_Error event handler is there a mechanism to get the error page to display in the total view of the browser not just in...
4
by: Frank | last post by:
Hi, I made a handler for unhandled errors. But before that is executed, VB.NET gives me the standard error window. In VB6 there was a setting (errortrapping) about handling errors in the design...
5
by: Lucvdv | last post by:
Can someone explain why this code pops up a messagebox saying the ThreadAbortException wasn't handled? The first exception is reported only in the debug pane, as expected. The second (caused by...
5
by: Samuel R. Neff | last post by:
When you have an unhandled exception in vb.net how do you view the exception information in the debugger? In C# the debugger creates a local variable that points to the exception and you can...
5
by: Sam Loveridge | last post by:
Hi All. I'm hoping someone can point me in the direction of a solution to unhandled exceptions in MDI child forms causing the application to crash. I've found various articles describing a...
0
by: Colmeister | last post by:
I recently read Jason Clark's excellent article on Unhandled Exceptions (http://msdn.microsoft.com/msdnmag/issues/04/06/NET/default.aspx) and have attempted to incorporate the features he talks...
2
by: Bob | last post by:
I MUST be able to trap unhandled exceptions, bring the thread to a routine that then closes the thread on which the execption occurred without closing or affecting the other threads. Think of an...
3
by: Andreas van de Sand | last post by:
Hi everyone, I've got an application which uses a custom exception handler and to avoid unhandled exceptions I've got a try{}catch (Exception){} block around the main Application.Run(...). ...
20
by: joseph_gallagher | last post by:
Hi, I've recently ported a .Net 1.1 application to .Net 2.0 and the one new feature that is getting on my nerves is that when there is an unhandled exception the application completely crashes,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.