描述:
各位大哥大姐:
我用safearray存了数据,代码如下:
VARIANT testdialog::getbarcodes()
{
HANDLE hCspText; // handle to barcode text file
char szCspText[80]; // Barcode text
DWORD nCspTextLength; // for loop index
long nRetStatus;
VARIANT varValue;
nRetStatus = cspReadData();
// Create SafeArray of VARIANT BSTRs
SAFEARRAY *pSA;
SAFEARRAYBOUND aDim[1]; // a one dimensional array
aDim[0].lLbound= 0; // Visual Basic arrays start with index 0
aDim[0].cElements= nRetStatus;
pSA= SafeArrayCreate(VT_VARIANT,1,aDim);
if (pSA != NULL) {
long aLong[1];
for (long l= aDim[0].lLbound; l< (aDim[0].cElements + aDim[0].lLbound); l++) {
CspTextLength = cspGetBarcode(szCspText, l, sizeof(szCspText));
// subtract out the null terminator
VARIANT vOut;
VariantInit(&vOut);
vOut.vt= VT_BSTR;
BSTR b=_com_util::ConvertStringToBSTR(szCspText);
vOut.bstrVal=::SysAllocString(b);
aLong[0]= l;
hr= SafeArrayPutElement(pSA, aLong, &vOut);
if (FAILED(hr)) { // "correctly" copies VARIANT
VariantClear(&vOut); // release BSTR from memory on error
SafeArrayDestroy(pSA); // does a deep destroy on error
// return hr;
}
VariantClear(&vOut); // does a deep destroy of source VARIANT
} // end iteration
varValue.parray=pSA;
varValue.vt = VT_ARRAY|VT_BSTR;
}
return varValue;
}
然后在另一个函数中取数据,如下:
void testdialog::Onread()
{
VARIANT v =getbarcodes();
if (v.parray&&VT_ARRAY)
{
AfxMessageBox("sss111111111111111111");
if (v.bstrVal&&VT_BSTR)
{
LONG LBound;
LONG UBound;
::SafeArrayGetLBound(v.parray, 1, &LBound);
::SafeArrayGetUBound(v.parray, 1, &UBound);
LONG Count = UBound - LBound +1;
char temp[200];
ltoa(Count,temp,10);
AfxMessageBox(temp);
BSTR pvData ;
HRESULT hr;
hr=::SafeArrayAccessData(v.parray, (void **)&pvData);
if(FAILED(hr))
AfxMessageBox("errorsss");
char * buf = _com_util::ConvertBSTRToString(pvData);
CString ss=*pvData;
AfxMessageBox(ss);
AfxMessageBox("---------------");
::SafeArrayUnaccessData(v.parray);
}
}
}
为什么取不到正确的数据,老是为空,AfxMessageBox(ss)输出为空,但是在存数据时,可以看到数据确实读入了,vOut.bstrVal=::SysAllocString(b); 这里确实有数据,请各位高人帮忙,谢谢!!!在线等