2025-03-28 09:49:56 +08:00

246 lines
7.9 KiB
C#

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Management;
using System.Threading;
using System.Security.Permissions;
using System.Security;
using System.ComponentModel;
using mshtml;
using System.Reflection;
namespace SuperMap.RealEstate.SecurityDevice
{
[SecuritySafeCritical]
[DefaultEvent("DeviceStateChanged")]
public partial class SecurityDeviceControl : UserControl
{
IClientSecurityDevice _SecurityDevice = new UsbKeyLicenseDevice();
public SecurityDeviceControl()
{
try
{
InitializeComponent();
//注册消息
_SecurityDevice.MessageChanged += new ClientSecurityDeviceMessageHandler(SecurityDeviceControl_MessageChanged);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 设备初始化,并连接设备。
/// </summary>
/// <returns>是否找到设备</returns>
public bool Device_Connect()
{
bool _result = false;
try
{
SetValue(null);
_result=Device_Read();
RegisterNotification();
return _result;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
public event ClientSecurityDeviceStateHandler DeviceStateChanged;
protected virtual void OnDeviceStateChanged(ClientSecurityDeviceEventArgs e)
{
if (DeviceStateChanged != null)
{
DeviceStateChanged(this, e);
}
}
/// <summary>
/// 用户授权时间
/// </summary>
public string User_Expiry
{
get
{
if (string.IsNullOrEmpty(textBoxDate.Text))
return DateTime.Now.AddYears(1).ToString("yyyy-MM-dd HH:mm:ss");
else
{
DateTime _DateTime=new DateTime();
if (DateTime.TryParse(textBoxDate.Text, out _DateTime))
return DateTime.Parse(textBoxDate.Text).ToString("yyyy-MM-dd HH:mm:ss");
else
return DateTime.Now.AddYears(1).ToString("yyyy-MM-dd HH:mm:ss");
}
}
}
public string Message
{
get
{
return _SecurityDevice.Message;
}
set
{
_SecurityDevice.Message = value;
OnDeviceMessageChanged(new ClientSecurityDeviceMessageEventArgs(value));
}
}
private void OnDeviceMessageChanged(ClientSecurityDeviceMessageEventArgs e)
{
if (DeviceMessageChanged != null)
{
DeviceMessageChanged(this, e);
}
}
public event ClientSecurityDeviceMessageHandler DeviceMessageChanged ;
void SecurityDeviceControl_MessageChanged(object sender, ClientSecurityDeviceMessageEventArgs e)
{
if (DesignMode)
return;
OnDeviceMessageChanged(e);
}
public void SetValue(ClientSecurityDeviceObject value)
{
if (DesignMode)
return;
textBoxName.Text = (value==null) ?"": value.Name;
textBoxUser.Text = (value == null) ? "" : value.User;
textBoxKey.Text = (value == null) ? "" : value.Key;
textBoxDate.Text = (value == null) ? "" : value.Date;
textBoxUrl.Text = (value == null) ? "" : value.Url;
//_DeviceKey = (value == null) ? "" : (value.Name + "|" + value.User + "|" + value.Key + "|" + value.Date + "|" + value.Url);
}
private ClientSecurityDeviceObject GetValue()
{
if (DesignMode)
return null;
ClientSecurityDeviceObject _SecurityDeviceInfo = new ClientSecurityDeviceObject();
_SecurityDeviceInfo.Name = textBoxName.Text;
_SecurityDeviceInfo.User = textBoxUser.Text;
_SecurityDeviceInfo.Key = textBoxKey.Text;
_SecurityDeviceInfo.Date = textBoxDate.Text;
_SecurityDeviceInfo.Url = textBoxUrl.Text;
return _SecurityDeviceInfo;
}
//string _DeviceKey = string.Empty;
//public string DeviceKey
//{
// get
// {
// return _DeviceKey;
// }
//}
#region WndProc
/// <summary>
///
/// </summary>
/// <param name="m">要处理的 WindowsSystem.Windows.Forms.Message。</param>
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case SecurityDeviceHelper.WM_DEVICECHANGE:
ListenerNotification(ref m);
break;
}
base.WndProc(ref m);
}
#endregion
#region ListenerNotification
/// <summary>
/// 侦听设备通知
/// </summary>
/// <param name="msg">消息</param>
private void ListenerNotification(ref Message msg)
{
int wParam = (int)msg.WParam;
if (wParam == SecurityDeviceHelper.DBT_DEVICEARRIVAL)
{
int devType = Marshal.ReadInt32(msg.LParam, 4);
if (devType == SecurityDeviceHelper.DBT_DEVTYP_DEVICEINTERFACE)
{
Device_Read();
}
}
if (wParam ==SecurityDeviceHelper.DBT_DEVICEREMOVECOMPLETE)
{
Device_Remove();
}
}
#endregion
#region Device_Read
public bool Device_Read()
{
ClientSecurityDeviceObject _SecurityDeviceInfo;
if (_SecurityDevice.Open())
{
if (_SecurityDevice.Read(out _SecurityDeviceInfo))
{
SetValue(_SecurityDeviceInfo);
OnDeviceStateChanged(new ClientSecurityDeviceEventArgs(_SecurityDeviceInfo, ClientSecurityDeviceState.Insert));
}
_SecurityDevice.Close();
return true;
}
else
{
return false;
}
}
#endregion
#region Device_Remove
private void Device_Remove()
{
//Message = "数字证书被拔出。";
OnDeviceMessageChanged(new ClientSecurityDeviceMessageEventArgs("数字证书被拔出"));
ClientSecurityDeviceObject _SecurityDeviceInfo = GetValue();
OnDeviceStateChanged(new ClientSecurityDeviceEventArgs(_SecurityDeviceInfo, ClientSecurityDeviceState.Remove));
SetValue(null);
}
#endregion
#region RegisterNotification
/// <summary>
/// 注册设备通知
/// </summary>
void RegisterNotification()
{
SecurityDeviceHelper.DEV_BROADCAST_DEVICEINTERFACE dbi = new SecurityDeviceHelper.DEV_BROADCAST_DEVICEINTERFACE();
int size = Marshal.SizeOf(dbi);
dbi.dbcc_size = size;
dbi.dbcc_devicetype = SecurityDeviceHelper.DBT_DEVTYP_DEVICEINTERFACE;
dbi.dbcc_reserved = 0;
dbi.dbcc_classguid = SecurityDeviceHelper.GUID_DEVINTERFACE_USB_DEVICE;
dbi.dbcc_name = 0;
IntPtr buffer = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(dbi, buffer, true);
if (SecurityDeviceHelper.RegisterDeviceNotification(Handle, buffer, SecurityDeviceHelper.DEVICE_NOTIFY_WINDOW_HANDLE) == IntPtr.Zero)
{
MessageBox.Show(SecurityDeviceHelper.GetLastError().ToString());
}
}
#endregion
}
}