Click to See Complete Forum and Search --> : Why does this SAFEARRAY not work?


June 1st, 1999, 10:18 AM
I am trying to send an array of strings from a VB client to a C++ server and am having difficulty. When I call SafeArrayAccessData(), the function returns E_UNEXPECTED, meaning that it could not access the data in the SAFEARRAY. Please someone point out what is the problem with my code!


///This is the C++ Server
///////////////////////
void ServerClass::PrintBStringArray(const VARIANT FAR& inval)
{
if((inval.vt & VT_ARRAY) == 0)
return; //Not an array
if((inval.vt & VT_BSTR) == 0)
return; //Not filled with BSTRs

SAFEARRAY FAR *psa=inval.parray;
BSTR HUGEP *bstrArray;
HRESULT hr;
CString strg;
hr=SafeArrayAccessData(psa, (void HUGEP* FAR*)&bstrArray);
if (FAILED(hr))
{
AfxMessageBox("Problem with array");
if(hr==E_INVALIDARG) AfxMessageBox("Invalid SafeArray!");
if(hr==E_UNEXPECTED) AfxMessageBox("Unexpected Error");
}
strg=CString(bstrArray[0]);
AfxMessageBox(strg);
SafeArrayUnaccessData(psa);
return;
}

/////////////////////////////
//This is the VB Client
////////////////////////////
Private Sub Command3_Click()
Dim StrArr(4) As String
StrArr(0) = "This is entry 1"
StrArr(1) = "This is the second entry"
StrArr(2) = "this is number three"
StrArr(3) = "The last entry..."
Set Myobj = CreateObject("AutoDLLTest.ServerClass")
Myobj.PrintBStringArray StrArr()
End Sub




Result: UNEXPECTED ERROR!

Is the parameter passed in (const VARIANT FAR& inval) OK? This is what ClassWizard created for me, and it appears to work OK....

Thanks, Jeff

muscicapa
June 2nd, 1999, 12:18 AM
try making the C++ server interface method take the VARIANT* as [in,out]