Solved

How to pass structures between a OCX and a VB app

Posted on 1998-07-17
8
293 Views
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;
}
  s1_ST;

typedef struct
{
    LPSTR      lps2;
    short      s2;
    s1_ST      **lpst;
}
  s2_ST;

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.
0
Comment
Question by:edsont
  • 5
  • 2
8 Comments
 

Author Comment

by:edsont
Comment Utility
Adjusted points to 75
0
 
LVL 7

Expert Comment

by:galkin
Comment Utility
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
0
 

Author Comment

by:edsont
Comment Utility
galkin,

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

Regards
0
 
LVL 1

Expert Comment

by:sudhirbrat
Comment Utility

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.
#else
      unsigned short cbElements;
      unsigned short cLocks;
      unsigned long handle;            // Unused but kept for compatibility.
#endif
      void HUGEP* pvData;                         // Pointer to the data.
      SAFEARRAYBOUND rgsabound[1];            // One bound for each dimension.
} SAFEARRAY;


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
TYPE
      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.

0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:edsont
Comment Utility
sudhirbrat,

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 ?

Regards
0
 

Author Comment

by:edsont
Comment Utility
Adjusted points to 100
0
 
LVL 1

Accepted Solution

by:
sudhirbrat earned 100 total points
Comment Utility

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;
    }
      s2_ST;

And the VB structure

TYPE
      lps2 as string
      s2 as Integer
      lpst() as s1_ST
END TYPE

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.
0
 

Author Comment

by:edsont
Comment Utility
sudhirbrat,

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 ?

Regards
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

10 Experts available now in Live!

Get 1:1 Help Now