using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;
namespace ConsoleApplication1
{
    class Program
    {
        [DllImport("HYDati.dll")]
        public static extern IntPtr SendData(string auth_code, byte[] pData, int len, int type, int timeout, int pri, string extr_str);
        [DllImport("HYDati.dll")]
        public static extern IntPtr GetAnswer(string tid);

        static void Main(string[] args)
        {
            FileStream fsRead = new FileStream(@"1.jpg", FileMode.Open);
            
                int fsLen = (int)fsRead.Length;
                byte[] heByte = new byte[fsLen];
                int r = fsRead.Read(heByte, 0, heByte.Length);
                string myStr = System.Text.Encoding.ASCII.GetString(heByte);

              //密码串，图片数据，数据长度，题目类型，超时时间，固定值1，备注要求
                IntPtr tid = SendData("密码串", heByte, fsLen, 1000, 60, 1, "");
                //IntPtr接收返回值，然后转换为c#里string
                string tid_tmp = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(tid);
            //下面要加循环，如果获取结果返回为空，代表还没识别，延迟1秒继续调用。超时服务器会返回#开始头返回值，
            //最好自己在循环里加下判断超时，避免服务器无返回死循环。
                for (int i = 0; i < 60;i++ )
                {
                    IntPtr answer = GetAnswer(tid_tmp);
                    string answer_tmp = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(answer);
                    if(answer_tmp!=""){
                        break;
                    }
                    Thread.Sleep(1000);
                }

              
     
    }
    }
}