How to pass structures between a OCX and a VB app

Posted on 1998-07-17
Last Modified: 2013-11-20
How could I manage passing a complex C data structure between a VB app and a OCX ?

For example, I'd like to use the following s2_ST structure:

typedef struct
    short      s1;
    long      l1;
    LPSTR      lps1;

typedef struct
    LPSTR      lps2;
    short      s2;
    s1_ST      **lpst;

where lpst is a array of pointers to s1_ST structures

A VB app must fill a parameter (or parameters) providing those fields to a OCX.  The OCX must return that parameter to the VB app with the fields modified.

I'm using Visual C 5.0 and used MFC Appwizard to build the OCX skeleton.
Question by:edsont
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
  • 5
  • 2

Author Comment

ID: 1319395
Adjusted points to 75

Expert Comment

ID: 1319396
Custom structures are not OLE automation compatible types. I would suggest you crate a method that takes void* argument and ActiveX control will convert it to structure pointer

Author Comment

ID: 1319397

Could you please give an example of your solution using the data structures I've written in the question ?

Technology Partners: 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!


Expert Comment

ID: 1319398

To pass data structures in automation you can use SAFEARRAY structure. It can be understood by VB.

The structure looks like this, refer VC help for more information.

typedef struct FARSTRUCT tagSAFEARRAY {
      unsigned short cDims;             // Count of dimensions in this array.
      unsigned short fFeatures;      // Flags used by the SafeArray
                                                // routines documented below.
#if defined(WIN32)
      unsigned long cbElements;      // Size of an element of the array.
                                                // Does not include size of
                                                // pointed-to data.
      unsigned long cLocks;            // Number of times the array has been
                                                // locked without corresponding unlock.
      unsigned short cbElements;
      unsigned short cLocks;
      unsigned long handle;            // Unused but kept for compatibility.
      void HUGEP* pvData;                         // Pointer to the data.
      SAFEARRAYBOUND rgsabound[1];            // One bound for each dimension.

If you want to pass a structure or string or an array from OCX to VB, you must first allocate memory in the OCX using SAFEARRAY, and then pass to VB, and VB can fill the structure.

The following example explains how to do that.

The structure should be defined in OCX and VB as follows.

The OCX structure
    typedef struct
        short s1;
        long l1;
        LPSAFEARRAY lps1; //replaced by LPSTR
    } s1_ST;

and the VB structure
      s1 as Integer
      l1 as Integer
      lps1 as String
END TYPE      

Before passing the above structue allocate memory for "lps1" using the following functions:

SafeArrayCreate, etc....

Refer VC help for the functions, starting with SafeArray...

And then you can pass the structure to VB.


Author Comment

ID: 1319399

I think your proposed answer is ok for the s1_ST structure, but my greatest problem is related to the s2_ST structure.

Could you give an example of your solution using the s2_ST structure ?


Author Comment

ID: 1319400
Adjusted points to 100

Accepted Solution

sudhirbrat earned 100 total points
ID: 1319401

The structure s2_ST should look like this in VC++

typedef struct
        LPSTR lps2; //Replace this string with SAFEARRAY as I said earlier
        short s2;
        LPSAFEARRAY lpst;

And the VB structure

      lps2 as string
      s2 as Integer
      lpst() as s1_ST

To create the Array in VC++, use the following code:

s2_ST st;
SAFEARRAYBOUND rgsabound[1];      
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = 2;

st.lpst = SafeArrayCreate(VT_VARIANT, 1, rgsabound);

This will create One dimension array with 2 elements.
And you can pass the structure to VB and Get data from VB.

Author Comment

ID: 1319402

Using MFC ClassWizard, what kind of data type could I use for that parameter ?  VARIANT ?

At VB side, how could I manage passing the User Defined type parameter to the OCX ?


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

Suggested Solutions

Title # Comments Views Activity
Perl Awk Need Help 3 138
Annoying "thing" blocks my view 4 131
Detect file exist or not 3 200
How do I get Window Title of all opened process? 4 35
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 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