2010-09-12 40 views
2

我是一名物理學家。我正在嘗試使用導入的ActiveX控件(ocx文件)來處理Delphi。假設庫中有3個自動化接口:IGraph,IGraphAxes和IAxis。庫的結構如下:獲得對idispatch接口的訪問idispatch接口的成員

=== IGraph的屬性:===
Idispatch * IGraphAxes;
... //其他成員

=== IGraphAxes’屬性:===
的IDispatch * x軸;
Idispatch * YAxis;
Idispatch * ZAxis;
整數顏色;
整數樣式;
... //其他成員

=== IAxis不屬性:===
浮子最小值,最大值;
布爾ShowNumbers;
... //其他成員

從IGraph,我能夠使用GetIDsOfNames()和Invoke()函數獲得對IGraphAxes(Color和Style)的簡單成員的訪問。但是,當我嘗試訪問XAxis(或YAxis,Zaxis)時,它會生成一個錯誤。首先,我使用GetIDsOfNames(),它返回XAxis的dispid沒有任何問題。但是當我用這個dispid調用Invoke時,會出現一個錯誤「地址訪問衝突...」。看起來,idispatch指針(** Xaxis)*指向沒有。我該如何解決這個問題?如何製作
Idispatch * Xaxis

IAxis接口是否相互連接?
P.S.對不起我的英語,我不是母語的人

回答

2

德爾福內置支持IDispatch後期綁定,所以你不需要直接調用Invoke()。只需像普通屬性那樣訪問成員,Delphi將在幕後爲你調用Invoke()。

如果您想直接調用Invoke(),那麼請顯示您的實際代碼失敗。 AV很可能是由於代碼中的錯誤,而不是OCX本身。

0

感謝您的回覆。 實際上,activex控件是用於繪製某些3d圖形的第三方可視組件。 這裏是ActiveX庫的內容:

[
UUID(...),
版本(XX),
helpstring( 「3-d圖形模塊」),
控制,
定製(...,...),
定製(...,...)
]

庫 GraphLib
{

importlib(「stdole2.tlb」);

[
uuid(...),),
隱藏
]
調度接口_IGraph
{
性質
helpstring( 「3-d格拉夫控制調度接口」:
[
ID(0x0000006)
]
IDispatch的* GraphAxes(...);
方法:
[
ID(0x0000001)
]
字節AddScatterGraph(...);
[
ID(0x0000002)
]
字節AddVectorFieldGraph(...);
[
ID(0x0000003)
]
字節AddParametricSurfaceGraph(...);
[
ID(0x0000004)
]
字節AddSurfaceGraph(...);
[
ID(0x0000005)
]
字節RemoveGraphs(無效);
};
[
UUID(...),
helpstring( 「3-d格拉夫控制」),
控制
]
共類cGraph
{
[默認]的調度接口_IGraph;
[default,source] dispinterface _IGraphEvents;
};
[
uuid(...)
]
調度接口IGraphAxis
{
屬性:
[
ID(00000001)
]
字節編號;
[
id(0x00000002)
]
double Min;
[
id(0x00000003)
]
double Max;
[
id(0x00000004)
]
short GridNum;
[
ID(0x00000005)
]
字節ShowLabel;
方法:
};
[
UUID(...)
]
共類cGraphAxis
{
[默認]的調度接口IGraphAxis;
};
[
UUID(...)
]
調度接口IGraphAxes
{
屬性:
[
ID(00000001)
]
的IDispatch * x軸;
[
ID(0x00000002)
]
的IDispatch * Y軸;
[
ID(0x00000003)
]
的IDispatch * Z軸;
[
ID(0x00000004)
]
整數顏色;
[
id(0x00000005)
]
integer Style;
方法:
};
[
UUID(...)
]
共類cGraphAxes
{
[默認]的調度接口IGraphAxes;
};
[
uuid(...),
helpstring() 「事件3-d格拉夫控制接口」
]
調度接口_IGraphEvents
{
屬性:
方法:
[
ID(0xFFFFFDA3)
]
空隙的MouseDown(...);
[
ID(0xFFFFFDA8)
]
無效點擊...);
[
ID(0xFFFFFDA7)
]
空隙DBLCLICK(...);
[
ID(0xFFFFFDA6)
]
空隙的KeyDown(...);
[
ID(0xFFFFFDA5)
]
空隙按鍵響應(...);
[
ID(0xFFFFFDA4)
]
空隙KEYUP(...);
[
ID(0xFFFFFDA2)
]
空隙的MouseMove(...);
[
ID(0xFFFFFDA1)
]
空隙的MouseUp(...);
};
};

