新学COM编程,对SafeArray的使用有些迷惑,在创建、访问SafeArray数组的时候,基本的过程和方法有哪些,函数中使用Variant变量传递SafeArray时应注意哪些问题?
还有是不是使用SafeArrayAccessData()后,可以利用获得的指针,直接作为数组使用?
我是新手,可能表达的不好,希望大家多给点学习COM编程的建议。
谢谢。
还有是不是使用SafeArrayAccessData()后,可以利用获得的指针,直接作为数组使用?
我是新手,可能表达的不好,希望大家多给点学习COM编程的建议。
谢谢。
这是我学习时写的一个接口函数,然后调用运行的时候总出现不能访问内存的错误,能帮我分析下么,谢谢。
STDMETHODIMP CMath::AddSafeArray(VARIANT vx, VARIANT vy, VARIANT *vz)
{
// TODO: Add your implementation code here
if(!(vx.vt & VT_ARRAY))
return DISP_E_TYPEMISMATCH;
if(!(vy.vt & VT_ARRAY))
return DISP_E_TYPEMISMATCH;
SAFEARRAY *pvx, *pvy, *pvz;
pvx = vx.parray;
pvy = vy.parray;
pvz = NULL;
long lLBound_vx, lUBound_vx; // The vx lower bound and upper bound
long lLBound_vy, lUBound_vy; // The vy lower bound and upper bound
// Get these SafeArrays' dimensions
if((SafeArrayGetDim(pvx) != 1) || (SafeArrayGetDim(pvy) != 1))
return FALSE;
// Get these SafeArrays' bound
SafeArrayGetLBound(pvx, 1, &lLBound_vx);
SafeArrayGetUBound(pvx, 1, &lUBound_vx);
SafeArrayGetLBound(pvy, 1, &lLBound_vy);
SafeArrayGetUBound(pvy, 1, &lUBound_vy);
// Calculate these SafeArrays' size
int vx_size, vy_size;
vx_size = lUBound_vx - lLBound_vx + 1;
vy_size = lUBound_vy - lLBound_vy + 1;
// Get these SafeArrays' value
int* vx_array = new int[vx_size];
int* vy_array = new int[vy_size];
int* vz_array = new int[vx_size];
int *pvxVal = NULL;
int *pvyVal = NULL;
HRESULT hr = SafeArrayAccessData(pvx, (void**)&pvxVal);
hr = SafeArrayAccessData(pvy, (void**)&pvyVal);
for(int i = vx_size - 1; i >= 0; i --)
{
vx_array[i] = pvxVal[i + lLBound_vx];
}
for(i = vy_size - 1; i >= 0; i --)
{
vy_array[i] = pvxVal[i + lLBound_vy];
}
for(i = vx_size - 1; i >= 0; i --)
{
vz_array[i] = vx_array[i] + vy_array[i];
}
pvz->cDims = 1;
pvz->cbElements = sizeof(vz_array[0]);
pvz->rgsabound[0].cElements = vx_size;
pvz->rgsabound[0].lLbound = 0;
vz->vt = VT_ARRAY;
vz->parray = pvz;
return S_OK;
}