delphi调用c++ 写的一个ocx控件 其中有个方法是 中的参数类型是olevariant类型,编译通过,运行时怎么传都报错'类型不匹配'.C++中该接口是这样定义的:
showImage(long framelenght,int codecType,unsigned long frameType,unsigned long width,unsigned long height,char * frame_in)C++中我测试了,是这么调用的: 
m_DrawerCtrl1.showImage(framelenght,decode_type,frame_type,width,height,(signed char*)&frame[0]);在delphi中我将socket收到的char数组转换成olevariant类型: 
FrameBuff:=VarArrayCreate([0, Flen+1],varbyte); 
for i := Low(filemem[displayindex]) to Flen do 
begin 
FrameBuff[i] := filemem[displayindex][i]; 
end;
将FrameBuff作为参数传入 编译是通过了 但 运行出现"类型不匹配错误": 
showImage(Flen,Ctype,Ftype,Fweight,Fheight,FrameBuff);

解决方案 »

  1.   

    补充一下filemem[displayindex][i]是一个二维char数组,每一维都是一个端口接受到的帧数据.
      

  2.   

    char*在delphi中应该是对应pchar或0开始的char数组
      

  3.   

    看看你在delphi中是怎么定义该函数的,应该是类型设置有误,所以编译能过,运行出错
      

  4.   

    对的,是pchar 但我试图将pchar传入是,编译错误,delphi的到如TLB文件是这样的:
    unit DrawerLib_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 : 1.2
    // File generated on 2009-4-29 15:08:11 from Type Library described below.// ************************************************************************  //
    // Type Lib: D:\drawtest2\Drawer.ocx (1)
    // LIBID: {6A665779-7D68-4FE5-BC03-541CD8312A58}
    // LCID: 0
    // Helpfile: D:\drawtest2\Drawer.hlp
    // HelpString: Drawer ActiveX 控件模块
    // DepndLst: 
    //   (1) v2.0 stdole, (F:\WINDOWS\system32\stdole2.tlb)
    // ************************************************************************ //
    {$TYPEDADDRESS OFF} // Unit must be compiled without type-checked pointers. 
    {$WARN SYMBOL_PLATFORM OFF}
    {$WRITEABLECONST ON}
    {$VARPROPSETTER ON}
    interfaceuses 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
      DrawerLibMajorVersion = 1;
      DrawerLibMinorVersion = 0;  LIBID_DrawerLib: TGUID = '{6A665779-7D68-4FE5-BC03-541CD8312A58}';  DIID__DDrawer: TGUID = '{C57B2E4D-CF37-4B6D-A53C-A3F3A25C9524}';
      DIID__DDrawerEvents: TGUID = '{97CBBC3A-B5A3-4A31-AB68-24CD7CB4A38F}';
      CLASS_Drawer: TGUID = '{83521820-CEE7-41B5-9539-DE89C3BDFCF3}';
    type// *********************************************************************//
    // Forward declaration of types defined in TypeLibrary                    
    // *********************************************************************//
      _DDrawer = dispinterface;
      _DDrawerEvents = dispinterface;// *********************************************************************//
    // Declaration of CoClasses defined in Type Library                       
    // (NOTE: Here we map each CoClass to its Default Interface)              
    // *********************************************************************//
      Drawer = _DDrawer;
    // *********************************************************************//
    // Declaration of structures, unions and aliases.                         
    // *********************************************************************//
      PShortint1 = ^Shortint; {*}
    // *********************************************************************//
    // DispIntf:  _DDrawer
    // Flags:     (4096) Dispatchable
    // GUID:      {C57B2E4D-CF37-4B6D-A53C-A3F3A25C9524}
    // *********************************************************************//
      _DDrawer = dispinterface
        ['{C57B2E4D-CF37-4B6D-A53C-A3F3A25C9524}']
       procedure showImage(framelenght: Integer; codecType: SYSINT; frameType: LongWord; 
                            width: LongWord; height: LongWord; var frame_in: {??Shortint}OleVariant); dispid 1;

        procedure Hello; dispid 2;
      end;// *********************************************************************//
    // DispIntf:  _DDrawerEvents
    // Flags:     (4096) Dispatchable
    // GUID:      {97CBBC3A-B5A3-4A31-AB68-24CD7CB4A38F}
    // *********************************************************************//
      _DDrawerEvents = dispinterface
        ['{97CBBC3A-B5A3-4A31-AB68-24CD7CB4A38F}']
      end;
    // *********************************************************************//
    // OLE Control Proxy class declaration
    // Control Name     : TDrawer
    // Help String      : Drawer Control
    // Default Interface: _DDrawer
    // Def. Intf. DISP? : Yes
    // Event   Interface: _DDrawerEvents
    // TypeFlags        : (34) CanCreate Control
    // *********************************************************************//
      TDrawer = class(TOleControl)
      private
        FIntf: _DDrawer;
        function  GetControlInterface: _DDrawer;
      protected
        procedure CreateControl;
        procedure InitControlData; override;
      public
        procedure showImage(framelenght: Integer; codecType: SYSINT; frameType: LongWord; 
                            width: LongWord; height: LongWord; var frame_in: {??Shortint}OleVariant);

        procedure Hello;
        property  ControlInterface: _DDrawer read GetControlInterface;
        property  DefaultInterface: _DDrawer read GetControlInterface;
      published
        property Anchors;
        property  TabStop;
        property  Align;
        property  DragCursor;
        property  DragMode;
        property  ParentShowHint;
        property  PopupMenu;
        property  ShowHint;
        property  TabOrder;
        property  Visible;
        property  OnDragDrop;
        property  OnDragOver;
        property  OnEndDrag;
        property  OnEnter;
        property  OnExit;
        property  OnStartDrag;
      end;procedure Register;resourcestring
      dtlServerPage = 'ActiveX';  dtlOcxPage = 'ActiveX';implementationuses ComObj;procedure TDrawer.InitControlData;
    const
      CControlData: TControlData2 = (
        ClassID: '{83521820-CEE7-41B5-9539-DE89C3BDFCF3}';
        EventIID: '';
        EventCount: 0;
        EventDispIDs: nil;
        LicenseKey: nil (*HR:$80004005*);
        Flags: $00000000;
        Version: 401);
    begin
      ControlData := @CControlData;
    end;procedure TDrawer.CreateControl;  procedure DoCreate;
      begin
        FIntf := IUnknown(OleObject) as _DDrawer;
      end;begin
      if FIntf = nil then DoCreate;
    end;function TDrawer.GetControlInterface: _DDrawer;
    begin
      CreateControl;
      Result := FIntf;
    end;procedure TDrawer.showImage(framelenght: Integer; codecType: SYSINT; frameType: LongWord; 
                                width: LongWord; height: LongWord; var frame_in: {??Shortint}OleVariant); 
     
    begin
      DefaultInterface.showImage(framelenght, codecType, frameType, width, height, frame_in);
    end;procedure TDrawer.Hello;
    begin
      DefaultInterface.Hello;
    end;procedure Register;
    begin
      RegisterComponents(dtlOcxPage, [TDrawer]);
    end;end.
    我也曾经试图将这个接口中的var frame_in: {??Shortint}OleVariant 改为 frame_in:pchar  但编译错误;位置是
       procedure showImage(framelenght: Integer; codecType: SYSINT; frameType: LongWord; 
                            width: LongWord; height: LongWord; var frame_in: {??Shortint}OleVariant); dispid 1;
      

  5.   

    试试用array of array of char
      

  6.   

    先谢谢netwolfds !
    我在delphi中试过传入 pchar作为frame_in的参数.但编译错位
    这个函数是ocx带的方法,我将到如的TLB文件贴出来
    unit DrawerLib_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 : 1.2
    // File generated on 2009-4-29 15:08:11 from Type Library described below.// ************************************************************************  //
    // Type Lib: D:\drawtest2\Drawer.ocx (1)
    // LIBID: {6A665779-7D68-4FE5-BC03-541CD8312A58}
    // LCID: 0
    // Helpfile: D:\drawtest2\Drawer.hlp
    // HelpString: Drawer ActiveX 控件模块
    // DepndLst: 
    //   (1) v2.0 stdole, (F:\WINDOWS\system32\stdole2.tlb)
    // ************************************************************************ //
    {$TYPEDADDRESS OFF} // Unit must be compiled without type-checked pointers. 
    {$WARN SYMBOL_PLATFORM OFF}
    {$WRITEABLECONST ON}
    {$VARPROPSETTER ON}
    interfaceuses 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
      DrawerLibMajorVersion = 1;
      DrawerLibMinorVersion = 0;  LIBID_DrawerLib: TGUID = '{6A665779-7D68-4FE5-BC03-541CD8312A58}';  DIID__DDrawer: TGUID = '{C57B2E4D-CF37-4B6D-A53C-A3F3A25C9524}';
      DIID__DDrawerEvents: TGUID = '{97CBBC3A-B5A3-4A31-AB68-24CD7CB4A38F}';
      CLASS_Drawer: TGUID = '{83521820-CEE7-41B5-9539-DE89C3BDFCF3}';
    type// *********************************************************************//
    // Forward declaration of types defined in TypeLibrary                    
    // *********************************************************************//
      _DDrawer = dispinterface;
      _DDrawerEvents = dispinterface;// *********************************************************************//
    // Declaration of CoClasses defined in Type Library                       
    // (NOTE: Here we map each CoClass to its Default Interface)              
    // *********************************************************************//
      Drawer = _DDrawer;
    // *********************************************************************//
    // Declaration of structures, unions and aliases.                         
    // *********************************************************************//
      PShortint1 = ^Shortint; {*}
    // *********************************************************************//
    // DispIntf:  _DDrawer
    // Flags:     (4096) Dispatchable
    // GUID:      {C57B2E4D-CF37-4B6D-A53C-A3F3A25C9524}
    // *********************************************************************//
      _DDrawer = dispinterface
        ['{C57B2E4D-CF37-4B6D-A53C-A3F3A25C9524}']
        procedure showImage(framelenght: Integer; codecType: SYSINT; frameType: LongWord; 
                            width: LongWord; height: LongWord; var frame_in: {??Shortint}OleVariant); dispid 1;

        procedure Hello; dispid 2;
      end;// *********************************************************************//
    // DispIntf:  _DDrawerEvents
    // Flags:     (4096) Dispatchable
    // GUID:      {97CBBC3A-B5A3-4A31-AB68-24CD7CB4A38F}
    // *********************************************************************//
      _DDrawerEvents = dispinterface
        ['{97CBBC3A-B5A3-4A31-AB68-24CD7CB4A38F}']
      end;
    // *********************************************************************//
    // OLE Control Proxy class declaration
    // Control Name     : TDrawer
    // Help String      : Drawer Control
    // Default Interface: _DDrawer
    // Def. Intf. DISP? : Yes
    // Event   Interface: _DDrawerEvents
    // TypeFlags        : (34) CanCreate Control
    // *********************************************************************//
      TDrawer = class(TOleControl)
      private
        FIntf: _DDrawer;
        function  GetControlInterface: _DDrawer;
      protected
        procedure CreateControl;
        procedure InitControlData; override;
      public
        procedure showImage(framelenght: Integer; codecType: SYSINT; frameType: LongWord; 
                            width: LongWord; height: LongWord; var frame_in: {??Shortint}OleVariant);

        procedure Hello;
        property  ControlInterface: _DDrawer read GetControlInterface;
        property  DefaultInterface: _DDrawer read GetControlInterface;
      published
        property Anchors;
        property  TabStop;
        property  Align;
        property  DragCursor;
        property  DragMode;
        property  ParentShowHint;
        property  PopupMenu;
        property  ShowHint;
        property  TabOrder;
        property  Visible;
        property  OnDragDrop;
        property  OnDragOver;
        property  OnEndDrag;
        property  OnEnter;
        property  OnExit;
        property  OnStartDrag;
      end;procedure Register;resourcestring
      dtlServerPage = 'ActiveX';  dtlOcxPage = 'ActiveX';implementationuses ComObj;procedure TDrawer.InitControlData;
    const
      CControlData: TControlData2 = (
        ClassID: '{83521820-CEE7-41B5-9539-DE89C3BDFCF3}';
        EventIID: '';
        EventCount: 0;
        EventDispIDs: nil;
        LicenseKey: nil (*HR:$80004005*);
        Flags: $00000000;
        Version: 401);
    begin
      ControlData := @CControlData;
    end;procedure TDrawer.CreateControl;  procedure DoCreate;
      begin
        FIntf := IUnknown(OleObject) as _DDrawer;
      end;begin
      if FIntf = nil then DoCreate;
    end;function TDrawer.GetControlInterface: _DDrawer;
    begin
      CreateControl;
      Result := FIntf;
    end;procedure TDrawer.showImage(framelenght: Integer; codecType: SYSINT; frameType: LongWord; 
                                width: LongWord; height: LongWord; var frame_in: {??Shortint}OleVariant);  //var frame_in: {??Shortint}OleVariant);

    begin
      DefaultInterface.showImage(framelenght, codecType, frameType, width, height, frame_in);
    end;procedure TDrawer.Hello;
    begin
      DefaultInterface.Hello;
    end;procedure Register;
    begin
      RegisterComponents(dtlOcxPage, [TDrawer]);
    end;
    end.
    我曾经试图改过  这几个接口 var frame_in: {??Shortint}OleVariant 改为 frame_in:pchar 但编译时在
        procedure showImage(framelenght: Integer; codecType: SYSINT; frameType: LongWord; 
                            width: LongWord; height: LongWord; var frame_in: {??Shortint}OleVariant); dispid 1;
      报错
      

  7.   

    将var frame_in: {??Shortint}OleVariant 换成 array of array of char 还是在同样位置报错[Error] DrawerLib_TLB.pas(82): Identifier expected but 'ARRAY' found
    [Error] DrawerLib_TLB.pas(82): Illegal type in OLE automation section: 'Array'
      

  8.   

    各位,大侠,我想问一下,这个随控件导入的TLB文件能修改吗?
      

  9.   

    修改应该是可以的,不过用OCX一般会麻烦点,经常有些意外状况出现,最好是用dll
      

  10.   

     可能是指針的問題,你用: PSafeArray 這個來傳試下。
      

  11.   

    这个问题还没解决啊C++里面对frame_in都做了什么处理啊?类型不匹配是OCX报的还是delphi调用OCX前报的
      

  12.   

    我也遇到同样问题,如果不知道 函数参数 olevarinat的实际类型岂不是白费