Board index » delphi » Third try: Calling VB-6 automation objects from Delphi

Third try: Calling VB-6 automation objects from Delphi

I am still trying to successfully call methods in a VB6 automation object
that has array arguments.  In VB, the method takes dynamic arrays of Single and
Long Integer.

         In Delphi 2.0, I tried using the OLECreateArray function with types
varSingle and varInteger, as well as with varVariant (as per a previous suggestion).
In all cases, I get an OLE exception error indicating an argument  type mismatch.

        I have no trouble creating an instance of the object, or calling
methods that do not require arrays.  Simple functions and methods work fine.

        I have appended example code to this message.  The automation object,
for those who might have a copy, is the server that comes with the TC-2000 stock
data service from Worden Bros.  They provide info on VB, but not on Delphi.  I have
been able to call the functions from VBA in Excel, but would much prefer to work in
Delphi.

        Any help would be appreciated.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls,
  Forms, Dialogs, Grids, StdCtrls, OleAuto;

type
  TForm1 = class(TForm)
    Button1: TButton;
    StringGrid1: TStringGrid;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
    Server: Variant;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
     { This code works fine, runs with no problems.
       Initializes the server as it should.
       The Server object was written in VB, version 6 }
     StringGrid1.Cells[1, 1] := 'Creating Server Object';
     Server := CreateOleObject('TC2000Dev.cTC2000');
     StringGrid1.Cells[1, 2] := Server.TCEnabled;
     StringGrid1.Cells[1, 3] := Server.GetVersion;
     StringGrid1.Cells[1, 4] := Server.GetRevision;
     StringGrid1.Cells[1, 5] := 'Server Created';
end;

procedure TForm1.Button2Click(Sender: TObject);
var
   num, znum, opn, hi, lo, cls, vol, dt: Variant;
begin
     { This code attempts to retrieve data from the server.
       It fails when the GetPrices method is called. }
     opn := VarArrayCreate([1, 10], varVariant);
     hi := VarArrayCreate([1, 10], varVariant);
     lo := VarArrayCreate([1, 10], varVariant);
     cls := VarArrayCreate([1, 10], varVariant);
     vol := VarArrayCreate([1, 10], varVariant);
     dt := VarArrayCreate([1, 10], varVariant);
     num := 1234;
     znum := 0;
     { The GetPrices method fails at run time with a Delphi
       exception of type EOleError indicating a "type mismatch".
       I have tried VarArrayCreate with varSingle and varInteger
       to mimic the VB code shown below, but still get the same
       error message.  The server is the automation object that
       comes with the TC-2000 Worden Bros. stock data service.  }
     Server.GetPrices(num, opn, hi, lo, cls, vol, dt, znum);
     With StringGrid1 Do Begin
          Cells[2, 1] := FloatToStr(cls[2]);
          Cells[2, 2] := FloatToStr(vol[2]);
          Cells[2, 3] := IntToStr(VarArrayHighBound(cls, 1));
          Cells[2, 4] := IntToStr(VarArrayLowBound(cls, 1));
     End;
     Server := Unassigned;
end;

     {
        In Excel's VB, the following code works fine:

        Dim Server As Object
        Dim opn() As Single, hi() As Single, lo() As Single
        Dim cls() As Single, vol() As Long, dt() As Long
        Dim znum As Long, num As Long
        .
        .
        Sub Button1Click()
                Set Server = ...
                .
                Server.GetPrices num, opn(), hi(), lo(),
                        cls(), vol(), dt(), znum
                .
                . display the data...
        End Sub
     }

end.

 

Re:Third try: Calling VB-6 automation objects from Delphi


As far as I know VB variant arrays are zero based. Yours are one based.

--
Greg Lorriman
Free handy utils at http://www.lorriman.demon.co.uk

Jeffrey O. Katz <o...@panix.com> wrote in message
news:7tkfpu$4hm$5@news.panix.com...

Quote
> I am still trying to successfully call methods in a VB6 automation object
> that has array arguments.  In VB, the method takes dynamic arrays of
Single and
> Long Integer.

