pazival01 發表於 13-3-5 13:23
印象上 好像有兩個function, 看看要不要用另一個
--
透過轉換,把『群益報價api』的程式碼由VB轉成C#, 目前只能收到約4分鐘的台指期貨TX00報價, 謝謝。 C#的程式碼如下: using Microsoft.VisualBasic; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Diagnostics; using System.Runtime.InteropServices; public class Form1 { // *** DLL Import //* 0. SKQuoteLib_Initialize 初始使用者相關資訊 [DllImport("SKQuoteLib.dll",CallingConvention = CallingConvention.StdCall)] public static extern intSKQuoteLib_Initialize(string lpszLoginID, string lpszPassword); //* 1.SKQuoteLib_AttachConnectionCallBack 向報價函式庫註冊接收連線狀態的 Call back 函式位址 [UnmanagedFunctionPointer(CallingConvention.StdCall)] public delegate voidFOnNotifyConnection(int nKind, int nCode); static FOnNotifyConnectionDelegate_AttachConnection = new FOnNotifyConnection(OnConnectionBack); [DllImport("SKQuoteLib.dll",CallingConvention = CallingConvention.StdCall)] public static extern intSKQuoteLib_AttachConnectionCallBack(FOnNotifyConnectionDelegate_AttachConnection); //* 2.SKQuoteLib_AttachQuoteCallBack 當有索取的個股報價異動時,將透過此註冊的函式通知應用程式處理 [UnmanagedFunctionPointer(CallingConvention.StdCall)] public delegate voidFOnNotifyQuote(short sMarketNo, short sStockidx); public static FOnNotifyQuoteDelegate_AttachQuote = new FOnNotifyQuote(OnQuoteCallBack); [DllImport("SKQuoteLib.dll",CallingConvention = CallingConvention.StdCall)] public static extern intSKQuoteLib_AttachQuoteCallBack(FOnNotifyQuote Delegate_AttachQuote); //* 3.SKQuoteLib_EnterMonitor 建立與報價伺服器的連線 [DllImport("SKQuoteLib.dll",CallingConvention = CallingConvention.StdCall)] public static extern intSKQuoteLib_EnterMonitor(); //* 4.SKQuoteLib_RequestStocks 要求伺服器針對pStockNos內的商品代號作報價通知的動作 [DllImport("SKQuoteLib.dll",CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern intSKQuoteLib_RequestStocks(ref short psPageNo, string pStockNos); //* 5.SKQuoteLib_GetStockByIndex 根據市場別編號與系統所編的特殊代號,取回股票報價的相關資訊 [DllImport("SKQuoteLib.dll",CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern intSKQuoteLib_GetStockByIndex(short sMarketNo, short sIndex, ref TStock pStock); //* 6.SKQuoteLib_LeaveMonitor 中斷與報價伺服器的連線 [DllImport("SKQuoteLib.dll",CallingConvention = CallingConvention.StdCall)] public static extern intSKQuoteLib_LeaveMonitor(); //* 7.SKQuoteLib_GetStockByNo [DllImport("SKQuoteLib.dll",CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern intSKQuoteLib_GetStockByNo(String lpszStockNo, ref TStock pStock); //* 8.SKQuoteLib_AttchServerTimeCallBack 呼叫伺服器時間 [UnmanagedFunctionPointer(CallingConvention.StdCall)] public delegate voidFOnNotifyServerTime(Int16 tHour, Int16 tMinute, Int16 tSecond, Int16 tTotal); static FOnNotifyServerTimeDelegate_ServerTime = new FOnNotifyServerTime(CBFunc_ServerTime); [DllImport("SKQuoteLib.dll",CallingConvention = CallingConvention.StdCall)] public static extern intSKQuoteLib_AttchServerTimeCallBack(FOnNotifyServerTime Delegate_ServerTime); //*9.SKQuoteLib_RequestServerTime 要求報價主機傳送目前時間 [DllImport("SKQuoteLib.dll",CallingConvention = CallingConvention.StdCall)] public static extern intSKQuoteLib_RequestServerTime(); //* 10 .SKQuoteLib_GetServerTime 取得查詢的主機時間 [DllImport("SKQuoteLib.dll",CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern intSKQuoteLib_GetServerTime(ref CFormat05 pFormat05); // *** 定義TStock 結構 [StructLayout(LayoutKind.Sequential)] public struct TStock { public short Stockidx; public shortDecimal1; public short TypeNo; public byteMarketNo; [MarshalAs(UnmanagedType.ByValTStr,SizeConst = 20)] public stringStockNo; [MarshalAs(UnmanagedType.ByValTStr,SizeConst = 10)] public stringStockName; public int Open; public int High; public int Low; public int Close; // 單量 public int TickQty; // 昨收、參考價 public int Ref; // 買價 public int Bid; // 買量 public int Bc; // 賣價 public int Ask; // 賣量 public int Ac; // 買盤量 public int TBc; // 賣盤量 public int TAc; // 期貨未平倉 OI public int FutureOI; // 單量 public int TQty; public int YQty; // 漲停 public int Up; // 跌停 public int Down; } // *** 定義主機時間格式 CFormat05 [StructLayout(LayoutKind.Sequential)] public struct CFormat05 { public shortm_sHour; public shortm_sMinute; public shortm_sSecond; public int m_lTotal; } //*** 檢視 報價連線是否登入成功 public static voidOnConnectionBack(int Kind, int Code) { if (Code == 0) { Interaction.MsgBox("報價伺服器登入成功!!"); } else { Interaction.MsgBox(" 報價中斷!!"); } //*** 要求伺服器針對 pStockNos內的商品代號作報價通知的動作 int Status = 0; string ComIds =null; ComIds ="TX00"; Status =SKQuoteLib_RequestStocks(ref -1, ComIds); } //*** 接收報價的位置 public static voidOnQuoteCallBack(short Market, short Index) { // ERROR: Not supported in C#:OnErrorStatement int Status = 0; TStock Stock = newTStock(); //*** 要求報價 Status =SKQuoteLib_GetStockByIndex(Market, Index, ref Stock); //*** 接收台指期貨 TX00 報價的項目 Form1.txtTxBid.Text= Stock.Bid / 100; Form1.txtBuyQty.Text= Stock.Bc; Form1.txtTxAsk.Text= Stock.Ask / 100; Form1.txtSellQty.Text= Stock.Ac; Form1.txtTxClose.Text= Stock.Close / 100; Form1.txtTxTickQty.Text= Stock.TickQty; } //*** 程式開啟時 呼叫 API private voidForm1_Load(object sender, EventArgs e) { int Status = 0; Status = Status +SKQuoteLib_Initialize("身分證字號", "密碼"); Status = Status +SKQuoteLib_AttachConnectionCallBack(OnConnectionBack); Status = Status +SKQuoteLib_AttachQuoteCallBack(OnQuoteCallBack); Status = Status +SKQuoteLib_AttchServerTimeCallBack(CBFunc_ServerTime); Status =SKQuoteLib_EnterMonitor(); } //*** 結束報價按鈕 private voidbtnEnd_Click(object sender, EventArgs e) { SKQuoteLib_LeaveMonitor(); Interaction.MsgBox("結束報價,離開 !"); } //*** 接收SKQuoteLib_AttchServerTimeCallBack public static voidCBFunc_ServerTime(Int16 tHour, Int16 tMinute, Int16 tSecond, Int16 tTotal) { // Form1.Label7.Text= tHour // Form1.Label8.Text= tMinute // Form1.Label9.Text= tSecond // 留空() } //*** 5分鐘 秒呼叫一次SKQuoteLib_RequestServerTime() 伺服器時間 private voidTimer1_Tick(object sender, EventArgs e) { try { SKQuoteLib_RequestServerTime(); } catch (Exceptionex) { } } //*** 1 分鐘 秒呼叫一次SKQuoteLib_GetServerTime 查詢的主機時間時間 private voidTimer2_Tick(object sender, EventArgs e) { CFormat05 pFormat05= new CFormat05(); SKQuoteLib_GetServerTime(refpFormat05); } public Form1() { Load += Form1_Load; } } |