0

這裏是從類型庫生成的單元文件。
unit GraphLib_TLB;

// ************************************************************************ 
// 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.           
// ************************************************************************ // 

// ************************************************************************ // 
// Type Lib: graph3d.ocx (1) 
// LIBID: {...} 
// LCID: 0 
// HelpString: 3-D Graph module 
// DepndLst: 
// (1) v2.0 stdole, (C:\Windows\system32\stdole2.tlb) 
// ************************************************************************ // 
{$TYPEDADDRESS OFF} // Unit must be compiled without type-checked pointers. 
{$WARN SYMBOL_PLATFORM OFF} 
{$WRITEABLECONST ON} 
{$VARPROPSETTER ON} 
interface 

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

// *********************************************************************// 
// 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 
    // TypeLibrary Major and minor versions 
    GraphLibMajorVersion = x; 
    GraphLibMinorVersion = x; 

    LIBID_GLLib: TGUID = '{...}'; 

    DIID__IGraph: TGUID = '{...}'; 
    DIID__IGraphEvents: TGUID = '{...}'; 
    CLASS_cGraph: TGUID = '{...}'; 
    DIID_IGraphAxis: TGUID = '{...}'; 
    CLASS_cGraphAxis: TGUID = '{...}'; 
    DIID_IGraphAxes: TGUID = '{...}'; 
    CLASS_cGraphAxes: TGUID = '{...}'; 
type 

// *********************************************************************// 
// Forward declaration of types defined in TypeLibrary      
// *********************************************************************// 
    _IGraph = dispinterface; 
    _IGraphEvents = dispinterface; 
    IGraphAxis = dispinterface; 
    IGraphAxes = dispinterface; 
// *********************************************************************// 
// Declaration of CoClasses defined in Type Library       
// (NOTE: Here we map each CoClass to its Default Interface)     
// *********************************************************************// 
    cGraph = _IGraph; 
    cGraphAxis = IGraphAxis; 
    cGraphAxes = IGraphAxes; 

// *********************************************************************// 
// Declaration of structures, unions and aliases.       
// *********************************************************************// 
    PWideString1 = ^WideString; {*} 
    PShortint1 = ^Shortint; {*} 
    PSmallint1 = ^Smallint; {*} 

// *********************************************************************// 
// DispIntf: _IGraph 
// Flags:  (4112) Hidden Dispatchable 
// GUID:  {...} 
// *********************************************************************// 
    _IGraph = dispinterface 
    ['{...}'] 
    function AddScatterGraph(...): {??Shortint}OleVariant; dispid 1; 
    function AddVectorFieldGraph(...): {??Shortint}OleVariant; dispid 2; 
    function AddParametricSurfaceGraph(...): {??Shortint}OleVariant; dispid 3; 
    function AddSurfaceGraph(...): {??Shortint}OleVariant; dispid 4; 
    function RemoveGraphs: {??Shortint}OleVariant; dispid 5; 
    property GraphAxes: IDispatch dispid 6; 
    end; 

// *********************************************************************// 
// DispIntf: _IGraphEvents 
// Flags:  (4096) Dispatchable 
// GUID:  {...} 
// *********************************************************************// 
    _IGraphEvents = dispinterface 
    ['{...}'] 
    procedure MouseDown(); dispid -605; 
    procedure Click; dispid -600; 
    procedure DblClick; dispid -601; 
    procedure KeyDown(...); dispid -602; 
    procedure KeyPress(...); dispid -603; 
    procedure KeyUp(...); dispid -604; 
    procedure MouseMove(...); dispid -606; 
    procedure MouseUp(...); dispid -607; 
    procedure ReadyStateChange; dispid -609; 
    end; 

// *********************************************************************// 
// DispIntf: IGraphAxis 
// Flags:  (4096) Dispatchable 
// GUID:  {...} 
// *********************************************************************// 
    IGraphAxis = dispinterface 
    ['{...}'] 
    property Numbered: {??Shortint}OleVariant dispid 1; 
    property Min: Double dispid 2; 
    property Max: Double dispid 3; 
    property GridNum: Smallint dispid 4; 
    property ShowLabel: {??Shortint}OleVariant dispid 5;   
    end; 

