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

C Discussion :

Récupérer les données d'un type VARIANT


Sujet :

C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre émérite

    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2013
    Messages
    32
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2013
    Messages : 32
    Par défaut Récupérer les données d'un type VARIANT
    Bonjour,
    Je suis déjà venu pour un problème au sujet des objets COM (http://www.developpez.net/forums/d13...vec-objet-com/) et j'ai depuis bien approfondi le sujet et appris dessus.

    Cependant, pour la suite de mon projet, je suis confronté à la dernière difficulté de l'utilisation de ces objets : le type VARIANT.
    Je me suis bien entendu renseigné sur sa composition et j'ai compris que c'était un type au format variable dont on pouvait connaître le type au moment t grâce à son membre vt. Soit.
    Seulement j'ai des fonctions qui sont censées me renvoyer des valeurs dans un tableau (au moins 5 valeurs) et ces fonctions prennent toutes deux paramètres : VARIANT et unsigned long *. Le deuxième paramètre me renvoie toujours 6 et pas une autre valeur supplémentaire si j'envoie un tableau prédéfini (si c'est l'adresse d'une variable unique c'est aussi le même résultat).

    Le premier, ne renvoie rien, si j'initialise le type VARIANT il reste identique à ce qui a été fait avant, sinon cela donne des valeurs aléatoires en son sein (ce qui est logique).
    Cela s'explique sans doute par le fait que le type en paramètre n'est pas un pointeur et du coup la modification ne peut me revenir.

    Mais dans ce cas, comment je récupère les données voulues ?

    Pour que ce soit plus clair, voici mon code d'essai :

    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
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    #include <stdio.h>
    #include <windows.h>
    #include <objbase.h>
    #include <initguid.h>
     
    #define INTERFACE   IRobX
     
    DECLARE_INTERFACE_ (INTERFACE, IDispatch)
    {
        /* Implémentation de la classe IUnknown */
        STDMETHOD   (QueryInterface)		                (THIS_ GUID *, void **) PURE;
    	STDMETHOD_  (ULONG, AddRef)		                    (THIS) PURE;
    	STDMETHOD_  (ULONG, Release)                        (THIS) PURE;
     
        /* De même pour la classe IDispatch */
        STDMETHOD_ (HRESULT, GetTypeInfoCount)              (THIS_ UINT *) PURE;
        STDMETHOD_ (HRESULT, GetTypeInfo)                   (THIS_ UINT, LCID, ITypeInfo **) PURE;
        STDMETHOD_ (HRESULT, GetIDsOfNames)                 (THIS_ REFIID, LPOLESTR *, UINT, LCID, DISPID *) PURE;
        STDMETHOD_ (HRESULT, Invoke)                        (THIS_ DISPID, REFIID, LCID, WORD, DISPPARAMS *, VARIANT *, EXCEPINFO *, UINT *) PURE;
     
        /* Méthodes de la classe spécifique au robot */
    	STDMETHOD_  (HRESULT, CloseComm)                    (THIS_ ULONG *) PURE;
        STDMETHOD_  (HRESULT, Energize)	                    (THIS_ ULONG, ULONG *) PURE;
        STDMETHOD_  (HRESULT, GetErrorInfo)	                (THIS_ ULONG, BSTR *) PURE;
        STDMETHOD_  (HRESULT, GetCurrentPosition)	        (THIS_ VARIANT, ULONG *) PURE;
        STDMETHOD_  (HRESULT, GetString)	                (THIS_ VARIANT, ULONG *) PURE;
        STDMETHOD_  (HRESULT, GotoHome)	                    (THIS_ ULONG, ULONG *) PURE;
        STDMETHOD_  (HRESULT, GotoPoint)	                (THIS_ BSTR, ULONG, ULONG *) PURE;
        STDMETHOD_  (HRESULT, GotoPosition)	                (THIS_ LONG, LONG, LONG, LONG, LONG, ULONG, ULONG *) PURE;
        STDMETHOD_  (HRESULT, GotoUserPosition)	            (THIS_ ULONG, ULONG *) PURE;
        STDMETHOD_  (HRESULT, InitRobot)	                (THIS_ ULONG *) PURE;
        STDMETHOD_  (HRESULT, MemPointPosition)	            (THIS_ LONG, LONG, LONG, LONG, LONG, BSTR, ULONG *) PURE;
        STDMETHOD_  (HRESULT, Move)	                        (THIS_ ULONG, ULONG, ULONG, ULONG, ULONG *) PURE;
        STDMETHOD_  (HRESULT, OpenComm)	                    (THIS_ BSTR, ULONG *) PURE;
        STDMETHOD_  (HRESULT, RestoreUserDefaultPosition)	(THIS_ ULONG *) PURE;
       	STDMETHOD_  (HRESULT, SendString)	                (THIS_ BSTR, ULONG *) PURE;
        STDMETHOD_  (HRESULT, SetSpeed)	                    (THIS_ ULONG, ULONG *) PURE;
        STDMETHOD_  (HRESULT, SetUserPosition)	            (THIS_ LONG, LONG, LONG, LONG, LONG, ULONG *) PURE;
        STDMETHOD_  (HRESULT, Wait)	                        (THIS_ ULONG, ULONG *) PURE;
        STDMETHOD_  (HRESULT, SetLimits)	                (THIS_ LONG, LONG, LONG, LONG, LONG, ULONG *) PURE;
        STDMETHOD_  (HRESULT, RestoreDefaultLimits)	        (THIS_ ULONG *) PURE;
        STDMETHOD_  (HRESULT, GetLimits)	                (THIS_ VARIANT, ULONG *) PURE;
        STDMETHOD_  (HRESULT, SetToolParam)	                (THIS_ LONG, ULONG *) PURE;
        STDMETHOD_  (HRESULT, GetToolParam)	                (THIS_ LONG *) PURE;
        STDMETHOD_  (HRESULT, Align)	                    (THIS_ ULONG, ULONG *) PURE;
        STDMETHOD_  (HRESULT, GetVacuum)	                (THIS_ VARIANT, ULONG *) PURE;
        STDMETHOD_  (HRESULT, Pump)	                        (THIS_ ULONG, ULONG *) PURE;
        STDMETHOD_  (HRESULT, Valve)	                    (THIS_ ULONG, ULONG *) PURE;
        STDMETHOD_  (HRESULT, OpenManualBox)	            (THIS_ ULONG *) PURE;
        STDMETHOD_  (HRESULT, GetSpeed)	                    (THIS_ VARIANT, ULONG *) PURE;
        STDMETHOD_  (HRESULT, WaitForManualBoxEnd)	        (THIS_ ULONG *) PURE;
    	STDMETHOD_  (HRESULT, GetSerialNumber)	            (THIS_ BSTR *) PURE;
    	STDMETHOD_  (HRESULT, GetUserPosition)	            (THIS_ VARIANT, ULONG *) PURE;
    	STDMETHOD_  (HRESULT, Calibrate)	                (THIS_ LONG, ULONG *) PURE;
    	STDMETHOD_  (HRESULT, GetSpeedStatus)	            (THIS_ ULONG *) PURE;
    	STDMETHOD_  (HRESULT, GetSpeedNum)	                (THIS_ ULONG *) PURE;
    	STDMETHOD_  (HRESULT, GetCurrentPositionStatus)	    (THIS_ ULONG *) PURE;
    	STDMETHOD_  (HRESULT, GetCurrentPositionStr)	    (THIS_ BSTR *) PURE;
    	STDMETHOD_  (HRESULT, GetLimitsStatus)	            (THIS_ ULONG *) PURE;
    	STDMETHOD_  (HRESULT, GetLimitsStr)	                (THIS_ BSTR *) PURE;
    	STDMETHOD_  (HRESULT, GetVacuumStatus)	            (THIS_ ULONG *) PURE;
    	STDMETHOD_  (HRESULT, GetVacuumNum)	                (THIS_ ULONG *) PURE;
    	STDMETHOD_  (HRESULT, GetUserPositionStatus)	    (THIS_ ULONG *) PURE;
    	STDMETHOD_  (HRESULT, GetUserPositionStr)	        (THIS_ BSTR *) PURE;
    	STDMETHOD_  (HRESULT, OpenPositionManager)	        (THIS_ BSTR, ULONG *) PURE;
    	STDMETHOD_  (HRESULT, WaitForPositionManagerEnd)	(THIS_ BSTR *) PURE;
    	STDMETHOD_  (HRESULT, GetPositionFile)	            (THIS_ BSTR *) PURE;
    	STDMETHOD_  (HRESULT, SetPositionFile)	            (THIS_ BSTR) PURE;
    	STDMETHOD_  (HRESULT, Laser)	                    (THIS_ ULONG, ULONG *) PURE;
    	STDMETHOD_  (HRESULT, SetAccel)	                    (THIS_ ULONG, ULONG *) PURE;
    	STDMETHOD_  (HRESULT, GetAccel)	                    (THIS_ VARIANT, ULONG *) PURE;
    	STDMETHOD_  (HRESULT, GetAccelStatus)	            (THIS_ ULONG *) PURE;
    	STDMETHOD_  (HRESULT, GetAccelNum)	                (THIS_ ULONG *) PURE;
    };
     
     
    void CoupureRobot(void);
    int InitialisationRobot(char *port);
    void BSTRtoASC(BSTR str, char **strRet);
    void ASCtoBSTR(char *str, BSTR *strRet);
    void OuverturePositionManager(void);
    void PanneauControleRobot(void);
     
    #include <stdio.h>
    #include <OleAuto.h>
    #include <wchar.h>
    #include <string.h>
    #include <tchar.h>
     
    //92507B34-D92D-4F8C-967E-6C5166C0F1F6
    DEFINE_GUID(IID_Rob5x, 0x92507b34, 0xd92d, 0x4f8c, 0x96, 0x7e, 0x6c, 0x51, 0x66, 0xc0, 0xf1, 0xf6);
     
    //5DD00CD6-E135-4799-A83F-62F06688700F
    DEFINE_GUID(CLSID_Rob5x, 0x5dd00cd6, 0xe135, 0x4799, 0xa8, 0x3f, 0x62, 0xf0, 0x66, 0x88, 0x70, 0x0f);
     
    IRobX *Robot = NULL;
     
    void CoupureRobot(void)
    {
        ULONG retVal;
     
        Robot->lpVtbl->CloseComm(Robot, &retVal);
        Robot->lpVtbl->Release(Robot);
        CoUninitialize();
    }
     
    int InitialisationRobot(char *port)
    {
        ULONG resultat, retVal, valeur;
        BSTR chaineBSTR;
     
          /* On récupère l'objet, on lance une instance et on tente une communication avec le robot */
          if(SUCCEEDED((resultat = CoInitialize(NULL)))) {
              if(CoCreateInstance(&CLSID_Rob5x, NULL, CLSCTX_ALL, &IID_Rob5x, (void**) &Robot)){
                  _tprintf(_TEXT("%s\n"), _TEXT("Erreur dans la création de l'instance"));
                  return -42;
              } else {
                  ASCtoBSTR(port, &chaineBSTR);
                  resultat = Robot->lpVtbl->OpenComm(Robot, chaineBSTR, &retVal);
                  Robot->lpVtbl->InitRobot(Robot, &valeur);
              }
          }
    }
     
    void PanneauControleRobot(void)
    {
        ULONG valeur;
     
        Robot->lpVtbl->OpenManualBox(Robot, &valeur);
        Robot->lpVtbl->WaitForManualBoxEnd(Robot, &valeur);
    }
     
    void OuverturePositionManager(void)
    {
        ULONG valeur;
        BSTR chaine;
     
        Robot->lpVtbl->OpenPositionManager(Robot, NULL, &valeur);
        Robot->lpVtbl->WaitForPositionManagerEnd(Robot, &chaine);
    }
     
    /* Deux fonctions pour traduire les chaines de type BSTR vers des types classiques */
    void BSTRtoASC(BSTR str, char **strRet)
    {
        if(str != NULL) {
           ULONG length = WideCharToMultiByte(CP_ACP, 0, str, SysStringLen(str), NULL, 0, NULL, NULL);
           *strRet = malloc(sizeof(char)*length);
           length = WideCharToMultiByte(CP_ACP, 0, str, SysStringLen(str), *strRet, length, NULL, NULL);
           *(strRet)[length-2] = '\0';
        }
    }
     
    void ASCtoBSTR(char *str, BSTR *strRet)
    {
        if(str != NULL) {
            ULONG length = strlen(str);
            int ResultLength = MultiByteToWideChar (CP_ACP,MB_PRECOMPOSED, str,length,NULL,0);
            *strRet = SysAllocStringLen(NULL, ResultLength);
            MultiByteToWideChar (CP_ACP,MB_PRECOMPOSED, str,length,*strRet,ResultLength);
        }
    }
     
    void PrintVariant(VARIANT *var)
    {
    	if(var->vt==VT_UI1)
    		{ wprintf(L"VT_UI1:%u\n",var->bVal); }
    	else if(var->vt==VT_I2)
    		{ wprintf(L"VT_I2:%d\n",var->iVal); }
    	else if(var->vt==VT_I4)
    		{ wprintf(L"I4:%d\n",var->lVal); }
    	else if(var->vt==VT_R4)
    		{ wprintf(L"VT_R4:%f\n",var->fltVal); }
    	else if(var->vt==VT_R8)
    		{ wprintf(L"VT_R8:%lf\n",var->dblVal); }
    	else if(var->vt==VT_BOOL)
    		{
    		wprintf(L"VT_BOOL:");
    		if(var->boolVal==0)
    			{ wprintf(L"false\n"); }
    		else
    			{ wprintf(L"true\n"); }
    		}
    	else if(var->vt==VT_BSTR)
    		{  wprintf(L"VT_BSTR:%s\n",var->bstrVal); }
        else if(var->vt == (VT_BYREF|VT_UI4))
            printf("Tableau d'ULONG....\n");
        else
            printf("Rien dans la liste\n");
    }
     
     
    int main(void)
    {
        VARIANT A;
        ULONG retVal[100] = { 0 };
        HRESULT ret;
        int i = 0;
     
     
        VariantInit(&A);
     
        A.vt = VT_BYREF|VT_UI4;
        A.pulVal = malloc(sizeof(unsigned long) * 5);
        A.pulVal[0] = 1;
        A.pulVal[1] = 2;
        A.pulVal[2] = 3;
        A.pulVal[3] = 4;
        A.pulVal[4] = 5;
     
        InitialisationRobot("COM1");
        ret = Robot->lpVtbl->GetCurrentPosition(Robot, A, retVal);
        printf("Vt : 0x%08.8X, %d et valeur retournee : %lu\n", A.vt, A.decVal, retVal[0]);
        for(i = 0; i < 100; i++)
            printf("%d : %lu\n", i, retVal[i]);
        for(i = 0; i < 5; i++)
            printf("%d : %lu\n", i, A.pulVal[i]);
     
        printf("hrResult : %i\n", ret);
     
     
        PrintVariant(&A);
        CoupureRobot();
     
        return 0;
    }
    Voici l'IDL correspondant à l’interface :
    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
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    // Generated .IDL file (by the OLE/COM Object Viewer)
    //
    // typelib filename: Rob5x.exe
     
    [
      uuid(3CF4288A-9CB4-44DE-8CB2-8EDC34EB5A7C),
      version(1.0),
      helpstring("RobX 1.0 Type Library"),
      custom(DE77BA64-517C-11D1-A2DA-0000F8773CE9, 100663657),
      custom(DE77BA63-517C-11D1-A2DA-0000F8773CE9, 1360058814),
      custom(DE77BA65-517C-11D1-A2DA-0000F8773CE9, Created by MIDL version 6.00.0361 at Tue Feb 05 11:06:53 2013
    )
     
    ]
    library RobXLib
    {
        // TLib :     // TLib : OLE Automation : {00020430-0000-0000-C000-000000000046}
        importlib("stdole2.tlb");
     
        // Forward declare all types defined in this typelib
        interface IRobX;
     
        [
          uuid(5DD00CD6-E135-4799-A83F-62F06688700F),
          helpstring("CoRobX Class")
        ]
        coclass CoRobX {
            [default] interface IRobX;
        };
     
        [
          odl,
          uuid(92507B34-D92D-4F8C-967E-6C5166C0F1F6),
          helpstring("IRobX Interface"),
          dual,
          nonextensible,
          oleautomation
        ]
        interface IRobX : IDispatch {
            [id(0x00000001), helpstring("method CloseComm")]
            HRESULT CloseComm([out, retval] unsigned long* retVal);
            [id(0x00000002), helpstring("method Energize")]
            HRESULT Energize(
                            [in] unsigned long activate,
                            [out, retval] unsigned long* retVal);
            [id(0x00000003), helpstring("method GetErrorInfo")]
            HRESULT GetErrorInfo(
                            [in] unsigned long errorCode,
                            [out, retval] BSTR* retStr);
            [id(0x00000004), helpstring("method GetCurrentPosition")]
            HRESULT GetCurrentPosition(
                            [in] VARIANT coordArray,
                            [out, retval] unsigned long* retVal);
            [id(0x00000005), helpstring("method GetString")]
            HRESULT GetString(
                            [in] VARIANT readBytes,
                            [out, retval] unsigned long* retVal);
            [id(0x00000006), helpstring("method GotoHome")]
            HRESULT GotoHome(
                            [in] unsigned long timeout,
                            [out, retval] unsigned long* retVal);
            [id(0x00000007), helpstring("method GotoPoint")]
            HRESULT GotoPoint(
                            [in] BSTR name,
                            [in] unsigned long timeout,
                            [out, retval] unsigned long* retVal);
            [id(0x00000008), helpstring("method GotoPosition")]
            HRESULT GotoPosition(
                            [in] long X,
                            [in] long Y,
                            [in] long Z,
                            [in] long W,
                            [in] long P,
                            [in] unsigned long timeout,
                            [out, retval] unsigned long* retVal);
            [id(0x00000009), helpstring("method GotoUserPosition")]
            HRESULT GotoUserPosition(
                            [in] unsigned long timeout,
                            [out, retval] unsigned long* retVal);
            [id(0x0000000a), helpstring("method InitRobot")]
            HRESULT InitRobot([out, retval] unsigned long* retVal);
            [id(0x0000000b), helpstring("method MemPointPosition")]
            HRESULT MemPointPosition(
                            [in] long X,
                            [in] long Y,
                            [in] long Z,
                            [in] long W,
                            [in] long P,
                            [in] BSTR name,
                            [out, retval] unsigned long* retVal);
            [id(0x0000000c), helpstring("method Move")]
            HRESULT Move(
                            [in] unsigned long X,
                            [in] unsigned long Y,
                            [in] unsigned long Z,
                            [in] unsigned long timeout,
                            [out, retval] unsigned long* retVal);
            [id(0x0000000d), helpstring("method OpenComm")]
            HRESULT OpenComm(
                            [in] BSTR portName,
                            [out, retval] unsigned long* retVal);
            [id(0x0000000e), helpstring("method RestoreUserDefaultPosition")]
            HRESULT RestoreUserDefaultPosition([out, retval] unsigned long* retVal);
            [id(0x0000000f), helpstring("method SendString")]
            HRESULT SendString(
                            [in] BSTR bytesToSend,
                            [out, retval] unsigned long* retVal);
            [id(0x00000010), helpstring("method SetSpeed")]
            HRESULT SetSpeed(
                            [in] unsigned long speed,
                            [out, retval] unsigned long* retVal);
            [id(0x00000011), helpstring("method SetUserPosition")]
            HRESULT SetUserPosition(
                            [in] long X,
                            [in] long Y,
                            [in] long Z,
                            [in] long W,
                            [in] long P,
                            [out, retval] unsigned long* retVal);
            [id(0x00000012), helpstring("method Wait")]
            HRESULT Wait(
                            [in] unsigned long millisecs,
                            [out, retval] unsigned long* retVal);
            [id(0x00000013), helpstring("method SetLimits")]
            HRESULT SetLimits(
                            [in] long Xmin,
                            [in] long Xmax,
                            [in] long Ymin,
                            [in] long Ymax,
                            [in] long Zmin,
                            [out, retval] unsigned long* retVal);
            [id(0x00000014), helpstring("method RestoreDefaultLimits")]
            HRESULT RestoreDefaultLimits([out, retval] unsigned long* retVal);
            [id(0x00000015), helpstring("method GetLimits")]
            HRESULT GetLimits(
                            [in] VARIANT Limits,
                            [out, retval] unsigned long* retVal);
            [id(0x00000016), helpstring("method SetToolParam")]
            HRESULT SetToolParam(
                            [in] long ToolLength,
                            [out, retval] unsigned long* retVal);
            [id(0x00000017), helpstring("method GetToolParam")]
            HRESULT GetToolParam([out, retval] long* ToolLength);
            [id(0x00000018), helpstring("method Align")]
            HRESULT Align(
                            [in] unsigned long activate,
                            [out, retval] unsigned long* retVal);
            [id(0x00000019), helpstring("method GetVacuum")]
            HRESULT GetVacuum(
                            [in] VARIANT vacuum,
                            [out, retval] unsigned long* retVal);
            [id(0x0000001a), helpstring("method Pump")]
            HRESULT Pump(
                            [in] unsigned long activate,
                            [out, retval] unsigned long* retVal);
            [id(0x0000001b), helpstring("method Valve")]
            HRESULT Valve(
                            [in] unsigned long open,
                            [out, retval] unsigned long* retVal);
            [id(0x0000001c), helpstring("method OpenManualBox")]
            HRESULT OpenManualBox([out, retval] unsigned long* retVal);
            [id(0x0000001d), helpstring("method GetSpeed")]
            HRESULT GetSpeed(
                            [in] VARIANT speed,
                            [out, retval] unsigned long* retVal);
            [id(0x0000001e), helpstring("method WaitForManualBoxEnd")]
            HRESULT WaitForManualBoxEnd([out, retval] unsigned long* retVal);
            [id(0x0000001f), helpstring("method GetSerialNumber")]
            HRESULT GetSerialNumber([out, retval] BSTR* serialNumber);
            [id(0x00000020), helpstring("method GetUserPosition")]
            HRESULT GetUserPosition(
                            [in] VARIANT userPosition,
                            [out, retval] unsigned long* retVal);
            [id(0x00000021), helpstring("method Calibrate")]
            HRESULT Calibrate(
                            [in] long ask,
                            [out, retval] unsigned long* retVal);
            [id(0x00000022), helpstring("method GetSpeedStatus")]
            HRESULT GetSpeedStatus([out, retval] unsigned long* retVal);
            [id(0x00000023), helpstring("method GetSpeedNum")]
            HRESULT GetSpeedNum([out, retval] unsigned long* speed);
            [id(0x00000024), helpstring("method GetCurrentPositionStatus")]
            HRESULT GetCurrentPositionStatus([out, retval] unsigned long* retVal);
            [id(0x00000025), helpstring("method GetCurrentPositionStr")]
            HRESULT GetCurrentPositionStr([out, retval] BSTR* coordStr);
            [id(0x00000026), helpstring("method GetLimitsStatus")]
            HRESULT GetLimitsStatus([out, retval] unsigned long* retVal);
            [id(0x00000027), helpstring("method GetLimitsStr")]
            HRESULT GetLimitsStr([out, retval] BSTR* limitsStr);
            [id(0x00000028), helpstring("method GetVacuumStatus")]
            HRESULT GetVacuumStatus([out, retval] unsigned long* retVal);
            [id(0x00000029), helpstring("method GetVacuumNum")]
            HRESULT GetVacuumNum([out, retval] unsigned long* vacuum);
            [id(0x0000002a), helpstring("method GetUserPositionStatus")]
            HRESULT GetUserPositionStatus([out, retval] unsigned long* retVal);
            [id(0x0000002b), helpstring("method GetUserPositionStr")]
            HRESULT GetUserPositionStr([out, retval] BSTR* coordStr);
            [id(0x0000002c), helpstring("method OpenPositionManager")]
            HRESULT OpenPositionManager(
                            [in] BSTR positions,
                            [out, retval] unsigned long* retVal);
            [id(0x0000002d), helpstring("method WaitForPositionManagerEnd")]
            HRESULT WaitForPositionManagerEnd([out, retval] unsigned long* retVal);
            [id(0x0000002e), propget, helpstring("property PositionFile")]
            HRESULT PositionFile([out, retval] BSTR* filePath);
            [id(0x0000002e), propput, helpstring("property PositionFile")]
            HRESULT PositionFile([in] BSTR filePath);
            [id(0x0000002f), helpstring("method Laser")]
            HRESULT Laser(
                            [in] unsigned long activate,
                            [out, retval] unsigned long* retVal);
            [id(0x00000030), helpstring("method SetAccel")]
            HRESULT SetAccel(
                            [in] unsigned long accel,
                            [out, retval] unsigned long* retVal);
            [id(0x00000031), helpstring("method GetAccel")]
            HRESULT GetAccel(
                            [in] VARIANT accel,
                            [out, retval] unsigned long* retVal);
            [id(0x00000032), helpstring("method GetAccelStatus")]
            HRESULT GetAccelStatus([out, retval] unsigned long* retVal);
            [id(0x00000033), helpstring("method GetAccelNum")]
            HRESULT GetAccelNum([out, retval] unsigned long* speed);
        };
    };
    Pour information, la documentation du constructeur (faite pour du JavaScript), montre le code d'exemple associé à la fonction GetCurrentPosition() dont j'essaie de récupérer les données :

    GetCurrentPosition method
    This method retrieves the current coordinates of the robot.
    Parameters
    ● [out] Array: This array of integers holds the current coordinates of the robot. There are five
    elements in the array (X, Y, Z, W, P). X, Y, Z are in mm, W and P are in degrees.
    Return
    Returns 0 when successful, else an error is returned.
    JScript example

    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
     
    // Create a shell object to display user messages
    var WshShell = new ActiveXObject("WScript.Shell");
    //Create a robot control object
    var RobX = new ActiveXObject("KEOLABS.Rob5x");
    //Status return variable
    var ret;
     
    // Open communication with the robot
    ret = RobX.OpenComm("COM1");
    if (ret)
        WshShell.Popup(RobX.GetErrorInfo(ret));
    // Retrieve current Robot coordinates
    var coordinates = new Array(1,2,3,4,5);
    ret = RobX.GetCurrentPosition(coordinates);
    if (ret)
        WshShell.Popup(RobX.GetErrorInfo(ret));
    // Display coordinates
    WshShell.Popup("Current robot coordinates:"
                             + "\r\n X = " + coordinates[0]
                             + "\r\n Y = " + coordinates[1]
                             + "\r\n Z = " + coordinates[2]
                             + "\r\n W = " + coordinates[3]
                             + "\r\n P = " + coordinates[4]
    );
    // Close communication
    ret = RobX.CloseComm();
    if (ret)
           WshShell.Popup(RobX.GetErrorInfo(ret));
    J'ai testé pour d'autres fonctions fonctionnant sur le même procédé, sans succès...

    Je suis preneur des pistes que vous pouvez me suggérer et je vous remercie d'avance pour votre aide.
    Si jamais il y a besoin, je peux fournir plus d'informations s'il en manque.

  2. #2
    Membre Expert
    Avatar de Metalman
    Homme Profil pro
    Enseignant-Chercheur
    Inscrit en
    Juin 2005
    Messages
    1 049
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Enseignant-Chercheur
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2005
    Messages : 1 049
    Par défaut
    Je vais encore faire de la pub pour mon micro projet pas génial...
    Mais, dans le fichier My_Printer.c, la 1ère fonction sert à détecter le type et l'afficher.
    On m'avait tiré les oreilles car les variables ont le nom des types, ce qui est fortement déconseillé... donc à éviter de faire !
    Mais ça fonctionne malgré tout.

    Je ne sais pas si cela te sera utile ou non, mais ça imprimait de façon pas trop mal le contenu des VARIANT.
    Ca pourra peut être te donner une idée de comment le remplir avant de l'envoyer !

    Je copie son contenu ici :
    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
    void	PrintVTVal(VARIANT	vtProp,
    				   int		MyArgs)
    {
    	_TCHAR*		ansi = NULL;
    	_TCHAR		str[STR_BUF_SIZE];
    	DWORD		int32 = 0;
    	DWORD		int16 = 0;
    	DWORD		int8 = 0;
    	DWORD		uint32 = 0;
    	DWORD		uint16 = 0;
    	DWORD		uint8 = 0;
    	float		flott = 0.;
    	double		doubble = 0.;
    	BOOLEAN		boolean = FALSE;
     
    	if ((vtProp.vt == VT_BSTR) && (vtProp.bstrVal != NULL)) // string
    		{
    		//Copie de chaîne en fonction de la compilation
    		#ifndef UNICODE
    			WideCharToMultiByte(CP_ACP, 0, vtProp.bstrVal, -1, str,
    								STR_BUF_SIZE, NULL, NULL);
    		#else
    			StringCchCopyEx(str, STR_BUF_SIZE, vtProp.bstrVal, NULL, NULL, STRSAFE_FILL_BEHIND_NULL | STRSAFE_IGNORE_NULLS);
    		#endif
     
    			ansi = str;
    			if (MyArgs <= 1)
    				_tprintf(TEXT("%s%s\n"), TEXT("Value : "), vtProp.bstrVal);
    			else
    				_tprintf(TEXT("%s\n"), vtProp.bstrVal);
    		}
    		if (vtProp.vt == VT_I1) // int8
    		{
    			int8 = vtProp.intVal;
    			if (MyArgs <= 1)
    				_tprintf(TEXT("%s%d\n"), TEXT("Value (int8) : "), int8);
    			else
    				_tprintf(TEXT("%d\n"), int8);
    		}
    		if (vtProp.vt == VT_I2) // int16
    		{
    			int16 = vtProp.intVal;
    			if (MyArgs <= 1)
    				_tprintf(TEXT("%s%d\n"), TEXT("Value (int16) : "), int16);
    			else
    				_tprintf(TEXT("%d\n"), int16);
    		}
    		if ((vtProp.vt == VT_INT) || (vtProp.vt == VT_I4)) // int32
    		{
    			int32 = vtProp.intVal;
    			if (MyArgs <= 1)
    				_tprintf(TEXT("%s%d\n"), TEXT("Value (int32) : "), int32);
    			else
    				_tprintf(TEXT("%d\n"), int32);
    		}
    		if (vtProp.vt == VT_UI1) // uint8
    		{
    			uint8 = vtProp.bVal;
    			if (MyArgs <= 1)
    				_tprintf(TEXT("%s%u\n"), TEXT("Value (uint8) : "), uint8);
    			else
    				_tprintf(TEXT("%u\n"), uint8);
    		}
    		if (vtProp.vt == VT_UI2) // uint16
    		{
    			uint16 = vtProp.bVal;
    			if (MyArgs <= 1)
    				_tprintf(TEXT("%s%u\n"), TEXT("Value (uint16) : "), uint16);
    			else
    				_tprintf(TEXT("%u\n"), uint16);
     
    		}
    		if ((vtProp.vt == VT_UINT) || (vtProp.vt == VT_UI4)) // uint32
    		{
    			uint32 = vtProp.intVal;
    			if (MyArgs <= 1)
    				_tprintf(TEXT("%s%u\n"), TEXT("Value (uint32) : "), uint32);
    			else
    				_tprintf(TEXT("%u\n"), uint32);
    		}
    		if (vtProp.vt == VT_R4) // float
    		{
    			flott = vtProp.fltVal;
    			if (MyArgs <= 1)
    				_tprintf(TEXT("%s%e\n"), TEXT("Value (float) : "), flott);
    			else
    				_tprintf(TEXT("%e\n"), flott);
    		}
    		if (vtProp.vt == VT_R8) // double
    		{
    			doubble = vtProp.dblVal;
    			if (MyArgs <= 1)
    				_tprintf(TEXT("%s%e\n"), TEXT("Value (double) : "), doubble);
    			else
    				_tprintf(TEXT("%e\n"), doubble);
    		}
    		if (vtProp.vt == VT_NULL) // nothing
    		{
    			ansi = _T("NULL");
    			if (MyArgs <= 1)
    				_tprintf(TEXT("%s%s\n"), TEXT("Value (NULL) : "), ansi);
    			else
    				_tprintf(TEXT("%s\n"), TEXT("NULL"));
    		}
    		if (vtProp.vt == VT_BOOL) // double
    		{
    			if (vtProp.boolVal != 0)
    			{
    				ansi = _T("TRUE");
    				boolean = TRUE;
    			}
    			else
    			{
    				ansi = _T("FALSE");
    				boolean = FALSE;
    			}
    			if (MyArgs <= 1)
    				_tprintf(TEXT("%s%s\n"), TEXT("Value (bool) : "), ansi);
    			else
    				_tprintf(TEXT("%s\n"), ansi);
    		}
    }
    Bref : ce n'est pas un post "réponse", mais un post "piste"/tentative d'être utile !
    --
    Metalman !

    Attendez 5 mins après mes posts... les EDIT vont vite avec moi...
    Les flags de la vie : gcc -W -Wall -Werror -ansi -pedantic mes_sources.c
    gcc -Wall -Wextra -Werror -std=c99 -pedantic mes_sources.c
    (ANSI retire quelques fonctions comme strdup...)
    L'outil de la vie : valgrind --show-reachable=yes --leak-check=full ./mon_programme
    Et s'assurer que la logique est bonne "aussi" !

    Ma page Developpez.net

  3. #3
    Membre émérite

    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2013
    Messages
    32
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2013
    Messages : 32
    Par défaut
    Tu l'aimes ton projet, hein ?
    En fait j'ai déjà une fonction de ce type que j'ai testé et qui fonctionne bien globalement (et en lisant ton code, le tiens ne semble pas faire plus, peut être de manière plus propre sur certains aspects).

    Le problème c'est qu'extraire les données que la fonction doit me fournir semble impossible malgré mes tentatives. Peut être que j'exploite mal les VARIANT en tant que tel ou l'objet COM mais tout ceci me semble bien étrange comme comportement.

    Je te remercie en tout cas.

  4. #4
    Expert éminent
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 391
    Par défaut
    Ça me parait bizarre que le VARIANT soit déclaré en [in] uniquement, mais je ne connais pas trop les règles de COM quand au comportement in/out des SAFEARRAY.

    Car c'est bien ce qu'il faut utiliser ici, je pense: Un SAFEARRAY:
    Code C : 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
    ULONG ret = 0;
    HRESULT hr;
    SAFEARRAY * pArr = SafeArrayCreateVector(VT_I4, 0, 5);
    VARIANT v;
    v.parray = pArr;
    /*Alors, c'est soit l'un, soit l'autre, je ne sais pas lequel marchera*/
    v.vt = VT_I4|VT_ARRAY;
    /*ou bien*/
    v.vt = VT_SAFEARRAY;
     
    hr = pObj->lpVtbl->GetCurrentPosition(pObj, v, &ret);
    if(SUCCEEDED(hr))
    {
    	/*...*/
    }
    SafeArrayDestroy(pArr), pArr=NULL;
    C'est aussi bizarre que la fonction ne soit pas directement déclarée comme prenant un SAFEARRAY en paramètre dans l'IDL...
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  5. #5
    Membre émérite

    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2013
    Messages
    32
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2013
    Messages : 32
    Par défaut
    Je te remercie pour ta solution.
    Je pense en effet que c'est une bonne voie car cela permettrait d'envoyer un VARIANT en paramètre sans pointeur tout en pouvant récupérer les valeurs.


    D'après la documentation de Microsoft, c'est bien cette ligne là qui fonctionne :

    Après documentation et teste de ton code, j'ai tenté d'afficher les valeurs via ces lignes :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
        if(SUCCEEDED(hr))
        {
            for(i = 0 ; i < 5; i++)
                tableau[i] =  *((LONG *)(v.parray->pvData)+i);
            for(i = 0; i < 5; i++)
                printf("%d : %ld\n", i, tableau[i]);
     
        }
    Oui en soit la première boucle for n'est pas utile, c'était pour alléger un peu le printf.

    Malgré ça je reçois des valeurs nulles pour chacune (0 donc).
    Donc cela ne semble pas fonctionner au premier abord. Je vais quand même creuser la question plus en détail, je tenais à faire un premier retour au cas où.

    Je vous remercie pour cette piste intéressante ! N'hésitez pas si vous avez d'autres idées.

  6. #6
    Expert éminent
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 391
    Par défaut
    Juste pour l'info, évite d'accéder directement à la structure interne d'un SAFEARRAY si ce n'est pas absolument nécessaire: Utilise SafeArrayGetElement().

    Aussi, d'après la doc il faut également vérifier le ULONG retourné par la méthode, en plus du HRESULT.
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

Discussions similaires

  1. [DB2]Récupérer les données de type entier
    Par amy_g dans le forum DB2
    Réponses: 3
    Dernier message: 30/05/2007, 15h11
  2. XMLHttpRequest : Impossible de récupérer les données
    Par laurent_ifips dans le forum Général JavaScript
    Réponses: 10
    Dernier message: 22/11/2005, 13h28
  3. [](VB) Récupérer les données dans un fichier .xml
    Par Furius dans le forum VBScript
    Réponses: 4
    Dernier message: 02/10/2005, 20h39
  4. Récupérer les données interbase dans une TStringGrid
    Par Ousse dans le forum Bases de données
    Réponses: 1
    Dernier message: 24/03/2005, 12h51
  5. Récupérer les données d'une iframe
    Par juli1 dans le forum Balisage (X)HTML et validation W3C
    Réponses: 4
    Dernier message: 09/02/2005, 22h53

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