views:

134

answers:

1

Hi, I've been trying to run the sample project provided with the tutorial at stockbotprogramming http://www.stockbotprogramming.com/sharpcibtutorial1.php , but I keep getting a COMException every time I run the application.

I have the TWS Client running and the sample VB projects provided with the API are able to connect just fine, but when I try to use the C# sample provided by the tutorial then I get the following exception:

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in System.Windows.Forms.dll

Th exception happens when I try to add the TWS ActiveX control:

namespace CSharpTutorial1
{

    public partial class Form1 : Form
    {
        private AxTWSLib.AxTws tws;
        public Form1()
        {
            InitializeComponent();         
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            tws = new AxTWSLib.AxTws();
            tws.BeginInit();
            tws.Enabled = true;
            tws.Location = new System.Drawing.Point(32, 664);
            tws.Name = "tws";
            Controls.Add(tws); // <-- EXCEPTION HERE!
            tws.EndInit();
            tws.connect("127.0.0.1", 7496, 0);
            String msg = "Connected to TWS server version " + tws.serverVersion + "at " + tws.TwsConnectionTime;
            MessageBox.Show(msg);
        }
    }
}

The original project was probably done with Visual Studio 2005, but I have Visual Studio 2008 and it automatically converted the project (I've been reading that there are some problems there). Does anybody know what could be causing this exception? Any ideas on how to fix it?

+1  A: 

(1)Importent: Make sure you add the component to the IDE using the visual designer. First right click in the toolbox, click "choose items" , click the COM tab, then check the TWS control. Now using the visual IDE drag this item to the surface of your form. It must be visable in your app. (Don't create it in the Form1_Load())

(2)Do something like this... (don't use an IP Address of 127.0.0.1, leave it blank) axTws1.connect("", 7496, 0); axTws1.reqMktData(0, "AMD", "STK", "", 0, "", "", "SMART", "ISLAND", "USD", "", 0);

(3)Last(if you're using and 64 bit windows) compile the project in 32 bit code. Go to the configuration manager and then create a new profile "x86" and set all the configurations to x86.

I hope this helps. Ryan

Ryan White
*Finally!* Do you work for IB or something? In any case, thanks for the help!
Lirik