Can I save ADODB.Command into a file?



I want to save ADODB.Command into a file, send this file to another machine
via webservice(gSOAP), the remote computer loads ADODB.Command from the file
and execute it. I don't need to serialize ADODB.Connection.
Any idea on this?
Here is my code snippet that does not work. CoMarshalInterface on
ADODB.Command returns 0x80004002.

I also tried to persist ADODB.Parameters, but it seems that Parameter.value
is not persisted, the persisted file is 226 bytes, but I have stored a 12k
bytes into the blob parameter.
//#include <windows.h>
#include <iostream>
#include <fstream>
#include <comdef.h>
#include <atlcomcli.h>
#import "C:\PROGRA~1\COMMON~1\System\ado\msado15.dll" rename( "EOF",
"adoEOF" )
struct InitOle
{
InitOle() { ::CoInitialize(NULL); }
~InitOle() { ::CoUninitialize(); }
} _init_InitOle_;
void print_properties(LPCTSTR name, ADODB::PropertiesPtr Properties)
{
long prop_count = Properties->GetCount();
printf("%s property count = %d\n",name,prop_count);
for(long i=0;i<prop_count;i++)
{
printf("%s property
[%d]:%s\n",name,i,(LPCSTR)Properties->GetItem(i)->Name);
}
}
void marshal_to_file(IUnknown* pDisp,REFIID iid, const char* file_name)
{
DeleteFile(file_name);
CComPtr<IStream> pStream;
HRESULT hr = CreateStreamOnHGlobal(0, TRUE, &pStream);
hr = CoMarshalInterface(pStream, iid, pDisp,
MSHCTX_DIFFERENTMACHINE,NULL,
MSHLFLAGS_TABLESTRONG //MSHLFLAGS_NORMAL
);
std::cout<<"CoMarshalInterface return "<<std::hex<<hr<<std::endl<<std::dec;
if(pStream && SUCCEEDED(hr))
{
LARGE_INTEGER li;
li.QuadPart =0;
pStream->Seek( li,STREAM_SEEK_SET,NULL);
std::ofstream file;
file.open(file_name);
char buf[1024];
ULONG uread = 0;
ULONG total_read = 0;
while(true)
{
uread = 0;
hr = pStream->Read(buf,sizeof(buf),&uread);
if(FAILED(hr) || uread <= 0)
{
std::cout<<"total number of bytes:"<<total_read<<std::endl;
break;
}
if(uread>0)
{
total_read += uread;
file.write(buf, uread);
}

}
file.flush();
file.close();
hr = CoReleaseMarshalData(pStream);
std::cout<<"CoReleaseMarshalData return "<<std::hex<<hr<<std::endl;
}
}

HRESULT unmarshal_from_file(REFIID iid, LPVOID* ppvoid, const char*
file_name )
{
CComPtr<IStream> pStream;
HRESULT hr = CreateStreamOnHGlobal(0, TRUE, &pStream);
std::ifstream file;
file.open(file_name);
char buf[1024];
size_t num = 0;
size_t total = 0;
while(true)
{
file.read(buf, sizeof(buf));
num = file.gcount();
if(num>0)
{
pStream->Write(buf,num,NULL);
total += num;
}
else
break;
}
std::cout<<__FUNCTION__<<" total bytes:"<<std::dec<<total<<std::endl;
file.close();
LARGE_INTEGER li;
li.QuadPart =0;
pStream->Seek( li,STREAM_SEEK_SET,NULL);
hr = CoUnmarshalInterface(pStream,iid,ppvoid);
std::cout<<"CoUnmarshalInterface return
"<<std::hex<<hr<<std::endl<<std::dec;
return hr;
}
void marshal_cmd(ADODB::_CommandPtr pcmd)
{
//ADODB::_ConnectionPtr pconn = pcmd->ActiveConnection;
//pcmd->ActiveConnection = NULL;
marshal_to_file(pcmd,__uuidof(ADODB::Command),"c:\\command.dat");
//pcmd->ActiveConnection = pconn;
}
VOID PrintProviderError(ADODB::_ConnectionPtr pConnection)
{
ADODB::ErrorPtr pErr = NULL;
long nCount = 0;
long i = 0;
if( (pConnection->Errors->Count) > 0)
{
nCount = pConnection->Errors->Count;
for(i = 0; i < nCount; i++)
{
pErr = pConnection->Errors->GetItem(i);
printf("\n\t Error number: %x\t%s", pErr->Number,
(LPCSTR)pErr->Description);
}
}
}
int main(int argc, char* argv[])
{
ADODB::_ConnectionPtr Conn1;
ADODB::_CommandPtr Cmd1;
ADODB::_ParameterPtr oldParam= NULL;
ADODB::_ParameterPtr inParam=NULL;
ADODB::_ParameterPtr blobParam=NULL;
int line_number = 0;
_variant_t vtEmpty (DISP_E_PARAMNOTFOUND, VT_ERROR);
_variant_t vtEmpty2 (DISP_E_PARAMNOTFOUND, VT_ERROR);
_bstr_t bstrConnect="Provider=OraOLEDB.Oracle;Data Source=orcl;User
Id=system;Password=oracle;";
//create table USERPHOTO(USERNAME varchar(50),OLD varchar(20),PHOTO blob);
_bstr_t bstrCreate ( "CREATE OR REPLACE PROCEDURE TEST_BLOB ( "
"p_USERNAME in CHAR,p_OLD in CHAR,p_PHOTO in BLOB default empty_blob() ) "
"as "
"begin "
"begin "
"insert into USERPHOTO( "
"USERNAME,OLD,PHOTO) "
"values (p_USERNAME,p_OLD,p_PHOTO); "
"commit; "
"end; "
"end TEST_BLOB;" );
_bstr_t bstrSP("{CALL TEST_BLOB(?,?,?)}" );
try
{
_variant_t varOptional(DISP_E_PARAMNOTFOUND,VT_ERROR);
ADODB::_StreamPtr adostream;
adostream.CreateInstance(_T("ADODB.Stream"));
adostream->Type = ADODB::adTypeBinary;
adostream->Open(varOptional,ADODB::adModeUnknown,
ADODB::adOpenStreamUnspecified, _T(""), _T(""));
adostream->LoadFromFile("C:\\Temp\\onega.txt");
_variant_t vReadTo = adostream->Read(ADODB::adReadAll);
long blob_size = adostream->GetSize();
adostream->Close(); line_number = __LINE__;
_bstr_t bstrEmpty;
Conn1.CreateInstance( __uuidof( ADODB::Connection ) );
Cmd1.CreateInstance( __uuidof( ADODB::Command ) );
Conn1->ConnectionString = bstrConnect;
Conn1->Open( bstrConnect, bstrEmpty, bstrEmpty, -1 );
Conn1->Execute(bstrCreate,NULL,ADODB::adCmdText);
Cmd1->ActiveConnection = Conn1;
Cmd1->CommandText = bstrSP;
Cmd1->CommandType = ADODB::adCmdText;
Conn1->Properties->Refresh(); line_number = __LINE__;
inParam =
Cmd1->CreateParameter(_bstr_t("p_USERNAME"),ADODB::adChar,ADODB::adParamInput,2,_variant_t(
"a" ));
Cmd1->Parameters->Append(inParam);
marshal_to_file(inParam,__uuidof(ADODB::_Parameter),"c:\\parameter.dat");
oldParam =
Cmd1->CreateParameter(_bstr_t("p_OLD"),ADODB::adChar,ADODB::adParamInput,8,_variant_t(
"yes" ));
Cmd1->Parameters->Append(oldParam); line_number = __LINE__;
blobParam =
Cmd1->CreateParameter(_bstr_t("p_PHOTO"),ADODB::adLongVarBinary,ADODB::adParamInput,blob_size,vReadTo);
line_number = __LINE__;
Cmd1->Parameters->Append(blobParam); line_number = __LINE__;
Cmd1->Properties->Refresh();
ADODB::PropertyPtr prop = Cmd1->Properties->GetItem("SPPrmsLOB");
prop->PutValue(_variant_t(VARIANT_TRUE,VT_BOOL)) ; line_number = __LINE__;
marshal_to_file(Cmd1->Parameters,__uuidof(ADODB::Parameters),"c:\\parameters.dat");
ADODB::ParametersPtr pParams;
HRESULT hr =
unmarshal_from_file(__uuidof(ADODB::Parameters),reinterpret_cast<void**>(&pParams),"c:\\parameters.dat");
std::cout<<"CoUnmarshalInterface return
"<<std::hex<<hr<<std::endl<<std::dec;
if(SUCCEEDED(hr))
{
std::cout<<"number of parameters :"<<pParams->Count<<std::endl;
for(short i=0;i<pParams->Count; i++)
{
std::cout<<"Parameter
name:"<<(LPCSTR)pParams->GetItem(i)->Name<<std::endl;
}
}
marshal_cmd(Cmd1); line_number = __LINE__;
//Cmd1->Execute(NULL,NULL,ADODB::adExecuteNoRecords);
prop = Cmd1->Properties->GetItem("SPPrmsLOB"); line_number = __LINE__;
prop->PutValue(_variant_t(VARIANT_FALSE,VT_BOOL)) ; line_number =
__LINE__;
Conn1->Close();
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
printf("\nCOM error occurred, Source : %s \n Description : %s
\n",(LPCSTR)bstrSource,(LPCSTR)bstrDescription);
PrintProviderError(Conn1);
printf("line number:%d\n",line_number);
}
printf("\nprogram end\n");
system("pause");
return 0;
}


.



Relevant Pages

  • Re: [RFC] New kernel-message logging API
    ... return buf; ... static char* put_dec(char *buf, unsigned long long num) ... static int skip_atoi ...
    (Linux-Kernel)
  • resource_size_t printk whinging
    ... -asmlinkage int vprintk(const char *fmt, ... num = va_arg ...
    (Linux-Kernel)
  • hello-exploit.c
    ... int main(int argc, char *argv) { ... /*The greater the NUM of strlen- NUM, ... void help; ...
    (Bugtraq)
  • Re: Newbie question
    ... license, provide either the license text or at least ... int main ... If you have a good reason to use char, do so, of course. ... Since you have #define NUM, ...
    (comp.lang.c)
  • Re: String parsing program
    ... You check that errno has not been set ... check if *endp character is nothing but white space (This must be done ... ep - num, num); ... void parse_three_sizes(const char *input) ...
    (comp.lang.c)