Solved

Passing VB arrays from C...

Posted on 1999-01-12
5
168 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
  • 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now