Link to home
Start Free TrialLog in
Avatar of Axy
Axy

asked on

vc++ 6.0

I have defined some class properties or methods of myselves in an MFC ActiveX . But I can't use it in VB.
For example, I defined two classes named CPoint and CPoints use MFC class wizard as follow:
  class CPoint : public CCmdTarget
  {
   public:
      ...
   public:
     X : double;
     Y : double;
     ...(methods and properties)
   };

  class CPoints : public CCmdTarget
  {
   public:
      ...
   public:
     CPoint Items(short Index);
     void Add(CPoint aPoint);
     ...(methods and properties)
   };
   
   class CMyActiveXCtl : public COleControl
   {
    ....
    };

   Now I suppose that I will use these two ActiveX Automation objects in VB 6.0 as follow:
   dim pt as new Point
   dim pts as new Points
   pt.x = 100.1
   pt.y = 200.1
   pts.Add(pt)
   
   May you tell me how can I realize these? If possible, please give me the actual steps and detail source code of the example.Thanks a lot.

Avatar of umzilber
umzilber

Use ATL in VC++.

Steps:
1.  Create  a project in VC++ as an ATL project, choose DLL or ACTIVEX/CLIENT
2. use  Insert->New ATL Object to create a new ATL object (choose Simple Object)
3. right-click on the MIDL extension objject and add a property.
4 write the code for that property
5. compile the OCX
6. Use the OCX in VB
7. Done


Look on Microsofts web site on in MSDN for ATL tutorial and follow that.

Max

ASKER CERTIFIED SOLUTION
Avatar of gpeddle
gpeddle

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Axy

ASKER

Sir, thanks very for your help, I have found another method to resolve the question, that is to use the FromIDispatch() and GetIDispatch(), I felt it is more convenient than the method you give me, do you think so? In another hand, if you can tell me how can I define an array property in VC? For example, Item is an arrry proerty of Points, and I will use it in VB as following:
                   Dim pts as new Points
                   Dim p as new Point
                   pts.add p
                   Dim p1 as new Point
                   set p1 = pts(0)

Note: the Item is an default property of points in VC++.
How can I define _NewEnum proeperty in class Points?

The LPDISPATCH pointer you get is equivalent to the pointer you get from calling GetIDispatch, no difference.  When you call Points.Add from VB, the Point that you are adding is passed to VC as an LPDISPATCH.
=======================================================
RE 'set p1 = pts(0) '

You cannot do what you are proposing, since all communication
between VC an VB must use 'Automation' types.  It is not possible
to expose an overloaded [] operator to the VB.

You could just treat Points as a VB array and then pass it to C++ once all the Point objects are created and put into the array.  The VB array is what is called a SAFEARRAY, and it would be passed to VC as a VARIANT that contains a SAFEARRAY. You would have to write some pretty tricky code to get the values out from a VB SAFEARRAY passed to VC. Read about it and see if it makes sense to you.

OR to do the same thing in VC:

In order to do what you want, you chould write something like:

void Points::SetAt(LPDISPATCH item, long index)
{
      // Now set the item into the
      // (internal) array at the desired index
      // assume that the array is a CPtrArray
      m_Array[index] = item;

}


Also, NOTE that the VC to VB communication set up by ClassWizard
doesn't set up the internal array you'll need to create for storing the items you want to collect using either the Add() call we discussed yesterday or the SetAt() above.

You have to create the array as part of the class, perhaps in
the constructor.  The Add() or SetAt() methods just give you a way to pass the items from VB to VC++ where you can then write C++ code to put the items into the C++ array.

Go to the documentation and read CPtrList and CPtrArray.

Also, don't forget to review the AddRef and Release methods
from IDispatch.

Good Luck.