Solved

Handling VB arrays in C

Posted on 1999-01-12
4
254 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 200 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now