Thursday, 17 January 2019

How to Integrate SecsToHost.Net to Native C++ Code

PDF | Print |

This step-by-step article describes how easy it is to integrate SecsToHost.Net SDK into your Native Visual C++ code. To do this, we will create a simple MFC Dialog based application for demontration.


The following list outlines the recommended softwares that you need in order to follow this tutorial:

  • .NET Framework 2.0 or above
  • Visual Studio 2003 or Visual Studio 2005
  • SecsToHost SDK v2.0
  • Basic SECS/GEM knowledge

Getting started

Step 1: COM Registration

Before you start importing all the necessary libraries from SecsToHost.Net, first you must register them in the windows registry. To do so, please run the RegisterCOM.exe utility located in your [INSTALLATIONFOLDER]/SecsToHost.Net 2.0 SDK.

Note: Administrator right is required in order to register the entries successfully to windows registry.

Step 2 - Setting Up Visual C++ DEMO Project

In this article, we are going to create a simple HMI application which utilizes all the SECS/GEM capabilities provided by SecsToHost.Net. For simplicity (at least on the GUI side), we will create a MFC dialog based application to show you how to integrate SecsToHost.Net interface into your native Visual C++ application.

  1. Start Microsoft Visual Studio 2005 and select MFC Application
  2. Enter a demo project name "COMGEMStationVC"

The Visual C++ project wizard will generate "COMGEMStationVCDlg.h" and "COMGEMStationVCDlg.cpp" file.

Step 3: Importing SecsToHost.Net Libraries 

In your [INSTALLATIONFOLDER] you should have the following typed libraries:

  • Insphere.Connectivity.SecsToHost.Net.tlb
  • Insphere.Connectivity.Common.tlb
  • Insphere.Connectivity.Common.ToolModel.tlb
  • Insphere.Connectivity.Application.Objects.tlb
  • Insphere.Connectivity.Application.MessageServices.tlb
  • Insphere.Connectivity.Application.Exceptions.tlb

And these core .h, .dll and .lib:

  • CGEMController.h: header file that define the COMGEMController class and other events callback
  • CGEMController.lib: the core library file for development use.
  • CGEMController.dll: the core library file for runtime use.

Copy all the libraries mentioned above to your project folder (one level up from Debug or Release folder). Next, import the CGEMController.h and CGEMController.lib by drag and drop into your project Header Files and Resource Files respectively. If you have successfully drag the files, it would be shown like this:


Add the following code to the COMGEMStationVCDlg.h

// GEMController header file
#include "CGEMController.h"

Step 4: Initialize SecsToHost.Net Interface

Add the following codes to the COMGEMStationVCDlg.h to 

// Initialize COMGEMController object and register all the callbacks
void InitializeSecsToHost();
// Global variable to hold the COMGEMController reference
ICOMGEMControllerPtr m_GEMController;
// Callback handler for CSECsPrimaryInEvent
void CSECsPrimaryInEventHandler(CGEMController &sender, ICOMSECsPrimaryInEventArgs &args);
// Callback handler for CSECsSecondaryInEvent
void CSECsSecondaryInEventHandler(CGEMController &sender, ICOMSECsSecondaryInEventArgs &args);
// Callback handler for CCommunicationStateChangedEvent
void CCommunicationStateChangedEventHandler(CGEMController &sender, ICOMSECsEventArgs &args);
// Callback handler for CControlStateChangedEvent
void CControlStateChangedEventHandler(CGEMController &sender, ICOMSECsEventArgs &args);
// Callback handler for CProcessingStateChangedEvent
void CProcessingStateChangedEventHandler(CGEMController &sender, ICOMSECsEventArgs &args);
// Callback handler for CMessageActivityTraceEvent
void CMessageActivityTraceEventHandler(CGEMController &sender, ICOMSECsMessageActivityEventArgs &args);
// Callback handler for CSpoolingStateChangedEvent
void CSpoolingStateChangedEventHandler(CGEMController &sender, ICOMSECsEventArgs &args);
// Callback handler for CClockChangedEvent
void CClockChangedEventHandler(CGEMController &sender, ICOMSECsClockEventArgs &args);
// Callback handler for CRemoteCommandInEvent
void CRemoteCommandInEventHandler(CGEMController &sender, ICOMSECsRemoteCommandEventArgs &args);

Next, we will need to implement the InitializeSecsToHost() method in COMGEMStationVCDlg.cpp to get the COMGEMController reference and hook up all the callback:

void CCOMGEMStationVCDlg::InitializeSecsToHost()
CGEMController* p_GEMController = new CGEMController();
// Get the ICOMGEMControllerPtr reference
m_GEMController = p_GEMController->GetGEMController();
// Initialize the SecsToHost.Net
m_GEMController->Initialize("EquipmentTemplate.xml", "C:/Temp");
//// Register all callbacks
p_GEMController->CSECsPrimaryInEventCallback.push_back(CreateClosure(this, &CCOMGEMStationVCDlg::CSECsPrimaryInEventHandler));
p_GEMController->CSECsSecondaryInEventCallback.push_back(CreateClosure(this, &CCOMGEMStationVCDlg::CSECsSecondaryInEventHandler));
p_GEMController->CCommunicationStateChangedEventCallback.push_back(CreateClosure(this, &CCOMGEMStationVCDlg::CCommunicationStateChangedEventHandler));
p_GEMController->CControlStateChangedEventCallback.push_back(CreateClosure(this, &CCOMGEMStationVCDlg::CControlStateChangedEventHandler));
p_GEMController->CProcessingStateChangedEventCallback.push_back(CreateClosure(this, &CCOMGEMStationVCDlg::CProcessingStateChangedEventHandler));
p_GEMController->CMessageActivityTraceEventCallback.push_back(CreateClosure(this, &CCOMGEMStationVCDlg::CMessageActivityTraceEventHandler));
p_GEMController->CSpoolingStateChangedEventCallback.push_back(CreateClosure(this, &CCOMGEMStationVCDlg::CSpoolingStateChangedEventHandler));
p_GEMController->CClockChangedEventCallback.push_back(CreateClosure(this, &CCOMGEMStationVCDlg::CClockChangedEventHandler));
p_GEMController->CRemoteCommandInEventCallback.push_back(CreateClosure(this, &CCOMGEMStationVCDlg::CRemoteCommandInEventHandler));

The codes above first instantiate and get the iCOMGEMControllerPtr reference for m_GEMController variable with the CGEMController class. The next line of code calling the m_GEMController->Initialize method which takes two parameters:

  • EquipmentModel full path filename. In the example above, you must make sure the EquipmentTemplate.xml in the same project path.
  • LogfilePath. Specify the path where the log files will be generated.

The last part of the codes sync the callback events raised by COMGEMController class. Now we have hooked up all the codes necessary to start your specific equipment SECS/GEM application.