0

我收到 E_INVALIDARG 错误

STDMETHODIMP CDocumentController::HasPrivEventCode(double dUserId,double dPosCd,double dPPRCd,SAFEARRAY** pEventCode)
{ 
    // TODO: Add your implementation code here
    long lElements; // number of elements in the array
    long lCount;
    HRESULT lResult; // return code for OLE functions
    double *pArrayElements; // pointer to the elements of the array

    // checking if it is a one-dimensional array
    if ( (*pEventCode)->cDims != 1 ) return(E_FAIL);

    // checking if it is an array of double
    if ( (*pEventCode)->cbElements != 8 ) return(E_FAIL);

    // how many elements are there in the array
    lElements=(*pEventCode)->rgsabound[0].cElements;

    // locking the array before using its elements
    lResult=SafeArrayLock(*pEventCode);
    if (lResult != S_OK) return(E_FAIL);

    // using the array
    pArrayElements=(double*) (*pEventCode)->pvData;

    CMap<double,double,bool,bool> mapEventCds;

    for (lCount=0; lCount<lElements; lCount++) 
    { 
        double lVal = pArrayElements[lCount]; 
        mapEventCds.SetAt(lVal, false); 
    } 
    CheckPrivViewResultEventCds(dUserId, dPosCd, dPPRCd, mapEventCds); 

    long iEventCdIdx(0);
    double          dEventCd(0.0); 
    bool            bPriv(false); 
    POSITION        pos(mapEventCds.GetStartPosition()); 
    INT_PTR             nEventCnt(mapEventCds.GetCount()); 
    double* arrEventCdsWithPriv = new double[nEventCnt];
    VARIANT vt;

    //(*pEventCode)->pvData = NULL;

    lResult=SafeArrayUnlock(*pEventCode); 
    HRESULT hr = SafeArrayDestroyData(*pEventCode);

    for(INT_PTR count(0); count < nEventCnt; ++count) 
    { 
        mapEventCds.GetNextAssoc(pos, dEventCd, bPriv); 

        if (bPriv) 
        {   
            vt.vt = VT_R8;
            vt.dblVal = dEventCd;
            hr = SafeArrayPutElement(*pEventCode, &iEventCdIdx, &dEventCd);
            iEventCdIdx++;
        }   
    }
    // Empty the CMap 
    mapEventCds.RemoveAll(); 
    // releasing the array 
    lResult=SafeArrayUnlock(*pEventCode); 

    return S_OK;

}

*pEventCode是我的安全数组...是“双”值...当( )为真时dEventCd,我想将dEventCd*pEventCode一一插入到我的安全数组中...请帮助bPriv

4

1 回答 1

0

我对您使用的 API 没有经验。但是,我能够将这个创建 a 的小测试应用程序放在一起SAFEARRAY,将 2 的值放入第一个索引,然后成功检索该值。您将在下面的代码中看到有一个注释掉的部分,其中包括对SafeArrayDestroyData. 当该代码被注释掉时,应用程序就可以工作;但是,当您取消注释代码时,突然调用SafeArrayPutElement失败并显示E_INVALIDARG. 我相信这是你的问题。在将元素放入SAFEARRAY

#include "stdafx.h"

#include <OaIdl.h>
#include <iostream>
#include <comdef.h>
#include <stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
    SAFEARRAYBOUND rgsabound[1];
    rgsabound[0].lLbound = 0;
    rgsabound[0].cElements = 1;

    SAFEARRAY FAR* psa = SafeArrayCreate(VT_VARIANT, 1, rgsabound);

    if (psa == NULL) {
        std::cout << "Could not create safe array";

        return 0;
    }

    auto hr = SafeArrayLock(psa);
    _com_error lockErr(hr);
    printf(lockErr.ErrorMessage());
    std::cout << std::endl;

    hr = SafeArrayUnlock(psa);
    _com_error unlockErr(hr);
    printf(unlockErr.ErrorMessage());
    std::cout << std::endl;

    //This block is causing the trouble!
    /*
    hr = SafeArrayDestroyData(psa);
    _com_error destroyErr(hr);
    printf(destroyErr.ErrorMessage());
    std::cout << std::endl;
    */

    LONG  rgIndex = 0;
    double d = 2.0;

    hr = SafeArrayPutElement(psa, &rgIndex, &d);

    _com_error putErr(hr);
    printf(putErr.ErrorMessage());
    std::cout << std::endl;

    if (hr == S_OK) {
        double out = 0;
        hr = SafeArrayGetElement(psa, &rgIndex, &out);

        _com_error getErr(hr);
        printf(putErr.ErrorMessage());
        std::cout << std::endl;

        std::cout << "the value of the element retrieved was: " << out;
    }

    std::cin.get();

    return 0;
}
于 2013-11-12T05:00:34.873 回答