How to: Check if connected to Automation


Topic:

Check if connected to Automation


Class hierarchy:

IPCsApplication


Description:

To check weather a variable of IPCsApplication , is assigned to Automation, simply check for nil/null.
To access projects, symbols, text etc. in Automation, you need to to go through a variable of
IPCsApplication


Sourcecode:
[+] Delphi
uses DPSCAD_TLB;

var
  PCsApplication : IPCsApplication;

function TMainForm.CheckConnection() : boolean;
begin
  if (PCsApplication = nil) then
    begin
      try
        {tries to connection to Automation}
        PCsApplication := coApplication.Create();
        result := true;
      except
        {connection to Automation failed}
        On E:Exception do
          begin
            ShowMessage('Can''t connect to Automation' + #13#10 + E.ClassName +
            ' error raised, with message : '+E.Message);
            result := false;
          end;
      end;
    end
  else
    result := true;
end;

[+] Visual Basic
Dim PCsApplication As Dpscad.IPCsApplication

If PCsApplication Is Nothing Then
  Try
    'tries to connection to Automation
    Dim PCs As IPCsApplication
    Set PCs = CreateObject("PCsELautomation.Application")
    Return True
  Catch e As Exception
    'connection to Automation failed
    MessageBox.Show("Can't connect to Automation! Error: " & e.Message, "Connection to Automation failed", MessageBoxButtons.OK, _
      MessageBoxIcon.Error)
    Return False
  End Try
Else
  Return True
End If

[+] C#
Dpscad.IPCsApplication PCsApplication;

if (PCsApplication == null)
{
  try
  {
    /*tries to connection to Automation*/
    Type PCs = Type.GetTypeFromProgID("PCsELautomation.Application");
    PCsApplication = (Dpscad.IPCsApplication)Activator.CreateInstance(PCs);
    return true;
  }
  catch (Exception e)
  {
    /*connection to Automation failed*/
    MessageBox.Show("Can't connect to Automation! Error: " + e.Message,
      "Connection to Automation failed", MessageBoxButtons.OK,
      MessageBoxIcon.Error);
    return false;
  }
}
else
{
  return true;
}