Developer Guide and Reference

ID 767251
Date 10/31/2024
Public
Document Table of Contents

COM Server Concepts

This topic only applies to Windows.

The Fortran COM Server application wizard generates an initial Visual Studio project and creates a COM Hierarchy file, which describes the infrastructure of a COM server. You can modify this file with the COM Server Hierarchy Editor to define the implementation of one or more object classes, including its interface(s), and method(s).

When you save the hierarchy file, the Fortran source files are generated. You then need to write code to implement each method.

What Information You Need to Provide

When you create the COM Server project, you will need to describe the COM server classes that you want to implement.

A class implements one or more COM interfaces. In COM terminology, an interface is a semantically related set of functions. The interface as a whole represents a feature, or a set of related functionality, which is implemented by the class. An interface contains methods, otherwise known as member functions. A method is a routine that performs one of the actions that make up the feature. As far as the Fortran COM Server is concerned, methods are Fortran functions that take arguments and return a value like any other Fortran function.

Consider a simple example of a class that you will create using the Fortran COM Server Application Wizard, called AddingMachine. The class contains a single interface that we call IAdd. By convention, all interface names begin with a capital letter I. You can define three methods in the IAdd interface:

  • Clear, which takes no arguments and sets the current value of the adding machine to 0.

  • Add, which takes a single REAL argument, the amount to add to the current value.

  • GetValue, which returns the current value.

These methods allow you to perform specific, distinct tasks with the IAdd interface from any language that supports a COM client. The Fortran COM Server Editor provides a user interface to enter this information about the class (in this case the AddingMachine class), which is discussed later in Create the Fortran COM Server.

An interface can also contain properties. Properties are method pairs that set or return information about the state of an object. Properties must currently be implemented using the get_Method and put_Method and the same DISPID property.

In terms of the data associated with an object, a key concept of object-oriented programming is encapsulation. Encapsulation means that all of the details about how the object is implemented, including the data that it uses, and the logic that it uses to perform its work, is hidden from the client. The client's only access to the object is through the interfaces that the object supports.

You need to define the data that the object uses and code the logic of the methods. For the data, the Fortran COM Server Application Wizard uses the model that each instance of the object has an associated instance of a Fortran derived type. The code generated by the wizard takes care of creating and destroying the instances of the derived type as objects are created and destroyed.

You define the fields of the derived type. For example, with our AddingMachine class, each AddingMachine object needs to store the current value. The derived type associated with each AddingMachine object would contain a single field of type REAL. We name it CurrentValue.

Note that each instance of the AddingMachine object has its own instance of the derived type and its own CurrentValue. This means that the server could support multiple clients simultaneously and each client would see its own AddingMachine. That is, each client is unaffected by the existence of other clients. The derived type associated with each object is discussed in detail in Create the Fortran COM Server.

To summarize, this is what you need to do to create a COM server:

  • Create the COM Server project using the COM Server Application Wizard.

  • Define the classes, interfaces, methods, and properties using the COM Server Hierarchy Editor to create the Hierarchy file.

  • Define the fields in the derived type that is associated with each instance of a class.

  • Write code that initializes the fields in the derived type (if necessary), and code that releases any resources used by the fields in the derived type (if necessary).

  • Write the code that implements the methods.

What the COM Server Will Provide

The Fortran COM Server Editor generates source files that implement all of the infrastructure, or plumbing, of the COM server. The generated files take care of such tasks as follow:

  • Defining the GUIDs that uniquely identify your classes and interfaces.

    For a discussion of GUIDs, see Get a Pointer to an Objects Interface.

  • Registering the server and your classes and interfaces on the system.

  • Implementing the class factory that creates instances of your object.

  • Creating the Interface Definition Library (IDL) and type library that describes your classes and interfaces.

    For a discussion of type libraries, see Use the Module Wizard to Generate Code.

  • Implementing the IUnknown and IDispatch interfaces for your object.

    All of the interfaces created by the Fortran COM Server Editor are derived from IUnknown or IDispatch (dual interfaces).

  • Creating and destroying instances of the derived type associated with your object.

  • Invoking the Fortran routines that implement your object's methods.

The majority of these source files are generated fully by the Fortran COM Server Editor and are not modified by you. Other files contain the skeleton or template of your derived type and methods. You edit these Fortran source files to fill in your implementation.

Create the Fortran COM Server provides a walk-through of the AddingMachine example. It will show you how it's done.