how can i refine the hresult error?

I am using the SQL server. My problem is: i do a backup with password when i try to restore this backup without password the hResult return DB_E_ERRORSINCOMMAND but i want to specify the error. How can i do this? ( here is my code)

// .h
CSession m_sessao;

WLB_CODIGO_ERRO
RepositorioOLE::ExecutarComando(const string & cmd)
{
      WLB_CODIGO_ERRO ret = WLB_OK;
      CCommand< CDynamicAccessor > comando;
      HRESULT hResult;

      hResult = comando.Open(m_sessao,cmd.c_str());
      if( FAILED( hResult ) )
      {
            _com_error e(hResult);
            TRACE(e.ErrorMessage());
            LOG1("Codigo de erro no comando SQL (", cmd << ") : " << e.ErrorMessage());
            ret = WLB_ERRO_EXECUCAO_COMANDO_SQL;
      }
      comando.Close();
      comando.ReleaseCommand();

      return ret;
}

PS: I would be very grateful if somebody could help me
emu10k1Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
drichardsConnect With a Mentor Commented:
You can use CDBErrorInfo to get access to all the error information.  It is a pretty thin wrapper around the error API and is found in atldbcli.h along with the other OLE DB classes.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/vcrefcdberrorinfo.asp

You use GetErrorRecords to retrieve the number of error records and then call one of the error info retrieval methods to get the information for each error record.  GetAllErrorInfo lets you get directly to the error data without having to explicitly get the IErrorInfo interface and then query for the error data.  You do this in addition to recording the HRESULT error as you are already doing.
0
 
SteHCommented:
Have you had a look at the following sample:
http://support.microsoft.com/default.aspx?scid=kb;en-us;122957
0
 
itsmeandnobodyelseCommented:
I would assume that

HRESULT GetErrorInfo(

DWORD  dwReserved,      
  IErrorInfo  **pperrinfo  
)


is the function you need, though i never used it myself.

Regards, Alex
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
jkrCommented:
Shouldn't that be

HRESULT SetErrorInfo(
  DWORD  dwReserved,    
  IErrorInfo  *perrinfo  
);

?
0
 
itsmeandnobodyelseCommented:
MSDN says

   pperrinfo Pointer to a pointer to an error object.

Obtains the error information pointer set by the previous call to SetErrorInfo in the current logical thread.

So, you'll get a pointer and not a filled struct.

Regards, Alex



0
 
emu10k1Author Commented:
The error that i can get was "RESTORE DATABASE is terminating abnormally."( In p->GetDescription()). This is the last line of the error in query analyzer. Is there any way to get the others?
0
 
itsmeandnobodyelseCommented:
@jkr

I didn't see that you posted SetErrorInfo and not GetErrorInfo. For SetErrorInfo it's a pointer, but i assume emu10k1 wants to get the error and not to set ????

Regards, Alex


0
 
itsmeandnobodyelseCommented:
You have to use IErrorRecords::GetErrorInfo to get all errors.

MSDN says:
----------------------------------------------------------------------------------------------------------------------
IErrorRecords
IErrorRecords is defined by OLE DB. It is used to add and retrieve records in an OLE DB error object. Information is passed to and from OLE DB error objects in an ERRORINFO structure. For information about this structure, see "Error Records" in Chapter 13.

When to Implement
IErrorRecords is implemented by code in the OLE DB SDK.

When to Call
Consumers use this interface to retrieve information stored in the records of an OLE DB error object. They call QueryInterface to get a pointer to this interface after retrieving an OLE DB error object with GetErrorInfo in the Automation DLL.

Providers use this interface to add records to an OLE DB error object. If they get an existing OLE DB error object with GetErrorInfo in the Automation DLL, they call QueryInterface on that object to get a pointer to this interface. If they create a new OLE DB error object through a class factory or with CoCreateInstance, they request that a pointer to this interface be returned.
----------------------------------------------------------------------------------------------

So first get an IErrorRecords object by QueryInterface, call IErrorRecords::GetRecordCount() and fetch all errors by calling IErrorRecords::GetErrorInfo(..)

HRESULT GetErrorInfo (
   ULONG         ulRecordNum,
   LCID            lcid,
   IErrorInfo **      ppErrorInfo);



Regards, Alex

0
 
