Solved

how can i refine the hresult error?

Posted on 2004-08-24
12
890 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

746 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

13 Experts available now in Live!

Get 1:1 Help Now