title: C# COM client (RTD)
body: <p>I'm working with a real time data server (a trading terminal) that has a COM interface for ticker updates.
  From Excel, I'm able to subscribe to the ticker updates using this - </p>
  
  <p><code>RTD(progId,,topic1,topic2, ...)</code></p>
  
  <p>I'm trying to build an application that will receive these ticker updates without any dependency on Excel or any of its APIs. I'm a Java developer with no experience in MS technologies, but I thought I'd give this a shot in C#.</p>
  
  <p>I used OleView to dig up this TypeLib</p>
  
  <pre><code>// TLib :     // TLib : OLE Automation : {00020430-0000-0000-C000-000000000046}
  importlib("stdole2.tlb");
  
  // Forward declare all types defined in this typelib
  interface IScripRTD;
  interface IRTDUpdateEvent;
  
  [
    uuid(A04A3240-D051-4B4C-B004-CA92151F11E5),
    helpstring("ScripRTD Class")
  ]
  coclass ScripRTD {
      [default] interface IScripRTD;
  };
  
  [
    odl,
    uuid(EC0E6191-DB51-11D3-8F3E-00C04F3651B8),
    helpstring("IScripRTD Interface"),
    dual,
    nonextensible,
    oleautomation
  ]
  interface IScripRTD : IDispatch {
      [id(0x0000000a)]
      HRESULT ServerStart(
                      [in] IRTDUpdateEvent* callback, 
                      [out, retval] long* result);
      [id(0x0000000b)]
      HRESULT ConnectData(
                      [in] long topicId, 
                      [in] SAFEARRAY(VARIANT)* strings, 
                      [in, out] VARIANT_BOOL* newValues, 
                      [out, retval] VARIANT* values);
      [id(0x0000000c)]
      HRESULT RefreshData(
                      [in, out] long* topicCount, 
                      [out, retval] SAFEARRAY(VARIANT)* data);
      [id(0x0000000d)]
      HRESULT DisconnectData([in] long topicId);
      [id(0x0000000e)]
      HRESULT Heartbeat([out, retval] long* result);
      [id(0x0000000f)]
      HRESULT ServerTerminate();
  };
  
  [
    odl,
    uuid(A43788C1-D91B-11D3-8F39-00C04F3651B8),
    dual,
    oleautomation
  ]
  interface IRTDUpdateEvent : IDispatch {
      [id(0x0000000a)]
      HRESULT UpdateNotify();
      [id(0x0000000b), propget]
      HRESULT HeartbeatInterval([out, retval] long* value);
      [id(0x0000000b), propput]
      HRESULT HeartbeatInterval([in] long value);
      [id(0x0000000c)]
      HRESULT Disconnect();
  };
  </code></pre>
  
  <p>I came across this <a href="http://msdn.microsoft.com/en-us/library/aa645736%28v=vs.71%29.aspx" rel="nofollow noreferrer">example</a>, but the part about using TlbImp to autogenerate the C# interfaces is not clear to me. Could someone provide a better example of how to instantiate this interface and register a callback to get the ticker updates?</p>
  
  <p>Incidentally, I was able to whip up a quick AHK script that comes close to what I'm looking for - </p>
  
  <pre><code>get_price(symbol)
  {
      comObj := ComObjCreate(progId)
      num := ComObjParameter(3, 5)
      update := ComObjParameter(0xB, -1)
      arr := ComObjArray(VT_VARIANT:=12, 3)
      arr[0] := "MktWatch"
      arr[1] := symbol
      arr[2] := "Last Traded Price"
      price := comObj.ConnectData(num, arr, update )
      return price 
  }
  </code></pre>
  
  <p>This does not register a callback, but if I run this with a timer, I'm able to get ticker updates for individual symbols.</p>
  
  <p>In case it makes a difference, I'm using Win 7 x64.</p>
  
  <p>I have looked at this <a href="https://stackoverflow.com/questions/3601997/excel-rtd-real-time-data-client-other-than-excel?rq=1">example</a>, which seems to use reflection instead of declaring the COM interface, but I couldn't get it to work.</p>