This i the background to my problem, which is stated in bold text. I am currently working with a SAFEARRAY. I've declared a two dimensional array in VB6 and I'm trying to store data from a C++ dll in it. I have used the resources listed below to learn how to work with SAFEARRAYs and convert from a c_str to a BSTR.
BSTR to c_str conversion:
http://www.codeguru.com/cpp/cpp/string/conversions/print.php/c5639 ( this link was very helpful and easy to understand )
http://www.codeguru.com/forum/archive/index.php/t-112768.html
SAFEARRAY implementations:
http://www.geocities.com/Jeff_Louie/safearray.html ( this link was very helpful and easy to understand )
http://msdn.microsoft.com/en-us/library/ms221283.aspx
http://www.codeguru.com/cpp/com-tech/activex/com/article.php/c2575
My problem is everytime I try use SafeArrayPutElement, hresult returns as E_INVALIDARG. This doesn't tell me which argument is invalid and as of now I'm kind of clueless as to how to remedy the problem. Would anyone have any pointers as to what I'm doing wrong?
Thanks in Advance.
int AddRecordToArray( struct t_recordinfo * PtrRecordInfo,
int RecordSize,
long NextRecordIndex,
SAFEARRAY ** PtrPtrLogFile )
{
/* declarations */
long ArrayUBound;
long ArrayLBound;
long PositionToSave[2];
unsigned int ArrayDimension;
HRESULT hresult;
char CStrData[512];
unsigned int ElementSize;
BSTR DataToInsert;
_variant_t RandomString;
/* initilizations */
USES_CONVERSION;
memset( CStrData, 0x00, sizeof(CStrData) );
/* insert postion */
PositionToSave[1] = NextRecordIndex;
/* data to insert */
memset( CStrData, 0x00, sizeof(CStrData) );
strncpy( CStrData,
PtrRecordInfo->PtrDate,
17 );
/* data as BSTR */
RandomString = A2W( CStrData );
/* save processing */
PositionToSave[0] = 0;
hresult = SafeArrayPutElement( *PtrPtrLogFile, PositionToSave, &RandomString );
/* all of these set hresult as 10 just so I can see which case occured */
switch ( hresult ) {
case S_OK:
hresult = 10;
break;
case DISP_E_BADINDEX:
hresult = 10;
break;
case E_INVALIDARG:
hresult = 10;
break;
case E_OUTOFMEMORY:
hresult = 10;
break;
}
return( hresult );
}