Solved

Passing VB arrays from C...

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

790 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