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

x
?
Solved

Handling VB arrays in C

Posted on 1999-01-12
4
Medium Priority
?
261 Views
Last Modified: 2010-04-15
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
4 Comments
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1255815
Take a look at www.worldofatl.com
They have some nice samples of using C and VB
0
 

Expert Comment

by:murrayc
ID: 1255816
You are on the right lines with the SafeArray API calls.
0
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 400 total points
ID: 1255817
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
 

Author Comment

by:gburson
ID: 1255818
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.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

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