• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

Handling VB arrays in C

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
gburson
Asked:
gburson
1 Solution
 
MirkwoodCommented:
Take a look at www.worldofatl.com
They have some nice samples of using C and VB
0
 
murraycCommented:
You are on the right lines with the SafeArray API calls.
0
 
Tommy HuiEngineerCommented:
The answer to your problem is ultimately SAFEARRAY. What you would want is to make the parameters in your OCX to take a VARIANT.

In the code for the function, you need to check the type of the the parameter. Make sure it is VT_ARRAY. From that, you can extract the SAFEARRAY* from the variant. Then you can use SafeArrayGetDim() to find out the dimensions of the array. You also need to use SafeArrayGetElemsize() to determine how big each item is. Use SafeArrayGetLBound() and SafeArrayGetUBound() to determine how many items are in a certain dimension. Finally, you can use SafeArrayGetElement() to retrieve an individual item from the array.

0
 
gbursonAuthor Commented:
Thanks Thui, but I am still having no luck ! The problem is that the OCX is supplied by a 3rd party with very little documentation. All I know is that it is normally called from VB passing a dynamic array. Here are the 2 functions I have coded to create and add strings to the array:

SAFEARRAY FAR* NST_API NSSAFEARRAYCREATE( NST_LONG pNum )
{
      SAFEARRAY FAR* psa;
      SAFEARRAYBOUND rgsabound[1];
      
      rgsabound[0].lLbound = 0;      
      rgsabound[0].cElements = pNum;
      psa = SafeArrayCreate(VT_VARIANT, 1, rgsabound);
   
    return psa;
}

NST_LONG NST_API NSSAFEARRAYPUTELEMENT( SAFEARRAY FAR* psa, NST_LONG iIdx, OLECHAR FAR* szData)
{
      HRESULT hRes;
      VARIANT HUGEP *pvar;
      
      hRes = SafeArrayAccessData(psa, (void HUGEP* FAR*)&pvar);
      if (hRes != S_OK)
      {
            return -1;
      }

      pvar[iIdx].vt = VT_BSTR;
      pvar[iIdx].bstrVal = SysAllocString(szData);
      SafeArrayUnaccessData(psa);
      if (hRes != S_OK)
      {
            return -2;
      }

      return 0;
}

Thanks,
Glen.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now