C# class library for TWSLink

Using TWSLink in C#  projects is possible in 2 ways:

  1. Add Reference of the TWSLink COM module to your project. The function calls of TWSLink exported functions look a bit unusual because of their get/set prefix. [ddownload id=”409″ text=”Example for download” style=”link”].
  2. Add C# class libary of TWSLink to your C# project. Function calls look same as in other TWSLink projects.

The 2nd is what this post is about.

Aside the binaries, the package (download below) contains a solution with 3 projects to create the class library on your own:

  1. twslink2cs
    This generates a c# wrapper DLL for TWSLink along with c# class files to call this wrapper DLL exported functions
    from .NET
  2. twslink2cslib
    This is the final .NET class library for TWSLink using the c# class library files
    from project 1
  3. twslink2csapp
    A c# Form example application which demonstrate how to use the C# .NET class library
    -> add twslink2cslib as reference
    -> create variable of TWSLink2Wrap in your form:
    private TWSLink2Wrap _twsl;

The first 2 projects can be used to create your own handy c# library from TWSLink. This is only required when TWSLink changes its interface (add,remove, change function). The 3. project demonstrate how to use this handy library.

ToDo list how to update TWSLink C# class library when a new TWSLink version changed interface. 

  • rebuild twslink2cs
  • Copy twslink2cs.dll into windows/system32 folder
  • On 64 bit Windows, make sure to select the x64 build
    if you like to use 32 bit .NET apps, then also
    do the 32 bit build and copy resulting twslink2cs.dll
    to windws/syswow64 folder
  • Copy all .cs files generated to twslink2cslib project, because
    those files been changed, if TWSLink interface changed.
  • rebuild twslink2cslib
  • rebuild (adjust) your .net app having twslink2cslib as reference

[ddownload id=”915″ text=”Download C# class library for TWSLink” style=”link”]

Show sample code how to use class library

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;



namespace twslink2csapp
{
    
    public partial class Form1 : Form
    {
        private TWSLink2Wrap _twsl;

        public Form1()
        {
            InitializeComponent();
            test_connect();
        }
       
       
        #region test
        void    test_connect()
        {
            int ret=connect("",7496,0,2000);
        }
        #endregion

        #region twslink_instance
        private TWSLink2Wrap TL()
        // create a TWSLink COM instance
        {
            if (_twsl == null)
                _twsl = new TWSLink2Wrap("",1);
            return _twsl;
        }
        #endregion

        #region twslink_interface
        private int connect(string host,int port,int clientid,int timeout)
        {
            // connect to TWS or gateway;this will create a TWSLink instance and you can see the TWSLink tray icon in the taskbar
            int ret=TL().CONNECT(host,port,clientid,timeout);
            return ret;
        }
        #endregion

   
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.