C # Call the quote interface API
I have already passed the implementation of C # calling the quote interface API (see previous logs), but some people still haven't figured out how to implement it here. First, migrate the data structure to C # Based on the provided stockdrv. h # Code . 1. data structure definition // <summary>
// Summary of stockdrv. dll.
/// </Summary>
// [Structlayout (layoutkind. Sequential, charset = charset. ANSI, pack = 1)]
Public struct tagstock_structex
{
Public byte m_type; // stock's type, see Enum stocktype
[Financialas (unmanagedtype. byvalarray, sizeconst = 6)]
Public char [] m_code; // stock code
} [Structlayout (layoutkind. Sequential, charset = charset. ANSI, pack = 1)]
Public struct tagrcv_report_structex
{
Public uint16 m_cbsize; // structure size
Public int32 m_time; // transaction time
Public uint16 m_wmarket; // Stock Market Type
// M_szlabel and m_szname can be defined as [exploralas (unmanagedtype. byvalarray, sizeconst = 10)] according to your preferences.
Public char [] m_szlabel; // code. The size of the array ending with '\ 0' is stklabel_len, And it is Char [10] in the C ++ description.
[Financialas (unmanagedtype. byvalarray, sizeconst = 32)]
Public char [] m_szname; // name. The size of the array ending with '\ 0' is stkname_len, And it is Char [32] in the C ++ description.
/* It can also be defined as [financialas (unmanagedtype. byvaltstr, sizeconst = 10)]
Public String m_szlabel; // code. The size of the array ending with '\ 0' is stklabel_len, Which is Char [10] in the C ++ description.
[Financialas (unmanagedtype. byvaltstr, sizeconst = 32)]
Public String m_szname; // name. The size of the group ending with '\ 0' is stkname_len, Which is Char [32] */Public single m_flastclose in the C ++ description; // collect
Public single m_fopen; // available today
Public single m_fhigh; // maximum
Public single m_flow; // minimum
Public single m_fnewprice; // The latest
Public single m_fvolume; // transaction volume
Public single m_famount; // transaction amount [financialas (unmanagedtype. byvalarray, sizeconst = 3)]
Public single [] m_fbuyprice; // purchase price 1, 2, 3
[Financialas (unmanagedtype. byvalarray, sizeconst = 3)]
Public single [] m_fbuyvolume; // purchase volume 1, 2, 3
[Financialas (unmanagedtype. byvalarray, sizeconst = 3)]
Public single [] m_fsellprice; // sale price 1, 2, 3
[Financialas (unmanagedtype. byvalarray, sizeconst = 3)]
Public single [] m_fsellvolume; // sale volume 1, 2, 3 Public single m_fbuyprice4; // purchase price 4
Public single m_fbuyvolume4; // purchase volume 4
Public single m_fsellprice4; // sell price 4
Public single m_fsellvolume4; // sale volume 4 Public single m_fbuyprice5; // purchase price 5
Public single m_fbuyvolume5; // purchase 5
Public single m_fsellprice5; // sell price 5
Public single m_fsellvolume5; // definition of other items of the application quantity 5 ......}; [structlayout (layoutkind. Sequential, charset = charset. ANSI, pack = 1)]
Public struct tagrcv_file_headex
{
Public int m_dwattrib; // file subtype
Public int m_dwlen; // file length
Public int m_dwserialnoortime; // file serial number or time.
[Financialas (unmanagedtype. byvalarray, sizeconst = 260)]
Char [] m_szfilename; // file name or URL
} [Structlayout (layoutkind. Sequential, charset = charset. ANSI, pack = 1)]
Public struct tagrcv_data
{
Public int m_wdatatype; // file type
Public int m_npacketnum; // number of records, see note 1
[Financialas (unmanagedtype. struct)]
Public tagrcv_file_headex m_file; // file Interface
Public int m_bdisk; // whether the file is saved on the disk
Public intptr m_pdata;
}; One of the key points of the above definition is to describe pack. If not, some problems may occur during subsequent data processing. For example, the data structure defined in C # gets the structure length, which is inconsistent with the structure length of C ++. The reason is that there is no alignment processing or the two alignment processing methods are inconsistent. 2. Define public class stockdrv using APIs
{// Macro definition can be rewritten directly based on C ++
Public const int rcv_work_sendmsg = 4; // work type definition-window message mode
Public const int rcv_msg_stkdata = 0x8001; // specify the message public const int rcv_report = 0x3f001234; // stock quotes
Public const uint32 eke_head_tag = 0 xffffffff; // data header structure mark ...... // Interface function definition, according to the provision of stockdrv. h rewrite it as follows. // The Unicode DLL is provided. If it is not charset, change it to charset. ANSI: // explicit description to avoid unnecessary troubles [dllimport ("stockdrv. DLL ")]
Public static extern int stock_init (intptr nhwnd, int nmsg, int nworkmode );
[Dllimport (@ "stockdrv. dll", charset = charset. Unicode)]
Public static extern int getstockbycodeex (string strcode, int nmarket, ref tagrcv_report_structex srcvreort );
[Dllimport ("stockdrv. dll")]
Public static extern int setupreceiver (bool bsetup );
[Dllimport ("stockdrv. dll")]
Public static extern int gettotalnumber ();
[Dllimport ("stockdrv. dll")]
Public static extern int stock_quit (INT hwnd );
[Dllimport (@ "stockdrv. dll", charset = charset. Unicode)]
Public static extern int stock_init_nodialog (intptr hwnd, int MSG, int nworkmode, string szaddress, int nport, string szuser, string szpasswd, string szsecaddress, int nsecport, int nauth );
[Dllimport ("stockdrv. dll")]
Public static extern int isengineworking ();
[Dllimport ("stockdrv. dll")]
Public static extern int setautoreport (INT bautoreport );
[Dllimport ("stockdrv. dll")]
Public static extern int requeststockdata (INT ndatatype, tagstock_structex [] pstocks, int nsize, int nktype, int ndatacount );........ // The rest is written in this way. You can leave it empty if it is not used! Public stockdrv ()
{
//
// Todo: add the constructor logic here
//
}
}
/// Token} 3. when an API is called using a data sharing reference // data sharing reference, call the interface function stockdrv in C ++. stock_init (this. handle, stockdrv. rcv_msg_stkdata, stockdrv. rcv_work_sendmsg); stockdrv. setupreceiver (false); this method is generally not used. This method is provided to directly connect to the int lresult = stockdrv for compatibility purposes. stock_init_nodialog (this. handle, stockdrv. rcv_msg_stkdata, stockdrv. rcv_work_sendmsg ,.......); stockdrv. setupreceiver (false); next, request the data tagstock_structex [] pstocks = new tagstock_structex [1];
String S = "999999;
Pstocks [0]. m_code = S. tochararray (0, S. Length); pstocks [0]. m_type = 0x10;
Stockdrv. requeststockdata (stockdrv. rcv_report, pstocks, 1, 0, 0); 4. to receive data, you only need to reload wndproc. The Code is as follows: //////////////////////////////////////// /////////////////////
/// Reload the message processing process in the window where the virtual wndproc receives messages to intercept any messages
//////////////////////////////////////// ////////////////////
Protected override void wndproc (ref message m)
{
If (INT) M. MSG = stockdrv. rcv_msg_stkdata)
{
Tagrcv_data recvdata = (tagrcv_data) Marshal. ptrtostructure (M. lparam, typeof (tagrcv_data); If (INT) M. wparam = stockdrv. rcv_report) // market data
{
M_binitok = true;
Tagrcv_report_structex reprotdata;
Int reclength;
Reclength = marshal. sizeof (typeof (tagrcv_report_structex) // the actual length is required.
For (INT I = 0; I <recvdata. m_npacketnum; I ++)
{// If pack = 1 is not stated in the structure, it needs to be moved forward-2 // This is why many people feel that the difference is-2, as is VB6.0, VB. if pack = 1 is not described in. net, it is the same problem.
// Reprotdata = (tagrcv_report_structex) Marshal. ptrtostructure (New intptr (INT) recvdata. m_pdata + I * recLength-2), typeof (tagrcv_report_structex); // The structure needs to indicate pack = 1
Reprotdata = (tagrcv_report_structex) Marshal. ptrtostructure (New intptr (INT) recvdata. m_pdata + I * reclength), typeof (tagrcv_report_structex ));
// Data computing and analysis omitted
.........
}
}
}
Return;
}
Base. wndproc (ref m); // call the base class member function!
} Now, I have completed the instruction on how to call the API in C #. I believe you can develop a software suitable for you! If anything is wrong, please give me more advice!