program will collapse when encounter failed HRESULT

program will collapse when encounter failed HRESULT

Postby bcbooo » 15 March 2013, 08:27

This question is out of CAPE-OPEN standard scope, but make me very puzzled.

Recently I tried to develop a property package, the development environment is visual C++ 2005, I create a ATL dll project, and part of the code like the following:
**************************
STDMETHODIMP CPropertyPackage::GetPhaseList(VARIANT * phaseLabels, VARIANT * stateOfAggregation, VARIANT * keyCompoundId)
{
if ((!phaseLabels)||(!stateOfAggregation)||(!keyCompoundId)) return E_POINTER;
............
............
return NOERROR;
}
***************************

This function has been finished, so I need to test this, I create a MFC program "DllTester" like the following:
*******************
//pCapeThermoPhases has been initialized
//phaseLabels, stateOfAggregation and keyCompoundId are all VARIANT pionter
phaseLabels=NULL; // I set the phaseLabels=NULL to test the dll, of course it will cause an error !
HRESULT hr=pCapeThermoPhases->GetPhaseList(phaseLabels, stateOfAggregation,keyCompoundId) ;
if(FAILED(hr)) AfxMessageBox(_T("GetPhaseList failed !!!"));
******************

The result is : The DllTester popup a warning messagebox ,then collapses and doesn't come into the
" if(FAILED(hr)) AfxMessageBox(_T("GetPhaseList failed !!!")); "
the warning's caption is "Microsoft Visual C++ Debug Library" and context is "This application has requested the Runtime to terminate it in an unusual way."

Then I finded if the interface function return E_FAIL, E_POINTER, E_UNEXPECTED even ECapeInvalidArgumentHR and so on , the DllTester all will collapse. So I want to know how to prevent the main program collapse when the returned HRESULT of the interface function is error?

In the Microsoft's MSDN, there are some remarks :
The error code is a unique number that is assigned to represent the exception. Each exception is mapped to a distinct HRESULT. When managed code throws an exception, the runtime passes the HRESULT to the COM client. When unmanaged code returns an error, the HRESULT is converted to an exception, which is then thrown by the runtime.
I find the visual C++ is unmanaged code, whether this reason causes the collapsion ?
bcbooo
 
Posts: 66
Joined: 22 November 2012, 06:41
Location: China

Re: program will collapse when encounter failed HRESULT

Postby jasper » 15 March 2013, 10:01

C++ is not necessarily unmanaged, it depends on how you set up your project.

Code: Select all
//pCapeThermoPhases has been initialized
//phaseLabels, stateOfAggregation and keyCompoundId are all VARIANT pionter
phaseLabels=NULL; // I set the phaseLabels=NULL to test the dll, of course it will cause an error !
HRESULT hr=pCapeThermoPhases->GetPhaseList(phaseLabels, stateOfAggregation,keyCompoundId) ;
if(FAILED(hr)) AfxMessageBox(_T("GetPhaseList failed !!!"));


The above code you say crashes, only if an HRESULT that is non successful is returned? It runs ok if the HRESULT does not indicate an error?

It is hard to say what happens from the little code snippet you post, but I would try the following. Add a line break as such:

Code: Select all
//pCapeThermoPhases has been initialized
//phaseLabels, stateOfAggregation and keyCompoundId are all VARIANT pionter
phaseLabels=NULL; // I set the phaseLabels=NULL to test the dll, of course it will cause an error !
HRESULT hr=pCapeThermoPhases->GetPhaseList(phaseLabels, stateOfAggregation,keyCompoundId) ;
if(FAILED(hr))
 AfxMessageBox(_T("GetPhaseList failed !!!"));


now you can set a break point at the line containing AfxMessageBox. See if the code reaches this point. If so, step into the function.

If the code does not reach this point, something goes wrong before that. Set a breakpoint on the line that contains GetPhaseList and step into that function to see what happens.
User avatar
jasper
 
Posts: 1128
Joined: 24 October 2012, 15:33
Location: Spain

Re: program will collapse when encounter failed HRESULT

Postby bcbooo » 16 March 2013, 06:13

jasper wrote:C++ is not necessarily unmanaged, it depends on how you set up your project.

[code]
//pCapeThermoPhases has been initialized
//phaseLabels, stateOfAggregation and keyCompoundId are all VARIANT pionter
phaseLabels=NULL; // I set the phaseLabels=NULL to test the dll, of course it will cause an error !


jasper,

I have found why my client promgram will collapse when the dll return ERROR, because in the "stdafx.h" file , I shuould import the tlb file like the following:

#import "CAPE-OPENv1-1-0.tlb" raw_interfaces_only, raw_native_types, no_namespace, named_guids, auto_search

I compiled the code like the following before:

#import "F:\\ChemWork\\ChemWork\\CAPE-OPENv1-1-0.tlb" no_namespace

It's wrong ! It will raises the main program collapse when server dll return E_FAIL ,even if my code is normal.

Thank you very much, jasper !
bcbooo
 
Posts: 66
Joined: 22 November 2012, 06:41
Location: China


Return to Thermodynamic components

Who is online

Users browsing this forum: No registered users and 1 guest