110,530
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Newtonsoft.Json;
using BL.Core;
using BL.Core.WF;
using BL.Device.RfReader;
using System.Reflection;
using System.IO;
using System.Diagnostics;
using Microsoft.Win32;
namespace BL.Device.RfReader.ActiveX
{
[Guid("2079D3B4-6C55-4F47-876F-DBB874A4F68A")]
public partial class RfidActiveX : UserControl, IObjectSafety
{
private IRFReader reader;
private List<TagInfo> listTag; //记录读取到的标签集合
private ExceptionService exService;
private string lastError; //存储错误消息
/// <summary>
/// 构造方法
/// </summary>
public RfidActiveX()
{
InitializeComponent();
}
/// <summary>
/// 控件数据初始化
/// </summary>
/// <returns></returns>
public int Initial()
{
listTag = new List<TagInfo>();
reader = RfReaderFactory.GetReader(ReaderType.R_Cethik_Smart);
if (reader == null)
{
lastError = "不支持的读写器类型!";
return -1;
}
reader.TagsReport += reader_TagsReport;
return 1;
}
/// <summary>
/// 连接读写器
/// </summary>
/// <returns></returns>
public int Connect()
{
if (reader == null)
{
lastError = "请先选择读写器!";
return -1;
}
int result = exService.ThreadExceptionCatch(() =>
{
reader.Connect();
btnConnect.Enabled = false;
btnStart.Enabled = true;
ConvertEnable();
});
if (result == -1) return -1;
return 1;
}
/// <summary>
/// 断开连接
/// </summary>
/// <returns></returns>
public int DisConnect()
{
if (reader == null) return -1;
int result = exService.ThreadExceptionCatch(() =>
{
if (btnStop.Enabled) btnStop.PerformClick();
reader.DisConnect();
btnConnect.Enabled = true;
btnStart.Enabled = false;
ConvertEnable();
});
if (result == -1) return -1;
return 1;
}
/// <summary>
/// 外部调用检测ActiveX是否安装成功
/// </summary>
/// <returns></returns>
public string Test()
{
return "success";
}
/// <summary>
/// 将组件安装为ActiveX
/// </summary>
/// <param name="isUnInstall"></param>
public void InstallActiveX(bool isUnInstall = false)
{
//注册COM
string version = typeof(System.Linq.Queryable).Assembly.ImageRuntimeVersion;
//找32位.Net类库
string pathDotNet32 = @"C:\windows\Microsoft.NET\Framework\" + version;
if (Directory.Exists(pathDotNet32))
{
//寻找COM注册程序RegAsm
string regAsm = Path.Combine(pathDotNet32, "RegAsm.exe");
string comFile = Assembly.GetExecutingAssembly().Location;
if (File.Exists(regAsm) && File.Exists(comFile))
{
//注册COM
if (isUnInstall) comFile += " /u";
Process.Start(new ProcessStartInfo(regAsm, comFile) { WindowStyle = ProcessWindowStyle.Hidden });
MsgBox.Show("执行成功!");
}
}
}
/// <summary>
/// 检查ActiveX是否安装成功
/// </summary>
public bool IsActiveXInstalled()
{
Type type = Type.GetTypeFromCLSID(new Guid("2079D3B4-6C55-4F47-876F-DBB874A4F68A"));
if (type == null) return false;
object ax = null;
try
{
ax = Activator.CreateInstance(type);
}
catch(Exception)
{
return false;
}
MethodInfo mi = type.GetMethod("Test");
object result = mi.Invoke(ax, null);
if (result.ToString() == "success") return true;
return false;
}
/// <summary>
/// 补充注册项(没有此注册项的话,通过本地RegAsm注册的ActiveX无法在IE中使用)
/// </summary>
/// <param name="type"></param>
[ComRegisterFunctionAttribute]
public static void RegisterFunction(Type type)
{
//注册[HKEY_CLASSES_ROOT\CLSID\{6AEBC61A-EFAA-4676-8174-D431EA938DEE}\InprocServer32]下的CodeBase
string keyPath = @"CLSID\{" + type.GUID.ToString().ToUpper() + @"}\InprocServer32";
var keyItem = Registry.ClassesRoot.CreateSubKey(keyPath);
keyItem.SetValue("CodeBase", Assembly.GetExecutingAssembly().Location);
//注册不同版本的CodeBase
string[] subKeys = keyItem.GetSubKeyNames();
foreach (var item in subKeys)
{
string subKey = keyPath + @"\" + item;
var subKeyItem = Registry.ClassesRoot.CreateSubKey(subKey);
subKeyItem.SetValue("CodeBase", Assembly.GetExecutingAssembly().Location);
}
}
/// <summary>
/// 取消注册项
/// </summary>
/// <param name="type"></param>
[ComUnregisterFunctionAttribute]
public static void UnregisterFunction(Type type)
{
//删除注册[HKEY_CLASSES_ROOT\CLSID\{6AEBC61A-EFAA-4676-8174-D431EA938DEE}\InprocServer32]下的CodeBase
string keyPath = @"CLSID\{" + type.GUID.ToString().ToUpper() + @"}\InprocServer32";
var keyItem = Registry.ClassesRoot.CreateSubKey(keyPath);
keyItem.DeleteValue("CodeBase");
//删除不同注册版本的CodeBase
string[] subKeys = keyItem.GetSubKeyNames();
foreach (var item in subKeys)
{
string subKey = keyPath + @"\" + item;
var subKeyItem = Registry.ClassesRoot.CreateSubKey(subKey);
subKeyItem.DeleteValue("CodeBase");
}
}
#region IObjectSafety 成员
private const string _IID_IDispatch = "{00020400-0000-0000-C000-000000000046}";
private const string _IID_IDispatchEx = "{a6ef9860-c720-11d0-9337-00a0c90dcaa9}";
private const string _IID_IPersistStorage = "{0000010A-0000-0000-C000-000000000046}";
private const string _IID_IPersistStream = "{00000109-0000-0000-C000-000000000046}";
private const string _IID_IPersistPropertyBag = "{37D84F60-42CB-11CE-8135-00AA004BB851}";
private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001;
private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002;
private const int S_OK = 0;
private const int E_FAIL = unchecked((int)0x80004005);
private const int E_NOINTERFACE = unchecked((int)0x80004002);
private bool _fSafeForScripting = true;
private bool _fSafeForInitializing = true;
public int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions, ref int pdwEnabledOptions)
{
//int Rslt = E_FAIL;
//string strGUID = riid.ToString("B");
//pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA;
//switch (strGUID)
//{
// case _IID_IDispatch:
// case _IID_IDispatchEx:
// Rslt = S_OK;
// pdwEnabledOptions = 0;
// if (_fSafeForScripting == true)
// pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER;
// break;
// case _IID_IPersistStorage:
// case _IID_IPersistStream:
// case _IID_IPersistPropertyBag:
// Rslt = S_OK;
// pdwEnabledOptions = 0;
// if (_fSafeForInitializing == true)
// pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA;
// break;
// default:
// Rslt = E_NOINTERFACE;
// break;
//}
//return Rslt;
pdwSupportedOptions = 1;
pdwEnabledOptions = 2;
return S_OK;
}
public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions)
{
//int Rslt = E_FAIL;
//string strGUID = riid.ToString("B");
//switch (strGUID)
//{
// case _IID_IDispatch:
// case _IID_IDispatchEx:
// if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_CALLER) && (_fSafeForScripting == true))
// Rslt = S_OK;
// break;
// case _IID_IPersistStorage:
// case _IID_IPersistStream:
// case _IID_IPersistPropertyBag:
// if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_DATA) && (_fSafeForInitializing == true))
// Rslt = S_OK;
// break;
// default:
// Rslt = E_NOINTERFACE;
// break;
//}
//return Rslt;
return S_OK;
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace BL.Device.RfReader.ActiveX
{
[ComImport, GuidAttribute("CB5BDC81-93C1-11CF-8F20-00805F2CD064")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IObjectSafety
{
[PreserveSig]
int GetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] ref int pdwSupportedOptions, [MarshalAs(UnmanagedType.U4)] ref int pdwEnabledOptions);
[PreserveSig()]
int SetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] int dwOptionSetMask, [MarshalAs(UnmanagedType.U4)] int dwEnabledOptions);
}
}
/// <summary>
/// 补充注册项(没有此注册项的话,通过本地RegAsm注册的ActiveX无法在IE中使用)
/// </summary>
/// <param name="type"></param>
[ComRegisterFunctionAttribute]
public static void RegisterFunction(Type type)
{
//注册[HKEY_CLASSES_ROOT\CLSID\{6AEBC61A-EFAA-4676-8174-D431EA938DEE}\InprocServer32]下的CodeBase
string keyPath = @"CLSID\{" + type.GUID.ToString().ToUpper() + @"}\InprocServer32";
var keyItem = Registry.ClassesRoot.CreateSubKey(keyPath);
keyItem.SetValue("CodeBase", Assembly.GetExecutingAssembly().Location);
//注册不同版本的CodeBase
string[] subKeys = keyItem.GetSubKeyNames();
foreach (var item in subKeys)
{
string subKey = keyPath + @"\" + item;
var subKeyItem = Registry.ClassesRoot.CreateSubKey(subKey);
subKeyItem.SetValue("CodeBase", Assembly.GetExecutingAssembly().Location);
}
}
/// <summary>
/// 取消注册项
/// </summary>
/// <param name="type"></param>
[ComUnregisterFunctionAttribute]
public static void UnregisterFunction(Type type)
{
//删除注册[HKEY_CLASSES_ROOT\CLSID\{6AEBC61A-EFAA-4676-8174-D431EA938DEE}\InprocServer32]下的CodeBase
string keyPath = @"CLSID\{" + type.GUID.ToString().ToUpper() + @"}\InprocServer32";
var keyItem = Registry.ClassesRoot.CreateSubKey(keyPath);
keyItem.DeleteValue("CodeBase");
//删除不同注册版本的CodeBase
string[] subKeys = keyItem.GetSubKeyNames();
foreach (var item in subKeys)
{
string subKey = keyPath + @"\" + item;
var subKeyItem = Registry.ClassesRoot.CreateSubKey(subKey);
subKeyItem.DeleteValue("CodeBase");
}
}