Matcom中的COM在delphi中如何调用(200分)

  • 主题发起人 主题发起人 FreeFly
  • 开始时间 开始时间
F

FreeFly

Unregistered / Unconfirmed
GUEST, unregistred user!
Matcom中有一个COM,叫做MatrixVB,在VB的工程中引用MMatrix即可。
在它的说明文档中给出了在VB中调用的简单的例子。
我象既然它是一个COM,在delphi中也应可以调用。我通过Import type library
将MMatrix引入delphi,自动生成了一个MMatrix_tlb.pas。
下面是部分代码:
unit MMatrix_TLB;

// ************************************************************************ //
// WARNING //
// ------- //
// The types declared in this file were generated from data read from a //
// Type Library. If this type library is explicitly or indirectly (via //
// another type library referring to this type library) re-imported, or the //
// 'Refresh' command of the Type Library Editor activated while editing the //
// Type Library, the contents of this file will be regenerated and all //
// manual modifications will be lost. //
// ************************************************************************ //

// PASTLWTR : $Revision: 1.11.1.62 $
// File generated on 00-6-22 10:12:38 from Type Library described below.

// ************************************************************************ //
// Type Lib: C:/WINDOWS/SYSTEM/MMATRIX.DLL
// IID/LCID: {053E110E-0DE2-11D3-B81B-10C48BCB0000}/0
// Helpfile:
// HelpString:
// Version: 53.1
// ************************************************************************ //

interface

uses Windows, ActiveX, Classes, Graphics, OleCtrls, StdVCL;

// *********************************************************************//
// GUIDS declared in the TypeLibrary. Following prefixes are used: //
// Type Libraries : LIBID_xxxx //
// CoClasses : CLASS_xxxx //
// DISPInterfaces : DIID_xxxx //
// Non-DISP interfaces: IID_xxxx //
// *********************************************************************//
const
LIBID_MMatrix: TGUID = '{053E110E-0DE2-11D3-B81B-10C48BCB0000}';
IID__Matrix: TGUID = '{924E3CB2-4AA0-11D3-B81B-444553540001}';
CLASS_Matrix: TGUID = '{053E1112-0DE2-11D3-B81B-10C48BCB0000}';
IID__Mlib: TGUID = '{984396B2-8D76-11D3-B81D-00C04F2AC5EB}';
CLASS_Mlib: TGUID = '{053E1110-0DE2-11D3-B81B-10C48BCB0000}';
type

// *********************************************************************//
// Forward declaration of interfaces defined in Type Library //
// *********************************************************************//
_Matrix = interface;
_MatrixDisp = dispinterface;
_Mlib = interface;
_MlibDisp = dispinterface;

// *********************************************************************//
// Declaration of CoClasses defined in Type Library //
// (NOTE: Here we map each CoClass to its Default Interface) //
// *********************************************************************//
Matrix = _Matrix;
Mlib = _Mlib;


// *********************************************************************//
// Declaration of structures, unions and aliases. //
// *********************************************************************//

Mlib___v0 = _Mlib;