emu10k1Author Commented:
Alex,

      yes, i want to get the error description. I used the method that you suggested. But the error that i can get was "RESTORE DATABASE is terminating abnormally.". This is the last line of the error in query analyzer of SQLServer. Is there any way to get the others msg?

WLB_CODIGO_ERRO
RepositorioOLE::ExecutarComando(const string & cmd)
{
      WLB_CODIGO_ERRO ret = WLB_OK;
      CCommand< CDynamicAccessor > comando;
      IErrorInfo* pei;
      BSTR teste;
      HRESULT hResult;
          
      hResult = comando.Open(m_sessao,cmd.c_str());
      GetErrorInfo(0, &pei);
      pei->GetDescription(&teste);
      if( FAILED( hResult ) )
      {
            _com_error e(hResult);
            TRACE(e.ErrorMessage());
            LOG1("Codigo de erro no comando SQL (", cmd << ") : " << e.ErrorMessage());
            ret = WLB_ERRO_EXECUCAO_COMANDO_SQL;
      }
      comando.Close();
      comando.ReleaseCommand();

      return ret;
}
   
0
 
itsmeandnobodyelseCommented:
The answer was prior to the question ... ;-)

Look above!

Regards, Alex
0
 
itsmeandnobodyelseCommented:
Here an example of MSDN:

////////////////////////////////////////////////////////////////////////
// HRESULT GetErrorRecords //
// Get the error message generated by an OLE DB object
///////////////////////////////////////////////////////////////////////////

HRESULT GetErrorRecords(ULONG* pcRecords, IErrorRecords** ppIErrorRecords)
{
  ASSERT(pcRecords && ppIErrorRecords);
  HRESULT hr;  
  //NULL output params
  *pcRecords = 0;
  *ppIErrorRecords = NULL;  
  ISupportErrorInfo* pISupportErrorInfo = NULL;
  IErrorInfo* pIErrorInfo = NULL;  
  //See if this interface supports ErrorInfo
  //If not there is no reason to display any error
  if((hr = GetErrorInfo(0, &pIErrorInfo))==S_OK && pIErrorInfo)
  {
       TESTC(hr = pIErrorInfo->QueryInterface(IID_IErrorRecords, (void**)ppIErrorRecords));
       TESTC(hr = (*ppIErrorRecords)->GetRecordCount(pcRecords));
  }  
 
 CLEANUP:
       SAFE_RELEASE(pISupportErrorInfo);
       SAFE_RELEASE(pIErrorInfo);
       return hr;
}  

////////////////////////////////////////////////////////////////////////
// HRESULT GetErrorInfo //
// Get the error message generated by an OLE DB object
/////////////////////////////////////////////////////////////////////////////

HRESULT GetErrorInfo(BSTR* pBstr)
{
    ASSERT(pBstr);

    HRESULT hr;  ULONG cRecords = 0;
    IErrorRecords* pIErrorRecords = NULL;
   
    //Try to display Extened ErrorInfo
    if((hr = GetErrorRecords(&cRecords, &pIErrorRecords))==S_OK)  
    {
        //GetErrorInfo, just for the first error, (ie: 0)
        TESTC(hr = GetErrorInfo(0, pIErrorRecords, pBstr));
    }  

   CLEANUP:
    SAFE_RELEASE(pIErrorRecords);
    return hr;
}

////////////////////////////////////////////////////////////////////////
// HRESULT GetErrorInfo
//
// Get the error message generated by an OLE DB object
//
///////////////////////////////////////////////////////////////////////////

HRESULT GetErrorInfo(ULONG iRecord, IErrorRecords* pIErrorRecords, BSTR* pBstr)
{
    ASSERT(pBstr);
    HRESULT hr = S_OK;  
    IErrorInfo* pIErrorInfo = NULL;
    LCID lcid = GetSystemDefaultLCID();  
    //Get the Error Records
    if(pIErrorRecords)
    {
        //If there is ErrorInfo, GetSQLInfo for the desired record
        TESTC(hr = pIErrorRecords->GetErrorInfo(iRecord,lcid,&pIErrorInfo));  
       
        //If there was a CustomErrorObject
        if(pIErrorInfo)
            TESTC(hr = pIErrorInfo->GetDescription(pBstr));
    }  
   
CLEANUP:
   
    SAFE_RELEASE(pIErrorInfo);
    return hr;
}    

Regards, Alex
0
 
emu10k1Author Commented:
Drichards,

       Thank you very much, i got the error with CDBErrorInfo.


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.