// *********************************************************************// 
// DispIntf: IGraphAxes 
// Flags:  (4096) Dispatchable 
// GUID:  {...} 
// *********************************************************************// 
    IGraphAxes = dispinterface 
    ['{...}'] 
    property XAxis: IDispatch dispid 1; 
    property YAxis: IDispatch dispid 2; 
    property ZAxis: IDispatch dispid 3; 
    property Color: Integer dispid 4; 
    property Style: Smallint dispid 5; 
    end; 

// *********************************************************************// 
// OLE Control Proxy class declaration 
// Control Name  : TGraph 
// Help String  : 3-D Graph Control 
// Default Interface: _IGraph 
// Def. Intf. DISP? : Yes 
// Event Interface: _IGraphEvents 
// TypeFlags  : (34) CanCreate Control 
// *********************************************************************// 
    TGraph = class(TOleControl) 
    private 
    FOnError: TGraphError; 
    FOnReadyStateChange: TNotifyEvent; 
    FIntf: _IGraph; 
    function GetControlInterface: _IGraph; 
    protected 
    procedure CreateControl; 
    procedure InitControlData; override; 
    function Get_GraphAxes: IDispatch; 
    procedure Set_GraphAxes(const Value: IDispatch); 
    public 
    function AddScatterGraph(...): {??Shortint}OleVariant; 
    function AddVectorFieldGraph(...): {??Shortint}OleVariant; 
    function AddParametricSurfaceGraph(...): {??Shortint}OleVariant; 
    function AddSurfaceGraph(...): {??Shortint}OleVariant; 
    function RemoveGraphs: {??Shortint}OleVariant; 
    property ControlInterface: _IGraph read GetControlInterface; 
    property DefaultInterface: _IGraph read GetControlInterface; 
    property GraphAxes: IDispatch index 6 read GetIDispatchProp write SetIDispatchProp; 
    published 
    //properties visible in the Object Inspector go here 
    ... 
    property OnDragDrop; 
    property OnDragOver; 
    property OnEndDrag; 
    property OnEnter; 
    property OnExit; 
    property OnStartDrag; 
    property OnMouseUp; 
    property OnMouseMove; 
    property OnMouseDown; 
    property OnKeyUp; 
    property OnKeyPress; 
    property OnKeyDown; 
    property OnDblClick; 
    property OnClick; 
    property OnError: TGraphError read FOnError write FOnError; 
    property OnReadyStateChange: TNotifyEvent read FOnReadyStateChange write FOnReadyStateChange;  
    end;  

// *********************************************************************// 
// The Class CocGraphAxis provides a Create and CreateRemote method to    
// create instances of the default interface IGraphAxis exposed by     
// the CoClass cGraphAxis. The functions are intended to be used by    
// clients wishing to automate the CoClass objects exposed by the   
// server of this typelibrary.            
// *********************************************************************// 
    CocGraphAxis = class 
    class function Create: IGraphAxis; 
    class function CreateRemote(const MachineName: string): IGraphAxis; 
    end; 

// *********************************************************************// 
// The Class CocGraphAxes provides a Create and CreateRemote method to    
// create instances of the default interface IGraphAxes exposed by     
// the CoClass cGraphAxes. The functions are intended to be used by    
// clients wishing to automate the CoClass objects exposed by the   
// server of this typelibrary.            
// *********************************************************************// 
    CocGraphAxes = class 
    class function Create: IGraphAxes; 
    class function CreateRemote(const MachineName: string): IGraphAxes; 
    end; 

procedure Register; 

resourcestring 
    dtlServerPage = 'ActiveX'; 

    dtlOcxPage = 'ActiveX'; 

implementation 

uses ComObj; 

procedure TGraph.InitControlData; 
const 
    CEventDispIDs: array [0..1] of DWORD = (...); 
    CLicenseKey: array[0..33] of Word = ($006F, ..., ..., e.g.); 
    CControlData: TControlData2 = ( 
    ClassID: '{...}'; 
    EventIID: '{...}'; 
    EventCount: 2; 
    EventDispIDs: @CEventDispIDs; 
    LicenseKey: @CLicenseKey; 
    Flags: $00000000; 
    Version: 401); 
begin 
    ControlData := @CControlData; 
    TControlData2(CControlData).FirstEventOfs := Cardinal(@@FOnError) - Cardinal(Self); 
end; 

procedure TGraph.CreateControl; 

    procedure DoCreate; 
    begin 
    FIntf := IUnknown(OleObject) as _IGraph; 
    end; 

begin 
    if FIntf = nil then DoCreate; 
end; 

function TGraph.GetControlInterface: _IGraph; 
begin 
    CreateControl; 
    Result := FIntf; 
end; 

