我想转换接口但是出现access violate 的错误
var
   t: Olevariant;
   myi: MyInterface;
   i: integer;
begin
   myi:= MyInterface(IDispatch(socketconnection1.AppServer));
   myi.sendretparam(t,i);
   showmessage(t);
end;我想调用queryinterface﹐但出现这样的错误
[Error] Unit1.pas(87): Type not allowed in Variant Dispatch callvar
   t: Olevariant;
   myi: MyInterface;
   i: integer;
begin
   myi:= socketconnection1.AppServer.queryinterface(IID_MyInterface,MyInterface);
   myi.sendretparam(t,i);
   showmessage(t);
end;

解决方案 »

  1.   

    socketconnection1.Connected:= false;
    socketconnection1.ServerName:= MyInterface;
    socketconnection1.Connected:= true;
    socketconnection1.AppServer.queryinterface不知道是不是这个意思
      

  2.   

    回复人: lihao_ningxia(耗子) 
    你說什么聲明??
      

  3.   

    TSocketconnection 不能这样调用.你可以从TSocketconnection源码看出来,它只是实现IDispatch方式的分派调用,
    它没有实现对远程对象的查询接口.因为是远程调用,用强制接口转换更没有成功可能.
      

  4.   

    halfdream(哈欠):怎么樣能解決這樣的問題呢
      

  5.   

    這是我的接口單元
    unit server_TLB;{$TYPEDADDRESS OFF} // Unit must be compiled without type-checked pointers. 
    {$WARN SYMBOL_PLATFORM OFF}
    {$WRITEABLECONST ON}interfaceuses ActiveX, Classes, Graphics, Midas, StdVCL, Variants, Windows;
      
    // *********************************************************************//
    // 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
      serverMajorVersion = 1;
      serverMinorVersion = 0;  LIBID_server: TGUID = '{DE154303-0D86-4F49-B734-4E44F3A138A5}';  IID_Iservermodule: TGUID = '{3BB6C61B-1920-4F82-9841-9286CFAA17EA}';
      IID_MyInterface: TGUID = '{B6880ABC-15DF-4CCB-B616-721FAC4949FB}';
      CLASS_servermodule: TGUID = '{BA33845C-8421-4367-9385-B48108ED6FDE}';
    type// *********************************************************************//
    // Forward declaration of types defined in TypeLibrary                    
    // *********************************************************************//
      Iservermodule = interface;
      IservermoduleDisp = dispinterface;
      MyInterface = interface;
      MyInterfaceDisp = dispinterface;// *********************************************************************//
    // Declaration of CoClasses defined in Type Library                       
    // (NOTE: Here we map each CoClass to its Default Interface)              
    // *********************************************************************//
      servermodule = Iservermodule;
    // *********************************************************************//
    // Interface: Iservermodule
    // Flags:     (4416) Dual OleAutomation Dispatchable
    // GUID:      {3BB6C61B-1920-4F82-9841-9286CFAA17EA}
    // *********************************************************************//
      Iservermodule = interface(IAppServer)
        ['{3BB6C61B-1920-4F82-9841-9286CFAA17EA}']
        function  GetTableNames: OleVariant; safecall;
        function  ExecStoredproc(const storedprocName: WideString; const parameters: WideString): OleVariant; safecall;
      end;// *********************************************************************//
    // DispIntf:  IservermoduleDisp
    // Flags:     (4416) Dual OleAutomation Dispatchable
    // GUID:      {3BB6C61B-1920-4F82-9841-9286CFAA17EA}
    // *********************************************************************//
      IservermoduleDisp = dispinterface
        ['{3BB6C61B-1920-4F82-9841-9286CFAA17EA}']
        function  GetTableNames: OleVariant; dispid 1;
        function  ExecStoredproc(const storedprocName: WideString; const parameters: WideString): OleVariant; dispid 2;
        function  AS_ApplyUpdates(const ProviderName: WideString; Delta: OleVariant; 
                                  MaxErrors: Integer; out ErrorCount: Integer; var OwnerData: OleVariant): OleVariant; dispid 20000000;
        function  AS_GetRecords(const ProviderName: WideString; Count: Integer; out RecsOut: Integer; 
                                Options: Integer; const CommandText: WideString; 
                                var Params: OleVariant; var OwnerData: OleVariant): OleVariant; dispid 20000001;
        function  AS_DataRequest(const ProviderName: WideString; Data: OleVariant): OleVariant; dispid 20000002;
        function  AS_GetProviderNames: OleVariant; dispid 20000003;
        function  AS_GetParams(const ProviderName: WideString; var OwnerData: OleVariant): OleVariant; dispid 20000004;
        function  AS_RowRequest(const ProviderName: WideString; Row: OleVariant; RequestType: Integer; 
                                var OwnerData: OleVariant): OleVariant; dispid 20000005;
        procedure AS_Execute(const ProviderName: WideString; const CommandText: WideString; 
                             var Params: OleVariant; var OwnerData: OleVariant); dispid 20000006;
      end;// *********************************************************************//
    // Interface: MyInterface
    // Flags:     (4416) Dual OleAutomation Dispatchable
    // GUID:      {B6880ABC-15DF-4CCB-B616-721FAC4949FB}
    // *********************************************************************//
      MyInterface = interface(IAppServer)
        ['{B6880ABC-15DF-4CCB-B616-721FAC4949FB}']
        procedure sendretparam(var tt: OleVariant; Param2: Integer); safecall;
      end;// *********************************************************************//
    // DispIntf:  MyInterfaceDisp
    // Flags:     (4416) Dual OleAutomation Dispatchable
    // GUID:      {B6880ABC-15DF-4CCB-B616-721FAC4949FB}
    // *********************************************************************//
      MyInterfaceDisp = dispinterface
        ['{B6880ABC-15DF-4CCB-B616-721FAC4949FB}']
        procedure sendretparam(var tt: OleVariant; Param2: Integer); dispid 3;
        function  AS_ApplyUpdates(const ProviderName: WideString; Delta: OleVariant; 
                                  MaxErrors: Integer; out ErrorCount: Integer; var OwnerData: OleVariant): OleVariant; dispid 20000000;
        function  AS_GetRecords(const ProviderName: WideString; Count: Integer; out RecsOut: Integer; 
                                Options: Integer; const CommandText: WideString; 
                                var Params: OleVariant; var OwnerData: OleVariant): OleVariant; dispid 20000001;
        function  AS_DataRequest(const ProviderName: WideString; Data: OleVariant): OleVariant; dispid 20000002;
        function  AS_GetProviderNames: OleVariant; dispid 20000003;
        function  AS_GetParams(const ProviderName: WideString; var OwnerData: OleVariant): OleVariant; dispid 20000004;
        function  AS_RowRequest(const ProviderName: WideString; Row: OleVariant; RequestType: Integer; 
                                var OwnerData: OleVariant): OleVariant; dispid 20000005;
        procedure AS_Execute(const ProviderName: WideString; const CommandText: WideString; 
                             var Params: OleVariant; var OwnerData: OleVariant); dispid 20000006;
      end;// *********************************************************************//
    // The Class Coservermodule provides a Create and CreateRemote method to          
    // create instances of the default interface Iservermodule exposed by              
    // the CoClass servermodule. The functions are intended to be used by             
    // clients wishing to automate the CoClass objects exposed by the         
    // server of this typelibrary.                                            
    // *********************************************************************//
      Coservermodule = class
        class function Create: Iservermodule;
        class function CreateRemote(const MachineName: string): Iservermodule;
      end;implementationuses ComObj;class function Coservermodule.Create: Iservermodule;
    begin
      Result := CreateComObject(CLASS_servermodule) as Iservermodule;
    end;class function Coservermodule.CreateRemote(const MachineName: string): Iservermodule;
    begin
      Result := CreateRemoteComObject(MachineName, CLASS_servermodule) as Iservermodule;
    end;end.