IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Delphi Discussion :

Transtypage d'un Variant résultant de CreateOleObject()


Sujet :

Langage Delphi

  1. #1
    Membre du Club
    Inscrit en
    Décembre 2002
    Messages
    74
    Détails du profil
    Informations forums :
    Inscription : Décembre 2002
    Messages : 74
    Points : 44
    Points
    44
    Par défaut Transtypage d'un Variant résultant de CreateOleObject()
    Context :
    - Delphi7
    - windows 2000 ou XP
    - Composant ActiveX livré avec SAPGui (librairie SAPfewse.ocx)

    Question : Comment transtyper une variable de type variant en une variable d'un type connu (TGuiApplication) sachant que la stucture de l'object pointé par la variable de type variant pointe sur une structure de type TGuiApplication

    Dans l'exemple suivant :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    uses ..., SAPFEWSELib_TLB, ...
    const Ct1 = 'ConnectionString';
     
    procedure TForm1.Button1Click(Sender: TObject);
    var
      AVariant1,AVariant2,AVariant3  : Variant;
    begin
      AVariant1 := CreateOleObject('Sapgui.ScriptingCtrl.1');
      AVariant2 := AVariant1 .GetScriptingEngine;
      AVariant3 := AVariant2 .OpenConnectionByConnectionString;(Ct1 ,True,True);
    end;
    Comment transtyper le variant AVariant2 en type SAPGuiApplication
    pour permettre ensuite d'écrire :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
     
    procedure TForm1.Button1Click(Sender: TObject);
    var
      AVariant1,AVariant2  : Variant;
      AGuiApplication : TGuiApplication;
      AGuiConnection : TGuiConnection; 
    begin
      AVariant1 := CreateOleObject('Sapgui.ScriptingCtrl.1');
      AVariant2 := AVariant1 .GetScriptingEngine;
     
    // bien sur la ligne qui suit génère une erreur 
      AGuiApplication := AVariant2 as TGuiApplication;
     
      AGuiConnection := AGuiApplication.OpenConnectionByConnectionString(Ct1,True,True);
    end;

  2. #2
    Membre confirmé
    Avatar de Philippe Gormand
    Inscrit en
    Mars 2002
    Messages
    330
    Détails du profil
    Informations forums :
    Inscription : Mars 2002
    Messages : 330
    Points : 647
    Points
    647
    Par défaut Variant
    Bonjour.

    A mon avis, impossible !

    Le type Variant est justement conçu pour pointer sur des types de
    données qu'on ne connait pas.

    Or, avec le OLE on pointe sur des programmes, tous differents des autres.
    Donc, autant vouloir trantyper un objet WORD 2000 en objet TWAIN32.
    Sérieusment est ce possible ? Si j'ai bien compris la question !

    Explique ce que tu veux vraiment faire pour que l'on puisse t'orienter
    correctement.

    A+
    Rien n'est moins sur que l'incertain : Pierre DAC

  3. #3
    Rédacteur


    Profil pro
    Inscrit en
    Janvier 2003
    Messages
    7 171
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2003
    Messages : 7 171
    Points : 15 060
    Points
    15 060
    Billets dans le blog
    1
    Par défaut
    Salut,
    communique nous la déclaration de TGuiApplication.

  4. #4
    Membre du Club
    Inscrit en
    Décembre 2002
    Messages
    74
    Détails du profil
    Informations forums :
    Inscription : Décembre 2002
    Messages : 74
    Points : 44
    Points
    44
    Par défaut
    Bonjour,

    Voici une partie de la définition du type TGuiApplication :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
     
    // *********************************************************************//
    // Déclaration de classe proxy de contrôle OLE
    // Nom du contrôle      : TGuiApplication
    // Chaîne d'aide        : SAP Scripting Control
    // Interface par défaut : _Dsapfewse
    // DISP Int. Déf. ?     : Yes
    // Interface événements : _DsapfewseEvents
    // TypeFlags            : (34) CanCreate Control
    // *********************************************************************//
    TGuiApplication = class(TOleControl)
      private
        FOnCreateSession: TGuiApplicationCreateSession;
        FOnDestroySession: TGuiApplicationDestroySession;
        FOnIgnoreSession: TGuiApplicationIgnoreSession;
        FOnError: TGuiApplicationError;
        FIntf: _Dsapfewse;
        function  GetControlInterface: _Dsapfewse;
      protected
        procedure CreateControl;
        procedure InitControlData; override;
        function Get_Parent: GuiComponent;
        function Get_Children: GuiComponentCollection;
        procedure Set_Children(const Value: GuiComponentCollection);
        function Get_Connections: GuiComponentCollection;
        procedure Set_Connections(const Value: GuiComponentCollection);
        function Get_Utils: GuiUtils;
        procedure Set_Utils(const Value: GuiUtils);
      public
        function FindById(const Id: WideString): GuiComponent; overload;
        function FindById(const Id: WideString; Raise_: OleVariant): GuiComponent; overload;
        function OpenConnection(const Description: WideString): GuiConnection; overload;
        function OpenConnection(const Description: WideString; Sync: OleVariant): GuiConnection; overload;
        function OpenConnection(const Description: WideString; Sync: OleVariant; Raise_: OleVariant): GuiConnection; overload;
        function OpenConnectionByConnectionString(const ConnectString: WideString): GuiConnection; overload;
        function OpenConnectionByConnectionString(const ConnectString: WideString; Sync: OleVariant): GuiConnection; overload;
        function OpenConnectionByConnectionString(const ConnectString: WideString; Sync: OleVariant; 
                                                  Raise_: OleVariant): GuiConnection; overload;
        function CreateGuiCollection: IDispatch;
        function AddHistoryEntry(const Fieldname: WideString; const Value: WideString): WordBool;
        function DropHistory: WordBool;
        procedure Quit;
        procedure Ignore(WindowHandle: OLE_HANDLE);
        function GetScriptingEngine: IDispatch;
        function RegisterROT: WordBool;
        procedure RevokeROT;
        property  ControlInterface: _Dsapfewse read GetControlInterface;
        property  DefaultInterface: _Dsapfewse read GetControlInterface;
        property Name: WideString index 32001 read GetWideStringProp;
        property type_: WideString index 32015 read GetWideStringProp;
        property TypeAsNumber: Integer index 32032 read GetIntegerProp;
        property ContainerType: WordBool index 32033 read GetWordBoolProp;
        property Id: WideString index 32025 read GetWideStringProp;
        property Parent: GuiComponent read Get_Parent;
        property ToolbarVisible: WordBool index 32901 read GetWordBoolProp write SetWordBoolProp;
        property StatusbarVisible: WordBool index 32902 read GetWordBoolProp write SetWordBoolProp;
        property ButtonbarVisible: WordBool index 32903 read GetWordBoolProp write SetWordBoolProp;
        property TitlebarVisible: WordBool index 32904 read GetWordBoolProp write SetWordBoolProp;
        property Inplace: WordBool index 32907 read GetWordBoolProp write SetWordBoolProp;
      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;
        property Children: GuiComponentCollection read Get_Children write Set_Children stored False;
        property Connections: GuiComponentCollection read Get_Connections write Set_Connections stored False;
        property MajorVersion: Integer index 32909 read GetIntegerProp write SetIntegerProp stored False;
        property MinorVersion: Integer index 32910 read GetIntegerProp write SetIntegerProp stored False;
        property Patchlevel: Integer index 32919 read GetIntegerProp write SetIntegerProp stored False;
        property Revision: Integer index 32920 read GetIntegerProp write SetIntegerProp stored False;
        property NewVisualDesign: WordBool index 32912 read GetWordBoolProp write SetWordBoolProp stored False;
        property Utils: GuiUtils read Get_Utils write Set_Utils stored False;
        property HistoryEnabled: WordBool index 32916 read GetWordBoolProp write SetWordBoolProp stored False;
        property ConnectionErrorText: WideString index 32924 read GetWideStringProp write SetWideStringProp stored False;
        property OnCreateSession: TGuiApplicationCreateSession read FOnCreateSession write FOnCreateSession;
        property OnDestroySession: TGuiApplicationDestroySession read FOnDestroySession write FOnDestroySession;
        property OnIgnoreSession: TGuiApplicationIgnoreSession read FOnIgnoreSession write FOnIgnoreSession;
        property OnError: TGuiApplicationError read FOnError write FOnError;
      end;
     
    // *********************************************************************//
    // DispIntf :  _Dsapfewse
    // Flags :     (4096) Dispatchable
    // GUID :      {93A37525-9118-4731-92A7-B93DF1E34455}
    // *********************************************************************//
      _Dsapfewse = dispinterface
        ['{93A37525-9118-4731-92A7-B93DF1E34455}']
        property Name: WideString readonly dispid 32001;
        property type_: WideString readonly dispid 32015;
        property TypeAsNumber: Integer readonly dispid 32032;
        property ContainerType: WordBool readonly dispid 32033;
        property Id: WideString readonly dispid 32025;
        property Parent: GuiComponent readonly dispid 32038;
        property Children: GuiComponentCollection dispid 32019;
        property Connections: GuiComponentCollection dispid 32900;
        property ToolbarVisible: WordBool dispid 32901;
        property StatusbarVisible: WordBool dispid 32902;
        property ButtonbarVisible: WordBool dispid 32903;
        property TitlebarVisible: WordBool dispid 32904;
        property Inplace: WordBool dispid 32907;
        property MajorVersion: Integer dispid 32909;
        property MinorVersion: Integer dispid 32910;
        property Patchlevel: Integer dispid 32919;
        property Revision: Integer dispid 32920;
        property NewVisualDesign: WordBool dispid 32912;
        property Utils: GuiUtils dispid 32917;
        property HistoryEnabled: WordBool dispid 32916;
        property ConnectionErrorText: WideString dispid 32924;
        function FindById(const Id: WideString; Raise_: OleVariant): GuiComponent; dispid 32029;
        function OpenConnection(const Description: WideString; Sync: OleVariant; Raise_: OleVariant): GuiConnection; dispid 32905;
        function OpenConnectionByConnectionString(const ConnectString: WideString; Sync: OleVariant; 
                                                  Raise_: OleVariant): GuiConnection; dispid 32918;
        function CreateGuiCollection: IDispatch; dispid 32911;
        function AddHistoryEntry(const Fieldname: WideString; const Value: WideString): WordBool; dispid 32913;
        function DropHistory: WordBool; dispid 32914;
        procedure Quit; dispid 32906;
        procedure Ignore(WindowHandle: OLE_HANDLE); dispid 32908;
        function GetScriptingEngine: IDispatch; dispid 1;
        function RegisterROT: WordBool; dispid 32921;
        procedure RevokeROT; dispid 32923;
      end;

  5. #5
    Membre expert
    Avatar de TicTacToe
    Inscrit en
    Septembre 2005
    Messages
    1 940
    Détails du profil
    Informations personnelles :
    Âge : 51

    Informations forums :
    Inscription : Septembre 2005
    Messages : 1 940
    Points : 3 575
    Points
    3 575
    Par défaut
    en faisant ceci ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    type
      PTGuiApplication := ^TGuiApplication;
    var
      PGA : PTGuiApplication;
     
    begin
      PGA := MonVariant; // si probleme, éventuellement caster avec PTGuiApplication ou Pointer
      PGA^.GetScriptingEngine...
    ..
    bon courage !
    Section Delphi
    La mine d'or: La FAQ, les Sources

    Un développement compliqué paraitra simple pour l'utilisateur, frustrant non ?
    Notre revanche ? l'inverse est aussi vrai ;-)

  6. #6
    Rédacteur


    Profil pro
    Inscrit en
    Janvier 2003
    Messages
    7 171
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2003
    Messages : 7 171
    Points : 15 060
    Points
    15 060
    Billets dans le blog
    1
    Par défaut
    Salut,
    crée directement un TGuiApplication qui je crois encapsule ton interface.

    Pour associer une interface à un TOleServer on procéde ainsi :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
          WMILastError:=TSWbemLastError.Create(Self);
           With WMILastError do
             begin
              AutoConnect:=False;
              ConnectKind :=ckAttachToInterface;
              ConnectTo(IUnknown(objWbemErrorObject) as ISWbemLastError);
             end;
    Le type TSWbemLastError étant une enveloppe et objWbemErrorObject étant l'interface existante qu'on souhaite associer.

    Pour associer une interface à un TOleControl et avec ta maniére de faire je verrais bien qq chose comme :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
     AVariant1 := CreateOleObject('Sapgui.ScriptingCtrl.1'); 
     MonOleControl:=TGuiApplication.Create(Self)
     MonOleControl.DestroyControl;
     MonOleControl.OleObject:=AVariant1 as _Dsapfewse;
    Mais je n'ai jamais fait et je ne suis pas certain que cela soit pertinent.

    En tout cas TicTacToe pas de manipulation de pointeur car ici il s'agit d'objet COM donc d'interface, un variant pouvant être vue comme une interface de type IDispatch selon son contenu.

  7. #7
    Membre expert
    Avatar de TicTacToe
    Inscrit en
    Septembre 2005
    Messages
    1 940
    Détails du profil
    Informations personnelles :
    Âge : 51

    Informations forums :
    Inscription : Septembre 2005
    Messages : 1 940
    Points : 3 575
    Points
    3 575
    Par défaut
    Ok Laurent

    j'ai mis ceci vraiment sans conviction

    bon à savoir
    Section Delphi
    La mine d'or: La FAQ, les Sources

    Un développement compliqué paraitra simple pour l'utilisateur, frustrant non ?
    Notre revanche ? l'inverse est aussi vrai ;-)

  8. #8
    Rédacteur


    Profil pro
    Inscrit en
    Janvier 2003
    Messages
    7 171
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2003
    Messages : 7 171
    Points : 15 060
    Points
    15 060
    Billets dans le blog
    1
    Par défaut
    La solution que je te proposais ne fonctionne pas car la méthode DestroyControl est privée et la propriété OleObject en ReadOnly .

    Par contre créer un TOleControl puis récupérer l'interface associée via la propriété ControlInterface est possible.

  9. #9
    Membre du Club
    Inscrit en
    Décembre 2002
    Messages
    74
    Détails du profil
    Informations forums :
    Inscription : Décembre 2002
    Messages : 74
    Points : 44
    Points
    44
    Par défaut Transtypage d'un Variant résultant de CreateOleObject()
    Merci si vous pouviez me donner un exemple

    Cordialement

  10. #10
    Rédacteur


    Profil pro
    Inscrit en
    Janvier 2003
    Messages
    7 171
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2003
    Messages : 7 171
    Points : 15 060
    Points
    15 060
    Billets dans le blog
    1
    Par défaut
    Par exemple comme ceci
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    Var MonInterface : _Dsapfewse;
    begin
     MonOleControl:=TGuiApplication.Create(Self) 
     MonInterface:=MonOleControl.ControlInterface;
    ...

Discussions similaires

  1. Transtypage d'un variant.
    Par Lodonez vincent dans le forum Langage
    Réponses: 4
    Dernier message: 02/09/2004, 19h42
  2. conversion : VARIANT FAR* URL vers CString
    Par kam dans le forum MFC
    Réponses: 2
    Dernier message: 29/03/2004, 13h32
  3. [C++]closure + héritage + transtypage
    Par JEG dans le forum C++Builder
    Réponses: 11
    Dernier message: 30/01/2004, 14h26
  4. Convertion de type VARIANT à type CString
    Par j_grue dans le forum MFC
    Réponses: 2
    Dernier message: 07/11/2002, 14h18
  5. [transtypage]PChar et WideString
    Par rbag dans le forum Bases de données
    Réponses: 2
    Dernier message: 05/09/2002, 20h12

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo