碰到个问题,需要用VARIANT变量传递字符串数组,想到使用SAFEARRAY,就写了如下代码,结果运行出现堆内存错误,请问该如何改正,多谢赐教
int _tmain(int argc, _TCHAR* argv[])
{
SAFEARRAY psa;
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].cElements=2;
rgsabound[0].lLbound=0;
psa = SafeArrayCreate(VT_BSTR,1,rgsabound);
_bstr_t *buf;
SafeArrayAccessData(psa, (void *)&buf);
buf[0]="你好";
buf[1]="大家好";
SafeArrayUnaccessData(psa);
_variant_t var;
var.vt = VT_ARRAY|VT_BSTR;
var.parray=psa;
_bstr_t * buf1;
SafeArrayAccessData(var.parray, (void **)&buf1);
printf("%s\n",(LPSTR)(buf1[0]));
printf("%s\n",(LPSTR)(buf1[1]));
SafeArrayUnaccessData(psa);
getchar();
return 0;
}
补充:改成如下代码,则显示不出结果
int _tmain(int argc, _TCHAR* argv[])
{
SAFEARRAY psa;
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].cElements=2;
rgsabound[0].lLbound=0;
psa = SafeArrayCreate(VT_BSTR,1,rgsabound);
_bstr_t buf[2];
_bstr_t pbuf = &buf[0];
SafeArrayAccessData(psa, (void **)&pbuf);
buf[0]="你好";
buf[1]="大家好";
SafeArrayUnaccessData(psa);
_variant_t var;
var.vt = VT_ARRAY|VT_BSTR;
var.parray=psa;
_bstr_t buf1[2];
_bstr_t* pbuf1 = &buf1[0];
SafeArrayAccessData(var.parray, (void **)&pbuf1);
printf("%s\n",(LPSTR)(buf1[0]));
printf("%s\n",(LPSTR)(buf1[1]));
SafeArrayUnaccessData(psa);
getchar();
return 0;
}
自己摆弄了半天,总算改对了,看样子不能用_variant_t,改成BSTR就行。