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

Lazarus Pascal Discussion :

Conversion de headers C vers Delphi / FreePascal


Sujet :

Lazarus Pascal

  1. #1
    Membre confirmé

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2013
    Messages
    343
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Novembre 2013
    Messages : 343
    Points : 536
    Points
    536
    Billets dans le blog
    2
    Par défaut Conversion de headers C vers Delphi / FreePascal
    Bjr à tous,

    J'utilise une librairie DLL (phidgets21.dll), fournie avec un fichier d'entête C/C++.

    Certaines fonctions de cette DLL ont un prototype de la forme:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    __declspec (dllimport)   int __stdcall CPhidget_set_OnAttach_Handler (CPhidgetHandle phid, int (__stdcall * fptr) (CPhidgetHandle phid, void *userPtr), void *userPtr);
    prototype dans lequel se trouve une fonction imbriquée dans la liste des paramètres.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    type CInteger = type Integer;
    type TFunctionStdCallFPtr      = function(phid: CPhidgetHandle; userPtr: Pointer): CInteger; stdcall;
    type PTFunctionStdCallFPtr     = Pointer; //^TFunctionStdCallFPtr;
    //...
     function CPhidget_set_OnAttach_Handler(phid: CPhidgetHandle; fptr: PTFunctionStdCallFPtr; userPtr: Pointer): CInteger; stdcall; external PHIDGETS_LIBRARY;
    Jusqu'ici, aucun problème.

    Maintenant, j'ai un autre callback contenant une fonction imbriquée, avec réutilisation du paramètre data:
    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
     
     
    /**
     * Timestamp structure - usually initialized to 0.
     */
    typedef struct _CPhidget_Timestamp
    {
     int seconds;			/**< Number of seconds since timing began */
     int microseconds;		/**< Number of microseconds since last second passed - range is 0 - 999999 */
    } CPhidget_Timestamp, *CPhidget_TimestampHandle;
    //------------------
    /**
     * Timestamped position data returned by the \ref CPhidgetSpatial_set_OnSpatialData_Handler event.
     */
         typedef struct _CPhidgetSpatial_SpatialEventData
         {
          double acceleration[3];	/**< Acceleration data for up to 3 axes. */
          double angularRate[3];   /**< Angular rate data (Gyroscope) for up to 3 axes */
          double magneticField[3];	 /**< Magnetic field data (Compass) for up to 3 axes */
          CPhidget_Timestamp timestamp;   /**< Hardware timestamp */
         } CPhidgetSpatial_SpatialEventData, *CPhidgetSpatial_SpatialEventDataHandle;
     
    //...............
    // 
    __declspec (dllimport)
         int __stdcall CPhidgetSpatial_set_OnSpatialData_Handler (CPhidgetSpatialHandle phid,
    							      int (__stdcall * fptr) (CPhidgetSpatialHandle phid, void *userPtr, CPhidgetSpatial_SpatialEventDataHandle * data, int dataCount),
    							      void *userPtr);
    Je sèche complètement sur deux points:
    • Alignement des structures
    • Comment traduire ce prototype en prototype Pascal


    cdlt

  2. #2
    Expert confirmé
    Avatar de anapurna
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2002
    Messages
    3 421
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Mai 2002
    Messages : 3 421
    Points : 5 820
    Points
    5 820
    Par défaut
    salut

    qu'est ce qui te gene ?

    si tu traduit
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    __declspec (dllimport)   int __stdcall CPhidget_set_OnAttach_Handler (CPhidgetHandle phid, int (__stdcall * fptr) (CPhidgetHandle phid, void *userPtr), void *userPtr);
    par
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    type CInteger = type Integer;
    type TFunctionStdCallFPtr      = function(phid: CPhidgetHandle; userPtr: Pointer): CInteger; stdcall;
    type PTFunctionStdCallFPtr     = Pointer; //^TFunctionStdCallFPtr;
    //...
     function CPhidget_set_OnAttach_Handler(phid: CPhidgetHandle; fptr: PTFunctionStdCallFPtr; userPtr: Pointer): CInteger; stdcall; external PHIDGETS_LIBRARY;
    pourquoi ne fait tu pas la même chose pour ça
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    __declspec (dllimport)
         int __stdcall CPhidgetSpatial_set_OnSpatialData_Handler (CPhidgetSpatialHandle phid,
    			      int (__stdcall * fptr) (CPhidgetSpatialHandle phid, void *userPtr, CPhidgetSpatial_SpatialEventDataHandle * data, int dataCount),
    							      void *userPtr);

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    type 
       CInteger = type Integer;
       TFunctionStdCallFPtr2    = function(phid : CPhidgetSpatialHandle,userPtr: Pointer, data : Pointer;dataCount: Integer): CInteger; stdcall;
       PTFunctionStdCallFPtr2     = Pointer; //^TFunctionStdCallFPtr2;
    //...
       Function CPhidgetSpatial_set_OnSpatialData_Handler (phid : CPhidgetSpatialHandle; fptr : PTFunctionStdCallFPtr2,userPtr : Pointer): CInteger; stdcall;external PHIDGETS_LIBRARY;
    ou j'ai pas compris la question
    Nous souhaitons la vérité et nous trouvons qu'incertitude. [...]
    Nous sommes incapables de ne pas souhaiter la vérité et le bonheur, et sommes incapables ni de certitude ni de bonheur.
    Blaise Pascal
    PS : n'oubliez pas le tag

  3. #3
    Membre confirmé

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2013
    Messages
    343
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Novembre 2013
    Messages : 343
    Points : 536
    Points
    536
    Billets dans le blog
    2
    Par défaut
    Bon, je progresse un peu:

    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
     
    {$PACKRECORDS 4}
    // typedef struct _CPhidgetSpatial *CPhidgetSpatialHandle;
    type CPhidget_Timestamp = record
      seconds       : CInteger;
      microseconds  : CInteger
    end;
    type CPhidgetSpatial_SpatialEventData = record
      acceleration     : array[0..2] of CDouble;	//**< Acceleration data for up to 3 axes. */
      angularRate      : array[0..2] of CDouble; //**< Angular rate data (Gyroscope) for up to 3 axes */
      magneticField    : array[0..2] of CDouble; //**< Magnetic field data (Compass) for up to 3 axes */
      timestamp        : CPhidget_Timestamp;    //**< Hardware timestamp */
    end;
    type CPhidgetSpatial_SpatialEventDataHandle = ^CPhidgetSpatial_SpatialEventData;
    type pCPhidgetSpatial_SpatialEventDataHandle = ^CPhidgetSpatial_SpatialEventDataHandle;
    type TFunctionSpatialEventHandle = function(phid: CPhidgetHandle; userPtr: Pointer; data: pCPhidgetSpatial_SpatialEventDataHandle ; dataCount: CInteger): CInteger; stdcall;
    type PTFunctionSpatialEventHandle = Pointer; //^TFunctionSpatialEventHandle;
     
     
    type TFunctionStdCallFPtr      = function(phid: CPhidgetHandle; userPtr: Pointer): CInteger; stdcall;
    type PTFunctionStdCallFPtr     = Pointer; //^TFunctionStdCallFPtr;
    type TFunctionStdCallErrorFPtr = function(phid: CPhidgetHandle; userPtr: Pointer; ErrorCode: CInteger): CInteger; stdcall;
    type PTFunctionStdCallErrorFPtr = Pointer; //^TFunctionStdCallErrorFPtr;
    La fonction de callback:
    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
     
    //callback that will run at datarate
    //data - array of spatial event data structures that holds the spatial data packets that were sent in this event
    //count - the number of spatial data event packets included in this event
     
    function  SpatialDataHandler(spatial: CPhidgetSpatialHandle; userptr: Pointer;
                                 data   : pCPhidgetSpatial_SpatialEventDataHandle ;
                                 count: CInteger): CInteger; stdcall;
    var
      i: Integer;
      DT: CPhidgetSpatial_SpatialEventData;
    begin
      if (count = 0) then Exit(0);
      try
        for i := 0 to count - 1 do
        begin
          DT := CPhidgetSpatial_SpatialEventData(data^^); // double déréférencement
          if (SameValue(DT.magneticField[0], PUNK_DBL)) then exit(0);
          Form1.Memo2.Lines.Add(Format('=== Data Set: %d  of %d ===', [i, Count - 1]));
    		  Form1.Memo2.Lines.Add(Format('Acceleration> x: %.6f  y: %.6f  z: %.6f', [DT.acceleration[0], DT.acceleration[1], DT.acceleration[2]]));
    		  Form1.Memo2.Lines.Add(Format('Angular Rate> x: %.6f  y: %.6f  z: %.6f', [DT.angularRate[0], DT.angularRate[1], DT.angularRate[2]]));
    		  Form1.Memo2.Lines.Add(Format('Magnetic Field> x: %.6f  y: %.6f  z: %.6f', [DT.magneticField[0], DT.magneticField[1], DT.magneticField[2]]));
    		  Form1.Memo2.Lines.Add(Format('Timestamp> seconds: %d -- microseconds: %d', [DT.timestamp.seconds, DT.timestamp.microseconds]));
          //*)
        end;
     
      except
        ;
      end;
      Result := 0;
    end;
    Et son utilisation:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
      // ...
      R := CPhidgetSpatial_set_OnSpatialData_Handler(FSpatial, @SpatialDataHandler, nil);
      // ...
    Jusqu'ici, tout va bien.

    Maintenant, à la libération par

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
        CPhidget_close(FSpatial);  // freeze
    ,

    j'ai un joli freeze de l'appli

    La traduction d'entêtes .h en Delphi/Pascal est un cauchemar avec toutes ces déclarations imbriquées.

  4. #4
    Expert confirmé
    Avatar de anapurna
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2002
    Messages
    3 421
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Mai 2002
    Messages : 3 421
    Points : 5 820
    Points
    5 820
    Par défaut
    salut

    peut être faut il désactiver le OnSpatialData_Handler
    un truc du genre
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
      // ...
      R := CPhidgetSpatial_set_OnSpatialData_Handler(FSpatial,nil, nil);
      // ...
    avant de fermer le handle general
    Nous souhaitons la vérité et nous trouvons qu'incertitude. [...]
    Nous sommes incapables de ne pas souhaiter la vérité et le bonheur, et sommes incapables ni de certitude ni de bonheur.
    Blaise Pascal
    PS : n'oubliez pas le tag

  5. #5
    Membre confirmé

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2013
    Messages
    343
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Novembre 2013
    Messages : 343
    Points : 536
    Points
    536
    Billets dans le blog
    2
    Par défaut
    Citation Envoyé par anapurna Voir le message
    salut

    peut être faut il désactiver le OnSpatialData_Handler
    un truc du genre
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
      // ...
      R := CPhidgetSpatial_set_OnSpatialData_Handler(FSpatial,nil, nil);
      // ...
    avant de fermer le handle general
    C'est ce que j'ai fait avant de poster, mais le pb persiste

  6. #6
    Expert confirmé
    Avatar de anapurna
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2002
    Messages
    3 421
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Mai 2002
    Messages : 3 421
    Points : 5 820
    Points
    5 820
    Par défaut
    salut

    a priori le

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    function CPhidget_close(phid:CPhidgetHandle):longint;
    et le pendant de

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    function CPhidget_open(phid:CPhidgetHandle; serialNumber:longint):longint;
    donc il s'applique à ton handle global et pas uniquement au spatial ...
    le fait que le handle ne soit pas correctement defini peut produire des probleme
    si il ne retrouve pas ses petits il y a des chance que ton programme parte dans les chou

    je suppose que pour que cela fonction il faut attacher et detacher ton handle au 'Phidget' globale
    Nous souhaitons la vérité et nous trouvons qu'incertitude. [...]
    Nous sommes incapables de ne pas souhaiter la vérité et le bonheur, et sommes incapables ni de certitude ni de bonheur.
    Blaise Pascal
    PS : n'oubliez pas le tag

Discussions similaires

  1. Conversion de structures C vers Delphi
    Par sinfoni dans le forum API, COM et SDKs
    Réponses: 3
    Dernier message: 08/05/2010, 19h15
  2. [TP7] Conversion de sources Turbo Pascal 7 vers Delphi 6
    Par M.Tamisier dans le forum Turbo Pascal
    Réponses: 2
    Dernier message: 21/04/2006, 21h08
  3. conversion de Turbo Pascal vers Delphi 5
    Par samir1674 dans le forum Langage
    Réponses: 5
    Dernier message: 28/11/2005, 17h03
  4. Conversion Delphi 2 vers Delphi 2005
    Par ROYER dans le forum Bases de données
    Réponses: 10
    Dernier message: 25/11/2005, 18h45

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