Building a C++ Application Without an Import Statement

Using AppConn COM from C++ without using the import statement provides an efficient way to use the AppConn COM connector without having to use the VC++ compiler. The following topics explain how to begin a C++ project without using an import statement.

Overview

The following files contain the definitions needed to create and use the AppConn controls: appconn_i.c and appconn.h (located in the <VHI install folder>\include folder). The appconn_i.c file contains the AppConn class and interface Ids. This file should be compiled and linked with the program that uses AppConn. The appconn.h file contains the interface definitions for all of the AppConn objects, all of the AppConn constants, and external references to the AppConn class and interface Ids defined in appconn_i.c. This file should be included in source files that use AppConn objects using the following statement:


#include "appconn.h"


AppConn objects are accessed using an interface pointer to the object. To declare an interface pointer, use the following statement:


IAppConnModel *pSession;

Creating an AppConn Object

To create an AppConn object, use the CoCreateInstance method, passing the class Id, the aggregate object's IUnknown interface, the class context, the interface Id, and a pointer to the interface pointer. CoCreateInstance will return an HRESULT, which can be checked for success or failure.

HRESULT hr = CoCreateInstance(CLSID_AppConnModel,
     NULL, 
     CLSCTX_INPROC_SERVER, 
     IID_IAppConnModel, 
     (LPVOID *)&pSession);

Object lifetimes must be managed explicitly. When an object is assigned to another interface pointer AddRef must be called on the interface, and when the interface is no longer used Release must be called. When the reference count goes to 0 the object will be deleted.


IAppConnModel *pModel = pSession;
pModel->AddRef();
pSession->Release();
pSession = NULL;

Converting C++ Types Into COM Types

Calling AppConn methods may require converting C or C++ types into COM types. Strings must be converted to BSTR using SysAllocString to create the string and SysFreeString to destroy it. bool must be converted to VARIANT_BOOL and set to either VARIANT_FALSE (0) or VARAINT_TRUE (-1). Variant parameters must be initialized using VariantInit and destroyed using VariantClear. Optional parameters are declared as VARIANT and may be set as VT_EMPTY or VT_NULL to indicate that the parameter is not being included. Return parameters are returned to the caller as a pointer to an object in the last parameter of the method. All methods return an HRESULT.

  BSTR bstrServer;
  BSTR bstrModel;
  VARIANT varUserID;
  VARIANT varPassword;
  VARIANT varModelVariables;
  
  bstrServer = SysAllocString(L"localhost");
  bstrModel = SysAllocString(L"CCSDemo");
  VariantInit(&varUserID); //Will set VARIANT to VT_EMPTY
  VariantInit(&varPassword); //Will set VARIANT to VT_EMPTY
  VariantInit(&varModelVariables); //Will set VARIANT to VT_EMPTY
  hr = pSession->ConnectToModel(
     bstrServer,
     bstrModel,
     varUserID,
     varPassword,
     varModelVariables);
     SysFreeString(bstrServer);
     SysFreeString(bstrModel);
     VariantClear(&varUserID);
     VariantClear(&varPassword);
     VariantClear(&varModelVariables
 );

When a method call fails, the HRESULT may contain a standard Windows error code or a custom AppConn error code. The AppConn error codes are as follows:


APPTRIEVE_ERROR = 0x801E0001;
CHANNEL_ERROR = 0x801E0002;
SERVER_ERROR = 0x801E0003;
MARSHALLER_ERROR = 0x801E0004;
TIMEOUT_ERROR = 0x801E0005;
MODEL_DATA_ERROR = 0x801E0006;
MODEL_DEF_ERROR = 0x801E0007;
TERMINAL_ERROR = 0x801E0008;
DEAD_SESSION_ERROR = 0x801E0009;
USER_EXCEPTION_ERROR = 0x801E000A;
LOCKING_ERROR = 0x801E000B;
ALREADY_CONNECTED_ERROR = 0x801E000C;
NOT_CONNECTED_ERROR = 0x801E000D;
INVALID_ARG_ERROR = 0x801E000E;
INVALID_POINTER_ERROR = 0x801E000F;
BAD_VAR_TYPE_ERROR = 0x801E0010;
OUT_OF_MEMORY_ERROR = 0x801E0011;
ELEMENT_DOES_NOT_EXIST_ERROR = 0x801E0012;
ELEMENT_ALREADY_EXISTS_ERROR = 0x801E0013;
UNKNOWN_ERROR = 0x801E0014;

Error Handling

To get more specific error information, retrieve the ErrorInfo object. The ErrorInfo object is a standard COM object that can be retrieved by calling GetErrorInfo. ErrorInfo is stored in thread local storage, so there can only be one ErrorInfo object. AppConn will set the ErrorInfo when an error occurs and clear the ErrorInfo on the next call to an AppConn object. The ErrorInfo object contains a textual description of the error that can be accessed with the GetDescription method.


  IErrorInfo *pErrorInfo;
  LPOLESTR strDescription;
  if (SUCCEEDED(GetErrorInfo(0, &pErrorInfo)))
  {
      pErrorInfo->GetDescription(&strDescription);
      printf("Error: %s\n", OLE2A((LPOLESTR) strDescription));
      SysFreeString(strDescription);
      pErrorInfo->Release();
  }






 

 

  Attachmate