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 );

}

May be (I'm not sure) the problem is:

The variant types VT_DISPATCH, VT_UNKNOWN, and VT_BSTR are pointers, and do not require another level of indirection.

If the 3rd argument of SafeArrayPutElement call has BSTR type, no need in an address operator in the call (?)...

Are you confident that PositionToSave[1] is being set to a value which is within the bounds of the SAFEARRAY 2nd dimension? Have you checked with the debugger/cout statements.
You say you're using a 2 dimensional array created in VB6. Was it created using SafeArrayCreate()?

May be (I'm not sure) the problem is:

If the 3rd argument of SafeArrayPutElement call has BSTR type, no need in an address operator in the call (?)...

Yeah, that was the first thing I thought too..

Are you confident that PositionToSave[1] is being set to a value which is within the bounds of the SAFEARRAY 2nd dimension? Have you checked with the debugger/cout statements.
You say you're using a 2 dimensional array created in VB6. Was it created using SafeArrayCreate()?

At the time I was working with it I could see what was inside it, but I couldn't put objects into it.

I actually figured out what the problem was, and I'm going to post the solution up tommorrow... after I get more sleep... but for I brief summary...

When you make an array in VB using var_name(), var_name(0) ect, it automatically makes it a SAFEARRAY for you, so you can assume that all VB arrays are SAFEARRAYs. Also, when you want to insert a string into a VB SAFEARRAY you have to declare the array in VB as a variant. Then there were a few problems that I had to iron out with string conversion. I'll post the what I did to fix it tommorrow. Thanks for your help though! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.