1.50 1.51 1.52 | 2.00 2.10 2.20 4.00 4.10
WINDOWS | WINDOWS NT
kbole kbprg kbinterop kbcode
The information in this article applies to:
- The Microsoft Foundation Classes included with:
- Microsoft Visual C++ for Windows, versions 1.5, 1.51, 1.52
- Microsoft Visual C++, 32-bit Edition, versions 2.0, 2.1, 2.2,
4.0, 4.1
SUMMARY
It is often necessary to access properties within nested objects exposed by
OLE Automation. The Visual Basic syntax is:
OBJ1.OBJ2.Property
This article demonstrates the creation of a simple OLE Automation server
with Visual C++, accessible from Visual Basic, that exposes such an object.
MORE INFORMATION
The following steps demonstrate how to create a Visual C++ OLE Automation
server that allows Visual Basic to use the OBJ1.OBJ2.Property syntax.
Steps to Create the Visual C++ OLE Automation Server
- Create an AppWizard-generated project called AutoServ with OLE
Automation enabled.
- Once the project has been generated, start ClassWizard.
- Click the OLE Automation tab.
- Click the Add Class button, enter the following values, and then click
Create Class:
Class Name: nested
Class Type: CCmdTarget
Check: OLE Automation
- Click the ClassWizard's OLE Automation tab. Set the Class Name to
nested, click the Add Property button, enter the following values, and
then click OK:
External Name: Value
Type: long
- Change the Class Name in ClassWizard to CAutoServDoc. If you are
prompted to save changes, click Yes. Then click the Add Method button,
enter the following values, and click OK:
External Name: Nested
Return Type: LPDISPATCH
- Click OK to accept the additions created by ClassWizard.
- Open the project's AutoServDoc.h file, and add the following line to
the beginning of the file:
#include "nested.h"
- To the same file, add a public member variable m_nested of type nested.
A pointer mechanism could have been used to maintain the nested class;
however, for this example, the chosen method will automatically create
and destroy the nested object within the Documents constructor and
destructor respectively.
- Open the project's nested.h file, and modify the class so that the
constructor and destructor are public methods.
- Open the projects AutoServDoc.cpp file, and modify the Nested Method
as follows:
LPDISPATCH CAutoServDoc::Nested()
{
//TODO: Add your dispatch handler code here
return m_nested.GetIDispatch(TRUE);
}
- Build the project, and then run AutoServ.exe to register the server.
Steps to Test the Server in Visual Basic
- Start a new project in Visual Basic that will be used to test the OLE
Automation server.
- On the View menu, click Code.
- In the Object combo box, select (general).
- Enter the following code:
Dim Server As object
- Select Form in the Object combo box. Modify the Sub procedure as
follows:
Sub Form_Load ()
Set Server = CreateObject("autoserv.document")
Server.Nested.Value = 10
x = Server.Nested.Value
End Sub
- Add x and Server.Nested.Value to the Visual Basic watch window, so that
you can observe the changes while stepping into the Visual Basic
program.
|