function TGraph.Get_GraphAxes: IDispatch; 
begin 
    Result := DefaultInterface.GraphAxes; 
end; 

procedure TGraph.Set_GraphAxes(const Value: IDispatch); 
begin 
    DefaultInterface.GraphAxes := Value; 
end; 

function TGraph.AddScatterGraph(...): {??Shortint}OleVariant; 
begin 
    Result := DefaultInterface.AddScatterGraph(...); 
end; 

function TGraph.AddVectorFieldGraph(...): {??Shortint}OleVariant; 
begin 
    Result := DefaultInterface.AddVectorFieldGraph(...); 
end; 

function TGraph.AddParametricSurfaceGraph(...): {??Shortint}OleVariant; 
begin 
    Result := DefaultInterface.AddParametricSurfaceGraph(...); 
end; 

function TGraph.AddSurfaceGraph(...): {??Shortint}OleVariant; 
begin 
    Result := DefaultInterface.AddSurfaceGraph(...); 
end; 

function TGraph.RemoveGraphs: {??Shortint}OleVariant; 
begin 
    Result := DefaultInterface.RemoveGraphs; 
end; 

class function CocGraphAxis.Create: IGraphAxis; 
begin 
    Result := CreateComObject(CLASS_cGraphAxis) as IGraphAxis; 
end; 

class function CocGraphAxis.CreateRemote(const MachineName: string): IGraphAxis; 
begin 
    Result := CreateRemoteComObject(MachineName, CLASS_cGraphAxis) as IGraphAxis; 
end; 

class function CocGraphAxes.Create: IGraphAxes; 
begin 
    Result := CreateComObject(CLASS_cGraphAxes) as IGraphAxes; 
end; 

class function CocGraphAxes.CreateRemote(const MachineName: string): IGraphAxes; 
begin 
    Result := CreateRemoteComObject(MachineName, CLASS_cGraphAxes) as IGraphAxes; 
end; 

procedure Register; 
begin 
    RegisterComponents(dtlOcxPage, [TGraph]); 
end; 

end. 
0

當我導入OCX文件導入Delphi中,它出現在組件面板的的ActiveX選項卡上。我只是用鼠標拖動它,並把它放在一個窗體和一個對象上。
Graph1:TGraph;
會自動添加到我的代碼中。其屬性和事件在Object Inspector窗口中可見。現在我想從我的代碼中訪問控制軸。如您所見,該屬性表示座標軸。另外我猜想IGraphAxes的XAxis/YAxis/ZAxis成員是IGraphAxis類型的idispatch指針。我寫了下面的程序訪問一個IDispatch接口:

procedure TForm2.GetProperty(dispobj: IDispatch; PropertyName: WideString; 
           var retvalue: Variant; Sender: TObject); 
var hr: HRESULT; 
    DispId: integer; 
    value: Variant; 
    params: TDispParams; 
begin 
    hr:=dispobj.GetIDsOfNames(GUID_NULL,@PropertyName, 1, LOCALE_SYSTEM_DEFAULT, @DispId); 
    Label1.Caption:=inttostr(DispId); 
    hr:=dispobj.Invoke(DispId,GUID_NULL,LOCALE_SYSTEM_DEFAULT,DISPATCH_PROPERTYGET, 
        Params,@Value,nil,nil); 
    Retvalue:=Value; 
    Label2.Caption:=inttostr(value); 
end; 

procedure TForm2.SetProperty(dispobj: IDispatch; PropertyName: WideString; Value: OLEVariant; Sender: TObject); 
var 
    hr: HRESULT; 
    DispId: integer; 
    params: TDispParams; 
begin 
    hr:=dispobj.GetIDsOfNames(GUID_NULL,@PropertyName,1, LOCALE_SYSTEM_DEFAULT, @DispId); 
    Label1.Caption:=inttostr(DispId); 
    params.rgvarg:[email protected]; 
    params.rgdispidNamedArgs:[email protected]; 
    params.cArgs:=1; 
    params.cNamedArgs:=1; 
    hr:=dispobj.Invoke(DispId,GUID_NULL,LOCALE_SYSTEM_DEFAULT,     DISPATCH_PROPERTYPUT,Params,nil,nil,nil); 
end; 

他們很好地工作IGraphAxes的顏色和樣式屬性:

GetProperty(Graph1.GraphAxes, 'Color', retvalue, Sender); 

或者

SetProperty(Graph1.GraphAxes, 'Color',value,Sender); 

但如何可以完全訪問到IGraphAxes的XAxis/YAxis/ZAxis成員?