无法从char转换为void *

时间:2012-03-13 15:54:08

标签: c++ windows

SAFEARRAYBOUND bound[1] = {25, 0};
SAFEARRAY * psa = SafeArrayCreate(VT_UI1, 1, bound);
for(long int i = 0; i <25; i++)
    SafeArrayPutElement(psa, &i,sendBuf[i]);

我正在尝试从char数组创建safearray但是会​​收到此错误cannot convert from char to void* sendBuf是一个char数组

2 个答案:

答案 0 :(得分:3)

您没有向我们显示错误,但它显示SafeArrayPutElement指针作为第三个参数传递给元素。我相信如果你使用它会起作用:

SafeArrayPutElement(psa, &i, &sendBuf[i]);

请注意&

答案 1 :(得分:1)

在这种情况下使用SafeArrayAccessData()代替SafeArrayPutElement()会更有效:

SAFEARRAYBOUND bound[1] = {25, 0}; 
SAFEARRAY * psa = SafeArrayCreate(VT_UI1, 1, bound); 
void *pvData;
SafeArrayAccessData(psa, &pvData);
memcpy(pvData, sendBuf, 25);
SafeArrayUnaccessData(psa);

或者:

SAFEARRAYBOUND bound[1] = {25, 0}; 
SAFEARRAY * psa = SafeArrayCreate(VT_UI1, 1, bound); 
unsigned char *pvData;
SafeArrayAccessData(psa, (void**)&pvData);
for(long int i = 0; i <25; i++)     
    pvData[i] = sendBuf[i];
SafeArrayUnaccessData(psa);