Home > front end >  C COM (non MFC) calling method with pointer argument
C COM (non MFC) calling method with pointer argument

Time:10-28

I'm trying to reproduce a COM client in c in the non MFC way. I'm able to connect to the com interface and call some methods that require simple values as parameter, but I'm not able to call a method with a pointer as argument, the function is this:

short sProdGetCurrentMachineType(short* p_psMachineType)

and in the short variable pointed by p_psMachineType will be stored the result value.

I tried this:

DISPID dispid; //omitted for brevity, i get it from QueryInterface() on the com

VARIANT pVarResult;
EXCEPINFO pExcepInfo;
unsigned int* puArgErr = 0;
DISPPARAMS dispparams{}; 
VARIANTARG rgvarg[1];
short *p_psMachineType;
 

rgvarg[0].vt = VT_I2;
rgvarg[0].piVal = p_psMachineType;
dispparams.rgvarg = rgvarg;
dispparams.cArgs = 1;
dispparams.cNamedArgs = 0;

hresult = (*pDisp)->Invoke(
            dispid,
            IID_NULL,
            LOCALE_USER_DEFAULT,
            DISPATCH_METHOD,
            &dispparams, &pVarResult, &pExcepInfo, puArgErr
        );

but I get a TYPE_MISMATCH ERROR..

Instead I saw that using it as named argument I don't get error in the call but the pointer value is not populated, but i cannot find any example of pointers passed as named arguments.

Does anybody know how to handle it?

CodePudding user response:

As you say yourself, "in the short variable pointed by p_psMachineType will be stored the result value", so you need to pass it a valid pointer to an existing short.

short psMachineType;
// ...
rgvarg[0].piVal = &psMachineType;

CodePudding user response:

As Simon Mourier said the correct parameter to set on the rgvarg array was VT_I2 | VT_BYREF instead of VT_I2

  • Related