Solved

Handling VB arrays in C

Posted on 1999-01-12
4
259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops 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.

622 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