Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Passing VB arrays from C...

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

916 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