Solved

Passing VB arrays from C...

Posted on 1999-01-12
5
187 Views
Last Modified: 2010-05-03
Hello. I am trying to use an OCX written specifically for use with VB. Some of the parameters require a VB array to be passed. Of course, VB array are completely different than in C. Is there any api for handling VB type arrays ? I have tried using the Safe Array functions in the Microsoft Developer Studio 5.0 (e.g. safeArrayCreate) with no success. I'm not even sure this is the same thing ! Help...!
0
Comment
Question by:gburson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 4

Expert Comment

by:chabaud
ID: 1455631
Your are on the right way, your must use SAFEARRAY.
Here is an C++ sample using atl which create an array of variants:

STDMETHODIMP CClass::Test( VARIANT* retpsa)
{
hResult = S_OK;
SAFEARRAY FAR* psa;
SAFEARRAYBOUND rgsabound[2];
VARIANT HUGEP *pvar;
int i, idx;

rgsabound[0].lLbound = 0;
rgsabound[0].cElements = nRow-1;

rgsabound[1].lLbound = 0;
rgsabound[1].cElements = nCol-1;

psa = SafeArrayCreate(VT_VARIANT, 2, rgsabound);
if (psa == NULL)
      {
      hResult = HERROR | ERR_ALLOCATING_VARIANT;
      goto End;
      }

hResult = SafeArrayAccessData(psa, (void HUGEP* FAR*)&pvar);
if (FAILED(hResult))
      {
      hResult = HERROR | ERR_ACCESS_VARIANT;
      goto End;
      }

for (int i = 0; i < nRow; i++)
      {
        idx = i;

      pvar[idx].vt = VT_BSTR;
      pvar[idx].bstrVal = _bstr_t("some data").copy();

      idx += nRow;
      pvar[idx].vt = VT_I4;
      pvar[idx].lVal = (long)123;
      }

SafeArrayUnaccessData(psa);

VariantInit(retpsa);
retpsa->vt = VT_ARRAY | VT_VARIANT;
retpsa->parray = psa;

End:
SafeArrayDestroy(psa);

return 0;
}

0
 

Author Comment

by:gburson
ID: 1455632
Thanks Chabaud. Just to clarify - I believe the OCX expects dynamic arrays. Does this change the answer ?

Thank again.
0
 
LVL 4

Expert Comment

by:chabaud
ID: 1455633
My sample just return a two dimension safe array.

But you can get array in also. The import function is SafeArrayAccessData which allow you to access array element in C.
0
 

Author Comment

by:gburson
ID: 1455634
One last question - I will be coding in C, so to allocation the bstr, should I use sysAllocString ? Any other problems ?

Also I how do I close/grade this question !

Thankyou,
Glen.
0
 
LVL 4

Accepted Solution

by:
chabaud earned 200 total points
ID: 1455635
Yes you can use SysAllocString and SysFreeString to handle BSTR.

In C++ the built-in _bstr_t class is a very helpfull wrapper for this BSTR function.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Suggested Courses
Course of the Month11 days, left to enroll

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question