Somehow I don't think it crashes because you return E_FAIL.
your code in FinalConstruct.
Hello Jason,
The problem is that the application crashes, even before the control
returns to VB. So, error handling in VB is not an option.
I have analysed the existing code a bit more. I have found that the
Application's Core class has implemented DllGetClassObject function. In
this function, it returns the Interface of a Generic class which in
turn is a class inheriting the IClassFactory interface. Lets call this
class CAppCoClass.
--------------------------------------------------------------------------------------------------------------------
STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
{
HRESULT hRes = E_OUTOFMEMORY;
if ( rclsid == CLSID_ClassName)
{
CAppCoClass *appFactory = new CAppCoClass;
if ( appFactory == NULL)
{
return E_OUTOFMEMORY;
}
else
{
hRes = appFactory->QueryInterface(riid,ppv);
return hRes;
}
}
}
-------------------------------------------------------------------------------------------------------------------------
Here ClassName is the class which we intend to instantiate. In the
CreateInstance of class CAppCoClass,
-------------------------------------------------------------------------------------------------------------------------
HRESULT __stdcall CAppCoClass::CreateInstance(IUnknown* pUnknownOuter,
const IID& iid,
void** ppv)
{
if (pUnknownOuter != NULL)
{
return CLASS_E_NOAGGREGATION ;
}
//
// Create an instance of the component.
//
CComObject<CClassName> * pObject = NULL;
HRESULT hr = CComObject<CClassName>::CreateInstance(&pObject);
if ( SUCCEEDED(hr))
{
pObject->AddRef();
hr = pObject->QueryInterface(iid, ppv);
hr = pObject->Release();
return hr;
}
else
{
return hr;
}
}
-------------------------------------------------------------------------------------------------------------------------
So, if there is an error in the CreateInstance of CClassName, it should
return the Error along with the Error Code. But it gives an "Unhandled
Exception" error with the Description as "Access Violation". After this
the application crashes.
Please give any pointers to the possible cause for error here.
Thank You,
Shreyas
Jason Newell wrote:
What happens when you try this?
On Error Resume Next
Public appObj As DLLName.ClassName
Set appObj = New DLLName.ClassName
If Not (appObj Is Nothing) Then
'Success
Else
'Failed
End If
Jason Newell
shreyas.saxena@gmail.com wrote:
Hello all,
My project has an ATL COM backend with a VB front end. The COM
component is in the form of an ATL COM dll created using VC++. Now,
when my application is launched, which is in the form of a VB exe, it
instantiates the COM dll object. If we encounter an error during
instantiation in the FinalConstruct (constructor of the class), the COM
dll returns E_FAIL, which is an OLE error, and should be handled in the
VB. However, the application crashes, without giving an error.
Here is a code snippet, in which we are returning the error in case an
Application file important for the running of the application is not
found: -
****************************************************************************************************
HRESULT CClassName::FinalConstruct()
{
...
if( (_access( _bstr_t(m_bstrApplicationFile), 0 )) == -1 ) // 0
to check existence only
{
return E_FAIL;
}
...
}
****************************************************************************************************
From VB, we are instantiating the class ClassName like this :-
****************************************************************************************************
Public appObj As DLLName.ClassName
Set appObj = New DLLName.ClassName
****************************************************************************************************
The application crashes during the execution of Set appObj statement.
Please provide pointers to resolving the issue.
Thank You