Solved

how can i refine the hresult error?

Posted on 2004-08-24
12
897 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:emu10k1
12 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 11880484
Have you had a look at the following sample:
http://support.microsoft.com/default.aspx?scid=kb;en-us;122957
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11880529
I would assume that

HRESULT GetErrorInfo(

DWORD  dwReserved,      
  IErrorInfo  **pperrinfo  
)


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

Regards, Alex
0
 
LVL 86

Expert Comment

by:jkr
ID: 11882010
Shouldn't that be

HRESULT SetErrorInfo(
  DWORD  dwReserved,    
  IErrorInfo  *perrinfo  
);

?
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11882056
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
 

Author Comment

by:emu10k1
ID: 11882070
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11882098
@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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11882330
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
 

Author Comment

by:emu10k1
ID: 11882391
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11882480
The answer was prior to the question ... ;-)

Look above!

Regards, Alex
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11882768
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
 
LVL 19

Accepted Solution

by:
drichards earned 500 total points
ID: 11884714
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
 

Author Comment

by:emu10k1
ID: 11891576
Drichards,

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


0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question