B/S操作客户端硬件的解决方案

一般来说,客户端都是windows系统,所用的硬件驱动都是dll那么我们要想在客户端使用客户端硬件,就必须使用微软的com技术了,目前我找到两个具体的解决方案

一个是用MFC ActiveX,这里有个注意点,如果是win7系统,在MFC ActiveX项目属性中不能用静态库(lib),不然会导致无法注册

一个是用c#的windows窗体控件库

这里有几个注意点

一个,项目右键->属性->应用程序->程序集信息->勾选:使程序集com可见

二个,项目右键->属性->生成->勾选:为com互操作注册

还需要定义一个接口

    [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);
    }

让主类继承这个接口如下:

 

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;

namespace WindowsFormsControlLibrary3
{
    [Guid("A67A174D-0074-4806-99A1-1A75043A822C"), ProgId("MyFirstActiveX.UserControl1"), ComVisible(true)]
    public partial class UserControl1 : UserControl, IObjectSafety
    {
        public UserControl1()
        {
            InitializeComponent();
        }
        public void test()
        {
            MessageBox.Show("aa");
        }

        #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;
        }

        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;
        }

        #endregion
    }
}
这里用到的GUID是在菜单项 工具->生成GUID来生成的,以后js中调用的clsid也是这个ID

不管哪种办法,com组件在本地注册是必经之路,MFC ActiveX注册方法为:win7下把命令提示符界面路径引导至c:/windows/SysWOW64 下运行regsvr32 ocx全路径完成注册,

windows窗体控件库的注册方式为web注册

<object id="mytt" classid="clsid:A67A174D-0074-4806-99A1-1A75043A822C" codebase="c:/Debug/WindowsFormsControlLibrary3.dll"></object>

另外,js与com交互方式为


JS调用
function Open() 
{
var value_String = document.getElementsByName("Open_param1")[0].value;

var returnValue = CdmCtrl.Open(value_String);
document.getElementsByName("Open_result")[0].value = returnValue;

}

调用JS
<input type="button" name="button1" value="Open" onclick="Open()"/>

处理事件:
<SCRIPT type="text/javascript" FOR="CdmCtrl" EVENT="OnCashInStarted(p_lRetCode)" >
document.getElementsByName("log")[0].value += "fire OnCashInStarted(long p_lRetCode) event \n p_lRetCode = " + p_lRetCode + "\n\n";
</SCRIPT>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。