Utopia-Attendance-Software/MachineConnectivity.cs

91 lines
3.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace UtopiaAttService
{
public class MachineConnectivity
{
public const string HwDevCommDLL = @"HwDevComm.dll";
public const string HDCP_UtilsDLL = @"HDCP_Utils.dll";
[DllImport(HwDevCommDLL, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static extern int HwDev_Execute(string pDevInfoBuf, int nDevInfoLen, IntPtr pSendBuf, int nSendLen, ref IntPtr pRecvBuf, ref uint pRecvLen, CallBack pFuncTotalDone);
[DllImport(HwDevCommDLL, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static extern int HwDev_Finish(ref IntPtr pRecvBuf);
[DllImport("kernel32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static extern bool GlobalUnlock(IntPtr hMem);
[DllImport("kernel32.dll")]
public static extern IntPtr GlobalAlloc(int uFlags, int dwBytes);
[DllImport("kernel32.dll")]
public static extern IntPtr GlobalFree(IntPtr hMem);
[DllImport("kernel32.dll")]
public static extern IntPtr GlobalLock(IntPtr hMem);
public int ExecuteCommad(string pDevInfoBuf, int nDevInfoLen, string pSendBuf, int nSendLen, ref string pRecvBuf, ref uint pRecvLen, CallBack pFuncTotalDone)
{
byte[] bytes;
int num = 0;
IntPtr zero = IntPtr.Zero;
IntPtr ptr2 = IntPtr.Zero;
try
{
bytes = Encoding.Default.GetBytes(pSendBuf);
int length = bytes.Length;
zero = GlobalAlloc(0, length);
if (!(zero == IntPtr.Zero))
{
zero = GlobalLock(zero);
if (!(zero == IntPtr.Zero))
{
Marshal.Copy(bytes, 0, zero, length);
num = HwDev_Execute(pDevInfoBuf, pDevInfoBuf.Length, zero, length, ref ptr2, ref pRecvLen, pFuncTotalDone);
pRecvBuf = Marshal.PtrToStringAnsi(ptr2);
HwDev_Finish(ref ptr2);
}
else
{
return -1;
}
}
else
{
return -1;
}
}
catch (Exception exception1)
{
num = -1;
Console.Write(exception1.ToString());
throw new OperationCanceledException(exception1.Message);
}
finally
{
GlobalUnlock(zero);
GlobalFree(zero);
bytes = null;
}
return num;
}
public int BeCalled(uint nTotal, uint nDone) =>
0;
}
public delegate int CallBack(uint nTotal, uint nDone);
}