InvalidCastException When using ActiveX in C# project

♀尐吖头ヾ 提交于 2019-11-28 06:17:36

问题


Dear all trader and API Gurus,

Thank you in advance.

Background:

  1. Visual Studio 2010, C# Language, ActiveX OCX API as reference of project.
  2. Successively to use the method provided by API like reqAccountUpdate and use the event to receive information about account update.

Issue:

  1. Now I would like to require data about one specific financial instrument like a forex pair: AUD.USD .

a. Step 1: Inherit the interface of class IContract from TWSLib my Code is here as a picture and the constructor is specific for AUD.USD: a busy cat http://i.minus.com/iyG7RLuSGxiEl.jpg

As you can see that I have change the private object _combolLegs to private TWSLib.IComboLeg _comboLegs

b. Step 2: I also inherit the IComboleg and IUpdateComp to ComboLeg and UpdateComp and I do not assign any value to them since they are not related to the the AUD.USD forex trading.

c. Now I create a instance of contract and try to assign all information forex contract required for and submit my query. a busy cat http://i.minus.com/iXvgbSsp4MbYz.jpg

d. it is a runtime error. Please check the picture here: a busy cat http://i.minus.com/ifFxdfnyvfKF9.jpg

It seems like the contract instance I created could not be accepted by the api as formal IContract instance. Any idea from any .NET gurus?:)

Thanks lot in advance again,

Humble learner.

Wenhao

Edit

about the detail of IContract all I got sofar

a busy cat http://i.minus.com/iAoY8rCvbC22z.jpg


回答1:


//Try changing the way that you implement the Interface / Clase where you have 
// -- var auduse = new Contract();

//depending on boxing and unboxing from this point try to reverse it and do the following
IContract iaudUse = new Contract();
//then you would use iaudUse. what ever method in the Interface that you are wanting to use.


来源:https://stackoverflow.com/questions/8477107/invalidcastexception-when-using-activex-in-c-sharp-project

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!