// *********************************************************************//
// Interface: _Matrix
// Flags: (4560) Hidden Dual NonExtensible OleAutomation Dispatchable
// GUID: {924E3CB2-4AA0-11D3-B81B-444553540001}
// *********************************************************************//
_Matrix = interface(IDispatch)
['{924E3CB2-4AA0-11D3-B81B-444553540001}']
function Get_rows: Integer; safecall;
function Get_cols: Integer; safecall;
function Get_Dims: Integer; safecall;
function Get_Dimension(var dim1: Integer): Integer; safecall;
function Get_Str: WordBool; safecall;
procedure Set_Str(var Param1: WordBool); safecall;
function Get_r1(idx0: Integer): Double; safecall;
procedure Set_r1(idx0: Integer; Param2: Double); safecall;
function Get_i1(idx0: Integer): Double; safecall;
procedure Set_i1(idx0: Integer; Param2: Double); safecall;
function Get_r2(idx0: Integer; idx1: Integer): Double; safecall;
procedure Set_r2(idx0: Integer; idx1: Integer; Param3: Double); safecall;
function Get_i2(idx0: Integer; idx1: Integer): Double; safecall;
procedure Set_i2(idx0: Integer; idx1: Integer; Param3: Double); safecall;
function Get_r3(idx0: Integer; idx1: Integer; idx2: Integer): Double; safecall;
procedure Set_r3(idx0: Integer; idx1: Integer; idx2: Integer; Param4: Double); safecall;
function Get_i3(idx0: Integer; idx1: Integer; idx2: Integer): Double; safecall;
procedure Set_i3(idx0: Integer; idx1: Integer; idx2: Integer; Param4: Double); safecall;
function Get_Handle: Integer; safecall;
procedure Set_Handle(var Param1: Integer); safecall;
function Get_paren(var idx0: OleVariant; var idx1: OleVariant; var idx2: OleVariant;
var idx3: OleVariant): _Matrix; safecall;
procedure Set_paren(var idx0: OleVariant; var idx1: OleVariant; var idx2: OleVariant;
var idx3: OleVariant; var Param5: OleVariant); safecall;
procedure ReSize(rows: Integer; cols: Integer); safecall;
function Simple: OleVariant; safecall;
procedure Show(var how: OleVariant); safecall;
property rows: Integer read Get_rows;
property cols: Integer read Get_cols;
property Dims: Integer read Get_Dims;
property Dimension[var dim1: Integer]: Integer read Get_Dimension;
property Str: WordBool read Get_Str write Set_Str;
property r1[idx0: Integer]: Double read Get_r1 write Set_r1;
property i1[idx0: Integer]: Double read Get_i1 write Set_i1;
property r2[idx0: Integer; idx1: Integer]: Double read Get_r2 write Set_r2;
property i2[idx0: Integer; idx1: Integer]: Double read Get_i2 write Set_i2;
property r3[idx0: Integer; idx1: Integer; idx2: Integer]: Double read Get_r3 write Set_r3;
property i3[idx0: Integer; idx1: Integer; idx2: Integer]: Double read Get_i3 write Set_i3;
property Handle: Integer read Get_Handle write Set_Handle;
end;

// *********************************************************************//
// DispIntf: _MatrixDisp
// Flags: (4560) Hidden Dual NonExtensible OleAutomation Dispatchable
// GUID: {924E3CB2-4AA0-11D3-B81B-444553540001}
// *********************************************************************//
_MatrixDisp = dispinterface
['{924E3CB2-4AA0-11D3-B81B-444553540001}']
property rows: Integer readonly dispid 1745027083;
property cols: Integer readonly dispid 1745027082;
property Dims: Integer readonly dispid 1745027081;
property Dimension[var dim1: Integer]: Integer readonly dispid 1745027080;
property Str: WordBool dispid 1745027079;
property r1[idx0: Integer]: Double dispid 1745027078;
property i1[idx0: Integer]: Double dispid 1745027077;
property r2[idx0: Integer; idx1: Integer]: Double dispid 1745027076;
property i2[idx0: Integer; idx1: Integer]: Double dispid 1745027075;
property r3[idx0: Integer; idx1: Integer; idx2: Integer]: Double dispid 1745027074;
property i3[idx0: Integer; idx1: Integer; idx2: Integer]: Double dispid 1745027073;
property Handle: Integer dispid 1745027072;
function paren(var idx0: OleVariant; var idx1: OleVariant; var idx2: OleVariant;
var idx3: OleVariant): _Matrix; dispid 0;
procedure ReSize(rows: Integer; cols: Integer); dispid 1610809356;
function Simple: OleVariant; dispid 1610809357;
procedure Show(var how: OleVariant); dispid 1610809358;
end;

// *********************************************************************//
// Interface: _Mlib
// Flags: (4560) Hidden Dual NonExtensible OleAutomation Dispatchable
// GUID: {984396B2-8D76-11D3-B81D-00C04F2AC5EB}
// *********************************************************************//
_Mlib = interface(IDispatch)
['{984396B2-8D76-11D3-B81D-00C04F2AC5EB}']
function Get_IsXL: WordBool; safecall;
procedure Set_IsXL(IsXL: WordBool); safecall;
function ToMatrix(var x: OleVariant): _Matrix; safecall;
function CreateMatrix(var v: PSafeArray): OleVariant; safecall;
.......
function ylabel(var vv: PSafeArray): OleVariant; safecall;
function zeros(var vv: PSafeArray): OleVariant; safecall;
function zlabel(var vv: PSafeArray): OleVariant; safecall;
function zoom(var vv: PSafeArray): OleVariant; safecall;
function line(var v: PSafeArray): OleVariant; safecall;
property IsXL: WordBool read Get_IsXL write Set_IsXL;
end;

