Solved

Handling VB arrays in C

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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures 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.

863 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

21 Experts available now in Live!

Get 1:1 Help Now