HRESULT
ExecStoredProc(IN ADOCommand *pCmd,OUT ADORecordset **ppRecordset)
{
HRESULT hr;
if( pCmd == NULL || ppRecordset == NULL )
return E_POINTER;
*ppRecordset = NULL;
Here we are putting the command text:
BSTR wszCmd = ::SysAllocString( L"proc_refcursor_1" );
pCmd->put_CommandText( wszCmd );
::SysFreeString( wszCmd );
Here we are putting the command type:
hr = pCmd->put_CommandType( adCmdStoredProc );
Here we are getting ADOParameters object for appending:
ADOParameters *pParams = NULL;
hr = pCmd->get_Parameters( &pParams );
if( FAILED( hr ) )
return hr;
BSTR wszParamName;
This is the 1st OUT REFCURSOR:
wszParamName = ::SysAllocString( L"OutRefcursor1" );
This is an OUT parameter. We need to allocate a buffer as to tell
ADO that we have memory allocated for OUT parameter:
VARIANT varOutRefcursor1;
::VariantInit( &varOutRefcursor1 );
varOutRefcursor1.vt = VT_BYREF|VT_I1;
varOutRefcursor1.pcVal = (CHAR *)::CoTaskMemAlloc( sizeof(CHAR)*31 );
ADOParameter *pOutRefcursor1 = NULL;
hr = pCmd->CreateParameter( wszParamName,
dLongVarChar,
adParamOutput,
sizeof(CHAR)*31,
varOutRefcursor1,
&pOutRefcursor1 );
SysFreeString( wszParamName );
if( FAILED( hr ) )
{
SAFE_RELEASE( pParams );
return hr;
}
This is Query IDispatch interface for appending:
IDispatch *pDispOutRefcursor1 = NULL;
pOutRefcursor1->QueryInterface( IID_IDispatch, (void **)&pDispOutRefcursor1 );
SAFE_RELEASE( pOutRefcursor1 );
/* Append the parameter */
hr = pParams->Append( pDispOutRefcursor1 );
SAFE_RELEASE( pDispOutRefcursor1 );
if( FAILED( hr ) )
{
SAFE_RELEASE( pParams );
return hr;
}
This is the 2nd IN/OUT INTEGER:
wszParamName = ::SysAllocString( L"InOutInteger2" );
VARIANT varInOutInteger2;
::VariantInit( &varInOutInteger2 );
varInOutInteger2.vt = VT_I4;
varInOutInteger2.lVal = 125;
ADOParameter *pInOutInteger2 = NULL;
hr = pCmd->CreateParameter( wszParamName,
adInteger,
adParamInputOutput,
sizeof(LONG),
varInOutInteger2,
&pInOutInteger2 );
SysFreeString( wszParamName );
if( FAILED( hr ) )
{
SAFE_RELEASE( pParams );
return hr;
}
This is Query IDispatch interface for appending:
IDispatch *pDispInOutInteger2 = NULL;
pInOutInteger2->QueryInterface( IID_IDispatch, (void **)&pDispInOutInteger2 );
SAFE_RELEASE( pInOutInteger2 );
Append the parameter:
hr = pParams->Append( pDispInOutInteger2 );
SAFE_RELEASE( pDispInOutInteger2 );
if( FAILED( hr ) )
{
SAFE_RELEASE( pParams );
return hr;
}
This is 3rd OUT REFCURSOR():
wszParamName = ::SysAllocString( L"OutRefcursor3" );
This is an OUT parameter. We need to allocate a buffer so as to tell
ADO that we have memory allocated for OUT parameter:
VARIANT varOutRefcursor3;
::VariantInit( &varOutRefcursor3 );
varOutRefcursor3.vt = VT_BYREF|VT_I1;
varOutRefcursor3.pcVal = (CHAR *)::CoTaskMemAlloc( sizeof(CHAR)*31 );
ADOParameter *pOutRefcursor3 = NULL;
hr = pCmd->CreateParameter( wszParamName,
adLongVarChar,
adParamOutput,
sizeof(CHAR)*31,
varOutRefcursor3,
&pOutRefcursor3 );
SysFreeString( wszParamName );
if( FAILED( hr ) )
{
SAFE_RELEASE( pParams );
return hr;
}
This is Query IDispatch interface for appending:
IDispatch *pDispOutRefcursor3 = NULL;
pOutRefcursor3->QueryInterface( IID_IDispatch, (void **)&pDispOutRefcursor3 );
SAFE_RELEASE( pOutRefcursor3 );
Append the parameter:
hr = pParams->Append( pDispOutRefcursor3 );
SAFE_RELEASE( pDispOutRefcursor3 );
if( FAILED( hr ) )
{
SAFE_RELEASE( pParams );
return hr;
}
This is the 4th OUT NUMERIC:
wszParamName = ::SysAllocString( L"OutNumeric4" );
This is an OUT parameter. We need to allocate a buffer so as to tell
ADO that we have memory allocated for OUT parameter:
VARIANT varOutNumeric4;
::VariantInit( &varOutNumeric4 );
varOutNumeric4.vt = VT_BYREF|VT_DECIMAL;
varOutNumeric4.pdecVal = (DECIMAL *)::CoTaskMemAlloc( sizeof(DECIMAL) );
ADOParameter *pOutNumeric4 = NULL;
hr = pCmd->CreateParameter( wszParamName,
adNumeric,
adParamOutput,
sizeof(DECIMAL),
varOutNumeric4,
&pOutNumeric4 );
SysFreeString( wszParamName );
if( FAILED( hr ) )
{
SAFE_RELEASE( pParams );
return hr;
}
This is the Query IDispatch interface for appending:
IDispatch *pDispOutNumeric4 = NULL;
pOutNumeric4->QueryInterface( IID_IDispatch, (void **)&pDispOutNumeric4 );
SAFE_RELEASE( pOutNumeric4 );
Append the parameter:
hr = pParams->Append( pDispOutNumeric4 );
SAFE_RELEASE( pDispOutNumeric4 );
if( FAILED( hr ) )
{
SAFE_RELEASE( pParams );
return hr;
}
This is the 5th OUT REFCURSOR:
wszParamName = ::SysAllocString( L"OutRefcursor5" );
This is an OUT parameter. We need to allocate a buffer so as to tell
ADO that we have memory allocated for OUT parameter:
VARIANT varOutRefcursor5;
::VariantInit( &varOutRefcursor5 );
varOutRefcursor5.vt = VT_BYREF|VT_I1;
varOutRefcursor5.pcVal = (CHAR *)::CoTaskMemAlloc( sizeof(CHAR)*31 );
ADOParameter *pOutRefcursor5 = NULL;
hr = pCmd->CreateParameter( wszParamName,
adLongVarChar,
adParamOutput,
sizeof(CHAR)*31,
varOutRefcursor5,
&pOutRefcursor5 );
SysFreeString( wszParamName );
if( FAILED( hr ) )
{
SAFE_RELEASE( pParams );
return hr;
}
This is the Query IDispatch interface for appending:
IDispatch *pDispOutRefcursor5 = NULL;
pOutRefcursor5->QueryInterface( IID_IDispatch, (void **)&pDispOutRefcursor5 );
SAFE_RELEASE( pOutRefcursor5 );
Append the parameter here:
hr = pParams->Append( pDispOutRefcursor5 );
SAFE_RELEASE( pDispOutRefcursor5 );
if( FAILED( hr ) )
{
SAFE_RELEASE( pParams );
return hr;
}
This is for the 6th IN/OUT VARCHAR:
wszParamName = ::SysAllocString( L"InOutVarchar6" );
OLECHAR szVal[255];
::memset( szVal, 0, sizeof(OLECHAR)*255 );
::lstrcpyW( szVal, L"Hello" );
VARIANT varInOutVarchar6;
::VariantInit( &varInOutVarchar6 );
varInOutVarchar6.vt = VT_BSTR;
varInOutVarchar6.bstrVal = ::SysAllocStringLen( szVal, 255 );
ADOParameter *pInOutVarchar6 = NULL;
hr = pCmd->CreateParameter( wszParamName,adVarChar,adParamInputOutput,255,varInOutVarchar6,&pInOutVarchar6 );
SysFreeString( wszParamName );
if( FAILED( hr ) )
{
SAFE_RELEASE( pParams );
return hr;
}
This is the Query IDispatch interface for appending:
IDispatch *pDispInOutVarchar6 = NULL;
pInOutVarchar6->QueryInterface( IID_IDispatch, (void **)&pDispInOutVarchar6 );
SAFE_RELEASE( pInOutVarchar6 );
Append the parameter here:
hr = pParams->Append( pDispInOutVarchar6 );
SAFE_RELEASE( pDispInOutVarchar6 );
if( FAILED( hr ) )
{
SAFE_RELEASE( pParams );
return hr;
}