Solved

how can i refine the hresult error?

Posted on 2004-08-24
12
894 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

910 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now