Board index » cppbuilder » Convert from Delphi ?

Convert from Delphi ?


2006-01-10 10:06:13 PM
cppbuilder83
Hello,
I have a code for list of all SQL-server,
can help me for converting this source to C++ ?
Best Regards,
m.t
// Source ============================
procedure ListAvailableSQLServers(Names : TStrings);
var
RSCon: ADORecordsetConstruction;
Rowset: IRowset;
SourcesRowset: ISourcesRowset;
SourcesRecordset: _Recordset;
SourcesName, SourcesType: TField;
function PtCreateADOObject(const ClassID: TGUID): IUnknown;
var
Status: HResult;
FPUControlWord: Word;
begin
asm
FNSTCW FPUControlWord
end;
Status := CoCreateInstance(
CLASS_Recordset,
nil,
CLSCTX_INPROC_SERVER or CLSCTX_LOCAL_SERVER,
IUnknown,
Result);
asm
FNCLEX
FLDCW FPUControlWord
end;
OleCheck(Status);
end;
begin
SourcesRecordset := PtCreateADOObject(CLASS_Recordset) as _Recordset;
RSCon := SourcesRecordset as ADORecordsetConstruction;
SourcesRowset := CreateComObject(ProgIDToClassID('SQLOLEDB Enumerator'))
as ISourcesRowset;
OleCheck(SourcesRowset.GetSourcesRowset(nil, IRowset, 0, nil,
IUnknown(Rowset)));
RSCon.Rowset := RowSet;
with TADODataSet.Create(nil) do
try
Recordset := SourcesRecordset;
SourcesName := FieldByName('SOURCES_NAME'); { do not localize }
SourcesType := FieldByName('SOURCES_TYPE'); { do not localize }
Names.BeginUpdate;
try
while not EOF do
begin
if (SourcesType.AsInteger = DBSOURCETYPE_DATASOURCE) and
(SourcesName.AsString <>'') then
Names.Add(SourcesName.AsString);
Next;
end;
finally
Names.EndUpdate;
end;
finally
Free;
end;
end;
 
 

Re:Convert from Delphi ?

