using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

public static class CIDR
{
    [UnmanagedFunctionPointer(CallingConvention.StdCall,  CharSet = CharSet.Unicode)]
    public delegate void Callback(Int32 line, [MarshalAs(UnmanagedType.LPWStr)] string msg);

    [DllImport("POS_CIDR.dll", CharSet = CharSet.Unicode)]
    public static extern Int32 CIDR_RegisterEvent(MulticastDelegate funcPtr, Int32 mask);

    [DllImport("POS_CIDR.dll", CharSet = CharSet.Unicode)]
    public static extern Int32 CIDR_SaveLine(Int32 line, [MarshalAs(UnmanagedType.LPWStr)] string fileName, [MarshalAs(UnmanagedType.LPWStr)] string param);

    [DllImport("POS_CIDR.dll", CharSet = CharSet.Unicode)]
    public static extern Int32 CIDR_PlayLine(Int32 line);

    [DllImport("POS_CIDR.dll", CharSet = CharSet.Unicode)]
    private static extern IntPtr CIDR_Info();

    public static string CIDR_InfoStr()
    {
        string result = "";
        try
        {
            result = Marshal.PtrToStringUni(CIDR.CIDR_Info());
        }
        catch
        { }
        return result;
    }

    public static bool CIDR_GetInfo(out string dll_info, out int lines)
    {
        dll_info = "";
        lines = 0;
        try
        {
            string s = CIDR_InfoStr();
            string[] parts = s.Split(new string[]{ "\r\n" }, StringSplitOptions.None);
            if ((parts.Length>3) && parts[0].Contains("POS") && parts[2].Contains(":"))
            {
                dll_info = parts[0];
                int p = parts[2].IndexOf(":");
                lines = Convert.ToInt16(parts[2].Substring(p + 1).Trim());
                return true;
            }
        }
        catch
        {
        }
        return false;
    }


}
