Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Passing VB arrays from C...

Posted on 1999-01-12
5
Medium Priority
?
194 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 800 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

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…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
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…
Suggested Courses

670 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