// *********************************************************************//
// DispIntf: _MlibDisp
// Flags: (4560) Hidden Dual NonExtensible OleAutomation Dispatchable
// GUID: {984396B2-8D76-11D3-B81D-00C04F2AC5EB}
// *********************************************************************//
_MlibDisp = dispinterface
['{984396B2-8D76-11D3-B81D-00C04F2AC5EB}']
property IsXL: WordBool dispid 1073938432;
function ToMatrix(var x: OleVariant): _Matrix; dispid 1610809344;
function CreateMatrix(var v: {??PSafeArray} OleVariant): OleVariant; dispid 1610809842;
function acos(var vv: {??PSafeArray} OleVariant): OleVariant; dispid 1610809346;
function acosh(var vv: {??PSafeArray} OleVariant): OleVariant; dispid 1610809347;
function acot(var vv: {??PSafeArray} OleVariant): OleVariant; dispid 1610809348;
function acoth(var vv: {??PSafeArray} OleVariant): OleVariant; dispid 1610809349;
......
function zeros(var vv: {??PSafeArray} OleVariant): OleVariant; dispid 1610809839;
function zlabel(var vv: {??PSafeArray} OleVariant): OleVariant; dispid 1610809840;
function zoom(var vv: {??PSafeArray} OleVariant): OleVariant; dispid 1610809841;
function line(var v: {??PSafeArray} OleVariant): OleVariant; dispid 1610809606;
end;

CoMatrix = class
class function Create: _Matrix;
class function CreateRemote(const MachineName: string): _Matrix;
end;

CoMlib = class
class function Create: _Mlib;
class function CreateRemote(const MachineName: string): _Mlib;
end;

implementation

uses ComObj;

class function CoMatrix.Create: _Matrix;
begin
Result := CreateComObject(CLASS_Matrix) as _Matrix;
end;

class function CoMatrix.CreateRemote(const MachineName: string): _Matrix;
begin
Result := CreateRemoteComObject(MachineName, CLASS_Matrix) as _Matrix;
end;

class function CoMlib.Create: _Mlib;
begin
Result := CreateComObject(CLASS_Mlib) as _Mlib;
end;

class function CoMlib.CreateRemote(const MachineName: string): _Mlib;
begin
Result := CreateRemoteComObject(MachineName, CLASS_Mlib) as _Mlib;
end;

end.

利用下面的代码可以进行调用:
var
a : _MlibDisp;
b,c,d : olevariant;
begin
a := CreateComObject(CLASS_Mlib) as _MlibDisp;
c := form1.handle;
b := a.vbaxes(c);
end;

有两个问题:
1.对于有多个入口参数的该如何调用?
例如:zeros(m,n)函数用来生成m×n的矩阵
在VB中a=zeros(3,4)这样即可使用。
可是zeros在MMatrix_tlb中是这样定义的:
function zeros(var vv: {??PSafeArray} OleVariant): OleVariant; dispid 1610809839;
在函数中只有一个入口参数,这样的话该如何调用?
2.返回值该如何用?
还是zeros这个函数,如果这样使用
a := CreateComObject(CLASS_Mlib) as _MlibDisp;
c := 3;
b := a.zeros(c);
生成一个3×3的矩阵,那么返回值b该怎样化成delphi中的3×3的数组?
望得到高手的指点。
 
to FreeFly:
试一下,看行不?
var
a : _MlibDisp;
b,c,d : olevariant;
i, j: integer;
begin
a := CreateComObject(CLASS_Mlib) as _MlibDisp;
c := VarArrayOf([m,n]);//动态地定义 c 为数组,m、n为整数
b := a.zeros(c);//等于VB中的a = a.zeros(m,n)
if VarIsArray(b) then
begin
for i := VarArrayLowBound(b) to VarArrayHighBound(b) do
//相当于 for i := 0 to m - 1 do
if VarIsArray(b) then
begin
for j := VarArrayLowBound(b) to VarArrayHighBount(b) do
//相当于 for j := 0 to n - 1 do
begin
//使用数组元素b[i,j]的值,如 变量 := b[i,j]
end;
end
end;
end;
最好看一看 system 单元相关类型和过程、函数的定义。
 
bbkxjy, thank you. I find another way to use matocm in delphi.
 
后退
顶部