"Sasan" < XXXX@XXXXX.COM >wrote in message
Quote
I have a code for list of all SQL-server,
can help me for converting this source to C++ ?
Try this (untested):
_di_IUnknown __fastcall PtCreateADOObject(const IID ClassID)
{
_di_IUnknown Result;
WORD FPUControlWord;
asm {
FNSTCW FPUControlWord
}
HRESULT Status = CoCreateInstance(ClassID, NULL,
CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&Result);
asm {
FNCLEX
FLDCW FPUControlWord
}
OleCheck(Status);
return Result;
}
void __fastcall ListAvailableSQLServers(TStrings *Names)
{
DelphiInterface<_Recordset>SourcesRecordset;
DelphiInterface<ADORecordsetConstruction>RSCon;
DelphiInterface<ISourcesRowset>SourcesRowset;
_di_IUnknown Rowset;
SourcesRecordset = PtCreateADOObject(CLASS_Recordset);
RSCon = SourcesRecordset;
SourcesRowset = CreateComObject(ProgIDToClassID("SQLOLEDB
Enumerator"));
OleCheck(SourcesRowset->GetSourcesRowset(NULL, IID_IRowset, 0, NULL,
&Rowset));
RSCon->put_Rowset(RowSet);
TADODataSet *DS = new TADODataSet(NULL);
try
{
DS->Recordset = SourcesRecordset;
TField *SourcesName = DS->FieldByName("SOURCES_NAME");
TField *SourcesType = DS->FieldByName("SOURCES_TYPE");
Names->BeginUpdate();
try
{
while( !DS->Eof )
{
if( SourcesType->AsInteger == DBSOURCETYPE_DATASOURCE )
{
AnsiString Value = SourcesName->AsString;
if( Value != "" )
Names->Add(Value);
}
DS->Next();
}
}
__finally {
Names->EndUpdate();
}
}
__finally {
delete DS;
}
}
Gambit
 

Re:Convert from Delphi ?

Hello,
Thanks dear Lebeau,
but in line:
OleCheck(SourcesRowset->GetSourcesRowset(NULL, ::IID_IRowset, 0,
NULL, Rowset));
i take exception access violation
regards,
m.t
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"Sasan" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>I have a code for list of all SQL-server,
>can help me for converting this source to C++ ?

Try this (untested):

_di_IUnknown __fastcall PtCreateADOObject(const IID ClassID)
{
_di_IUnknown Result;
WORD FPUControlWord;

asm {
FNSTCW FPUControlWord
}

HRESULT Status = CoCreateInstance(ClassID, NULL,
CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, IID_IUnknown,
(void**)&Result);

asm {
FNCLEX
FLDCW FPUControlWord
}

OleCheck(Status);
return Result;
}

void __fastcall ListAvailableSQLServers(TStrings *Names)
{
DelphiInterface<_Recordset>SourcesRecordset;
DelphiInterface<ADORecordsetConstruction>RSCon;
DelphiInterface<ISourcesRowset>SourcesRowset;
_di_IUnknown Rowset;

SourcesRecordset = PtCreateADOObject(CLASS_Recordset);
RSCon = SourcesRecordset;
SourcesRowset = CreateComObject(ProgIDToClassID("SQLOLEDB
Enumerator"));
OleCheck(SourcesRowset->GetSourcesRowset(NULL, IID_IRowset, 0,
NULL,
&Rowset));
RSCon->put_Rowset(RowSet);

TADODataSet *DS = new TADODataSet(NULL);
try
{
DS->Recordset = SourcesRecordset;

TField *SourcesName = DS->FieldByName("SOURCES_NAME");
TField *SourcesType = DS->FieldByName("SOURCES_TYPE");

Names->BeginUpdate();
try
{
while( !DS->Eof )
{
if( SourcesType->AsInteger == DBSOURCETYPE_DATASOURCE )
{
AnsiString Value = SourcesName->AsString;
if( Value != "" )
Names->Add(Value);
}
DS->Next();
}
}
__finally {
Names->EndUpdate();
}
}
__finally {
delete DS;
}
}


Gambit


 

{smallsort}

Re:Convert from Delphi ?

"Sasan" < XXXX@XXXXX.COM >wrote in message
Quote
but in line:
OleCheck(SourcesRowset->GetSourcesRowset(NULL, ::IID_IRowset, 0,
NULL, Rowset));
i take exception access violation
Make sure that you check your interface pointers for NULL before accessing
them. Also, you removed the '&' from the last parameter in the code I gave
you. You must use the '&' operator:
OleCheck(SourcesRowset->GetSourcesRowset(NULL, IID_IRowset, 0, NULL,
&Rowset));
Gambit
 

Re:Convert from Delphi ?

If i use & , i take error and program not compile ?
[C++ Error] Unit1.cpp(96): E2064 Cannot initialize '_di_IInterface &' with
'IUnknown * *'
[C++ Error] Unit1.cpp(96): E2342 Type mismatch in parameter
'ppSourcesRowset' (wanted '_di_IInterface &', got 'IUnknown * *')
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"Sasan" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>but in line:
>OleCheck(SourcesRowset->GetSourcesRowset(NULL, ::IID_IRowset, 0,
>NULL, Rowset));
>i take exception access violation

Make sure that you check your interface pointers for NULL before accessing
them. Also, you removed the '&' from the last parameter in the code I
gave
you. You must use the '&' operator:

OleCheck(SourcesRowset->GetSourcesRowset(NULL, IID_IRowset, 0, NULL,
&Rowset));


Gambit


 

Re:Convert from Delphi ?

"Sasan" < XXXX@XXXXX.COM >wrote in message
Quote
[C++ Error] Unit1.cpp(96): E2064 Cannot initialize '_di_IInterface &' with
'IUnknown * *'
That means that your code is using VCL wrappers for the ADO interfaces and
not the ADO interfaces directly. In which case, the issue is more
complicated since GetSourcesRowset() is expecting IInterface instead of
IUnknown.
For that matter, I just noticed that the Delphi code you posted originally
is a copy/paste of the GetProviderNames() function from the ADODB unit. So,
why are you trying to implement the lookup code manually instead of just
using GetProviderNames()?
Gambit
 

Re:Convert from Delphi ?

Infact i want take list of availabe SQL-server
regards,
m.t
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"Sasan" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>[C++ Error] Unit1.cpp(96): E2064 Cannot initialize '_di_IInterface &'
>with
>'IUnknown * *'

That means that your code is using VCL wrappers for the ADO interfaces and
not the ADO interfaces directly. In which case, the issue is more
complicated since GetSourcesRowset() is expecting IInterface instead of
IUnknown.

For that matter, I just noticed that the Delphi code you posted originally
is a copy/paste of the GetProviderNames() function from the ADODB unit.
So,
why are you trying to implement the lookup code manually instead of just
using GetProviderNames()?


Gambit