Writing a GUI in C#

Discussion in 'Trading' started by Fredrik99, Apr 28, 2006.

  1. Hello everybody!

    I am trying to write a GUI for TWS in C#. IB supplies a nice little
    VB -sample that I am studying (TWSAPI_VBSa).In this example there is a connection dialog where three values are entered:
    The Ip address (as a string), a port number (as an int)and a clienti d(as an int).

    I tried to copy this "concept" to C#, as follows:

    imported a dll from vb,
    created an object of the TwsClass called "Test";
    TwsClass Test= new TwsClass();

    Declared three variables to use in the parameter list:

    int port = 7496;
    int ClientId = 0;
    string IPStr = "";

    I called the method:

    Test.connect(IPStr, port, ClientId);

    ...and got this error:

    Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
    at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
    at TWSLib.TwsClass.connect(String host, Int32 port, Int32 clientId)

    Why can´t I do this?
    If anybody has a clue please let me know.
    Has anybody else succeded in communicating with the TWS api from C#?


  2. Why is your IPStr blank?
  3. It is blank ,because I noticed that this is what you do in the VB app if the ip is on your local host.

    However I have tried my local IP as a string as well:

    Test.connect("", port, ClientId)

    This does not work either. That is-it does not work in C#.
    Both a blank and the ip as a string works in the VB-app.
  4. geringer


    I got a similar message trying to connect to TWS via
    JScript / VBScript. Never got it to work. Would be curious
    to see what the answer is.
  5. NinjaTrader_Ray

    NinjaTrader_Ray ET Sponsor

    If I recall correctly, you have to drop the TWS ActiveX control on a form to be able to use it. You can avoid this by communicating via socket interface.

  6. Wow!
    I really hop this is the one!!
    Where do I get theTWS ActiveX control ?

  7. NinjaTrader_Ray

    NinjaTrader_Ray ET Sponsor

    Just add the OCX that comes with the API to your project. Take a look here


    I posted some VB sample code nearly 3 years ago. There are also some .NET Socket wrappers I believe.
  8. GTG


    (1) Start a new windows forms project from scratch.
    (2) Go to the form Designer view.
    (3) Right-click on a blank spot in the toolbox.
    (4) Select "Choose Items"
    (5) Click on the "COM Components" tab
    (6) Check the check box by "TWS Control" in the listview.
    (7) You now have a component somewhere in your toolbox (sometimes it puts it in wierd places it seems) called "TWS Control". Find it and drag it onto your form.
    (8) You now have a variable declared and initialized called axTws1 or something like that.
    (9) I'd set the ipstring thing to "localhost" myself, but blank probably works fine too.
  9. Thank´s GTG!
    That worked! I now have a control called axTws1, and the intellisense (in Visual Studio) supplies a long list of methods that I can use. One of them is "Connect()"