> In Delphi 2.0, I tried using the OLECreateArray function with types
> varSingle and varInteger, as well as with varVariant (as per a previous
suggestion).
> In all cases, I get an OLE exception error indicating an argument  type
mismatch.

> I have no trouble creating an instance of the object, or calling
> methods that do not require arrays.  Simple functions and methods work
fine.

> I have appended example code to this message.  The automation object,
> for those who might have a copy, is the server that comes with the TC-2000
stock
> data service from Worden Bros.  They provide info on VB, but not on
Delphi.  I have
> been able to call the functions from VBA in Excel, but would much prefer
to work in
> Delphi.

> Any help would be appreciated.

> unit Unit1;

> interface

> uses
>   Windows, Messages, SysUtils, Classes, Graphics, Controls,
>   Forms, Dialogs, Grids, StdCtrls, OleAuto;

> type
>   TForm1 = class(TForm)
>     Button1: TButton;
>     StringGrid1: TStringGrid;
>     Button2: TButton;
>     procedure Button1Click(Sender: TObject);
>     procedure Button2Click(Sender: TObject);
>   private
>     { Private declarations }
>     Server: Variant;
>   public
>     { Public declarations }
>   end;

> var
>   Form1: TForm1;

> implementation

> {$R *.DFM}

> procedure TForm1.Button1Click(Sender: TObject);
> begin
>      { This code works fine, runs with no problems.
>        Initializes the server as it should.
>        The Server object was written in VB, version 6 }
>      StringGrid1.Cells[1, 1] := 'Creating Server Object';
>      Server := CreateOleObject('TC2000Dev.cTC2000');
>      StringGrid1.Cells[1, 2] := Server.TCEnabled;
>      StringGrid1.Cells[1, 3] := Server.GetVersion;
>      StringGrid1.Cells[1, 4] := Server.GetRevision;
>      StringGrid1.Cells[1, 5] := 'Server Created';
> end;

> procedure TForm1.Button2Click(Sender: TObject);
> var
>    num, znum, opn, hi, lo, cls, vol, dt: Variant;
> begin
>      { This code attempts to retrieve data from the server.
>        It fails when the GetPrices method is called. }
>      opn := VarArrayCreate([1, 10], varVariant);
>      hi := VarArrayCreate([1, 10], varVariant);
>      lo := VarArrayCreate([1, 10], varVariant);
>      cls := VarArrayCreate([1, 10], varVariant);
>      vol := VarArrayCreate([1, 10], varVariant);
>      dt := VarArrayCreate([1, 10], varVariant);
>      num := 1234;
>      znum := 0;
>      { The GetPrices method fails at run time with a Delphi
>        exception of type EOleError indicating a "type mismatch".
>        I have tried VarArrayCreate with varSingle and varInteger
>        to mimic the VB code shown below, but still get the same
>        error message.  The server is the automation object that
>        comes with the TC-2000 Worden Bros. stock data service.  }
>      Server.GetPrices(num, opn, hi, lo, cls, vol, dt, znum);
>      With StringGrid1 Do Begin
>           Cells[2, 1] := FloatToStr(cls[2]);
>           Cells[2, 2] := FloatToStr(vol[2]);
>           Cells[2, 3] := IntToStr(VarArrayHighBound(cls, 1));
>           Cells[2, 4] := IntToStr(VarArrayLowBound(cls, 1));
>      End;
>      Server := Unassigned;
> end;

>      {
>         In Excel's VB, the following code works fine:

>         Dim Server As Object
>         Dim opn() As Single, hi() As Single, lo() As Single
>         Dim cls() As Single, vol() As Long, dt() As Long
>         Dim znum As Long, num As Long
>         .
>         .
>         Sub Button1Click()
>                 Set Server = ...
>                 .
>                 Server.GetPrices num, opn(), hi(), lo(),
>                         cls(), vol(), dt(), znum
>                 .
>                 . display the data...
>         End Sub
>      }

> end.

Other Threads