使用VBS调用COM DLL将VARIANT转换为字节数组时出错

时间:2018-08-15 11:13:53

标签: c++ variant

我正在编写一个COM dll。并使用vbs进行呼叫。但是当我检查dll中的变量类型时,它与vbs(VT_array)上的类型不匹配,即VT_byref。因此,我使用了variantinit()和VariantCopyInd()来打开VT_array类型。但是,当我从VARIANT转换为字节数组时,它总是返回与输入数组大小相同的数组,但是变量的值始终为0x02 这是我的vbs:

Set a = CreateObject("ExtCryptoLib.ExtCryptoTools")
ar1 =  Array(1,2,3,4,5)
Wscript.Echo "@" & a.camtest01(ar1)

这是我的comdll方法

STDMETHODIMP CExtCryptoTools::camtest01(VARIANT vData, BSTR* hexData)
{
    OutputDebugString(L"2222222222");
    wchar_t log[128];
    SAFEARRAY *p = NULL;
    VARIANT var01;
    VariantInit(&var01);
    VariantCopyInd(&var01, &vData);
    VariantCopyInd(&vData, &var01);
    if (vData.vt == (VT_ARRAY | VT_VARIANT)){
        unsigned char* arr;//VariantToByteArray(vData,&len);
        unsigned long int len;
        VariantToByteArray(vData, &arr, &len);
    }
}

这是VariantToByteArray函数:

void VariantToByteArray(VARIANT v,BYTE**  ppByteArrayReceiver, ULONG*  pulArraySizeReceiver ){


    wchar_t log[32];
    SAFEARRAY* pSafeArray = NULL;
    pSafeArray=V_ARRAY(&v);
    //pSafeArray = *V_ARRAYREF(&v);
    ULONG    ul = 0;
    SAFEARRAYBOUND* psafearraybound = NULL;

    psafearraybound = &((pSafeArray -> rgsabound)[0]);

    if (ppByteArrayReceiver)
    {
        *ppByteArrayReceiver = NULL;
        *ppByteArrayReceiver = (BYTE*)malloc((psafearraybound -> cElements) * sizeof(BYTE));
        memset (*ppByteArrayReceiver, 0, (psafearraybound -> cElements) * sizeof(BYTE));
    }

    if (pulArraySizeReceiver)
    {
        *pulArraySizeReceiver = psafearraybound -> cElements;
    }

    for (ul = 0; ul < psafearraybound -> cElements; ul++)
    {
        BYTE byte = 0;

        SafeArrayGetElement
            (
            (SAFEARRAY*)pSafeArray, 
            (long*)&ul,
            (void*)&(byte)
            );
        wsprintf(log, L"arr[%d] = %d", ul, byte);
        OutputDebugString(log);
        if (ppByteArrayReceiver)
        {
            (*ppByteArrayReceiver)[ul] = byte; // always : byte = 0x02 ??
        }
    }

}   

谢谢。

0 个答案:

没有答案