在Console App

时间:2015-12-04 15:24:43

标签: c# wcf windows-8 msmq biometrics

所以我需要制作一个使用外部指纹识别器进行用户识别的Metro GUI应用程序。

我到目前为止在C ++中制作一个DLL,所以我可以在C#中调用一个名为CaptureSample()的方法来引用该DLL。 CaptureSample()方法为我提供了一个表示扫描指纹的字节数组(来自指纹识别器的灰度图像)。到目前为止这么好。这样做的方法是使用Microsoft生物识别框架访问阅读器,等待阅读器检测到手指放在阅读器上,然后将数据发送回扫描指纹。

这一切都很棒。但是有一个问题:我必须运行我作为管理员的任何应用程序来使用框架。这也是我不能从Web服务运行框架的原因,因为Webservice作为网络用户或类似用户运行,使库在尝试使用时拒绝任何访问。所以我必须制作一个可以运行它的额外控制台程序。

现在这里的事情变得多毛了。为了使指纹识别器与Windows Metro GUI App一起使用,您必须以管理员身份运行该应用程序。这是不可能的,因为所有Metro GUI应用程序都在沙箱中运行。我必须创建一个外部控制台应用程序,为应用程序调用DLL功能,然后将结果发送回应用程序。

为了使事情变得更加复杂,我发现.NET的Windows Phone子集没有MSMQ本来不错。因此,我创建了Metro应用程序需要调用的本地WCF服务,然后WCF服务通过MSMQ调用控制台程序,并在从控制台应用程序接收信息时将信息发送回Metro GUI App。

到目前为止一切顺利。理论上。我在此过程中遇到了问题,因为控制台应用程序确实运行并准备好在被询问时进行扫描。但当我扫描我的手指没有任何反应。控制台需要专注才能工作,而且由于应用程序像Kiosk一样运行,因此根本无法离开Metro GUI应用程序,除非进行维护。

我已经查看了在C#应用程序之外检测键盘输入的各种解决方案,但我不认为这适用于指纹识别器,或者如果有,我不知道我是怎么做的。 ; d那样做。有什么建议吗?

为了获得良好的安慰,我添加了工作流程图表,以便更容易理解。 (它是P5100 Zvetcobiometrics指纹识别器,如果有人好奇的话):

enter image description here

1 个答案:

答案 0 :(得分:2)

因此,我在Windows窗体中遇到了类似这样的问题,但不完全是您所描述的问题。

我知道我的解决方案不是一个预先编码的简易解决方案,但我觉得经过一些小修改,几年前我克服的障碍可能会对你有用。如果您认为我的解决方案可能会对您有所帮助,我可以向您发送任何遗失的内容。

我创建了一个使用条形码扫描仪的库存管理系统,我决定通过在一些C ++类中编组而不需要使用输入控件(如文本框)来实现从设备处理输入的功能。我需要应用程序能够处理条形码输入并做出决策,而无需任何额外的用户交互或要求。只需扫描即可。立即使用键盘/ HID设备的输入是我觉得这个解决方案适合您的问题的原因。

在测试我写的应用程序时,我能够进入全屏游戏,并且仍然能够在Windows窗体库存应用程序中使用条形码扫描程序。同样的功能应该在控制台环境中同样有效,因为控制台应用程序在后台时不会停止。您甚至可以将其设置为NT AUTHORITY,并防止它在作为服务运行时显示到桌面,并且它仍然应该突然消失。

我所做的是使用Win32API来反映设备,将设备与通过应用程序指定的设备(用户选择)相匹配,并基本上为该特定设备建立了一个监听器。

您可以使用控制台应用程序通过将控制台应用程序作为本地服务帐户运行来触发指纹传感器,或者以编程方式获取必要的授权(这将允许您以提升的权限运行它而不会让UAC阻塞您的方式)然后在你的城域应用程序中使用它来在设备发送时从设备读取输入。

以下是一些代码文件,用于了解我所描述的内容以及针对我的条形码扫描仪功能进行了修改。

如果您想看到任何遗失的部分,请再次私下与我联系。

PS:从技术上讲,这可以用作拦截密钥和排序的黑客攻击,因此我将自行决定使用免责声明,并且我不对任何愚蠢的人对此代码执行任何操作负责。< / p>

BarcodeScannerListenerInteropHelper.h:

#include <winuser.h>

BEGIN_INTEROP_NAMESPACE

using namespace System;
using namespace System::Collections::Generic;
using namespace HFSLIB::Barcode;
using namespace HFSLIB::Barcode::Interop;
using namespace HFSLIB::Barcode::Infrastructure::BarcodeScannerListener;

/// <summary>
/// Provides some helper methods that help the BarcodeScannerListener use native
/// Windows APIs without resorting to P/Invoking from C#.
/// </summary>
public ref class BarcodeScannerListenerInteropHelper
{
    public:
        /// <summary>
        /// Returns a dictionary of barcode device handles to information about
        /// the device.
        /// </summary>
        /// <param name="hardwareIds">The enumerable of hardware IDs to filter by.</param>
        /// <returns>The device handle-to-information mapping of the filtered hardware IDs.</returns>
        Dictionary<IntPtr, BarcodeScannerDeviceInfo^>^ InitializeBarcodeScannerDeviceHandles(
            IEnumerable<String^>^ hardwareIds);

        /// <summary>
        /// Registers ourselves to listen to raw input from keyboard-like devices.
        /// </summary>
        /// <param name="hwnd">the handle of the form that will receive the raw
        /// input messages</param>
        /// <exception cref="InvalidOperationException">if the call to register with the
        /// raw input API fails for some reason</exception>
        void HookRawInput(IntPtr hwnd);

        /// <summary>
        /// Gets information from a WM_INPUT message.
        /// </summary>
        /// <param name="rawInputHeader">The LParam from the WM_INPUT message.</param>
        /// <param name="deviceHandle">[Out] The device handle that the message came from.</param>
        /// <param name="handled">[Out] True if the message represents a keystroke from that device.</param>
        /// <param name="buffer">[Out] If handled is true, this contains the characters that the keystroke represents.</param>
        void GetRawInputInfo(
            IntPtr rawInputHeader, 
            IntPtr% deviceHandle, 
            bool% handled,
            String^% buffer);
    private:
        /// <summary>
        /// Converts a native raw input type into our version.
        /// </summary>
        /// <param name="rawInputType">The raw input type.</param>
        /// <returns>Our version of the type.</returns>
        static BarcodeScannerDeviceType GetBarcodeScannerDeviceType(DWORD rawInputType);
};

END_INTEROP_NAMESPACE

BarcodeScannerListenerInteropHelper.cpp:

#include "BarcodeScannerListenerInteropHelper.h"
using namespace System::ComponentModel;

BEGIN_INTEROP_NAMESPACE

/// <summary>
/// Gets information from a WM_INPUT message.
/// </summary>
/// <param name="rawInputHeader">The LParam from the WM_INPUT message.</param>
/// <param name="deviceHandle">[Out] The device handle that the message came from.</param>
/// <param name="handled">[Out] True if the message represents a keystroke from that device.</param>
/// <param name="buffer">[Out] If handled is true, this contains the characters that the keystroke represents.</param>
void BarcodeScannerListenerInteropHelper::GetRawInputInfo(
    IntPtr rawInputHeader,
    IntPtr% deviceHandle, 
    bool% handled,
    String^% buffer)
{
    UINT cbSize;
    HRAWINPUT hRawInput;

    hRawInput = (HRAWINPUT)rawInputHeader.ToPointer();
    if (GetRawInputData(hRawInput, RID_INPUT, NULL, &cbSize, sizeof(RAWINPUTHEADER)) == 0)
    {
        RAWINPUT* raw;

        raw = (RAWINPUT*)malloc(cbSize);

        if (GetRawInputData(hRawInput, RID_INPUT, raw, &cbSize, sizeof(RAWINPUTHEADER)) == cbSize)
        {
            deviceHandle = IntPtr(raw->header.hDevice);
            handled = raw->header.dwType == RIM_TYPEKEYBOARD &&
                raw->data.keyboard.Message == WM_KEYDOWN;

            if (handled)
            {
                BYTE state[256];

                // Force the keyboard status cache to update
                GetKeyState(0);

                // Note: GetKeyboardState only returns valid state when
                // the application has focus -- this is why we weren't
                // getting shift keys when the application was not focused
                if (GetKeyboardState(state))
                {
                    WCHAR unmanagedBuffer[64];

                    if (ToUnicode(raw->data.keyboard.VKey,
                            raw->data.keyboard.MakeCode,
                            state,
                            unmanagedBuffer,
                            64,
                            0) > 0)
                    {
                        buffer = gcnew String(unmanagedBuffer);
                    }
                }
            }
        }

        free(raw);
    }
}

/// <summary>
/// Registers ourselves to listen to raw input from keyboard-like devices.
/// </summary>
/// <param name="hwnd">the handle of the form that will receive the raw
/// input messages</param>
/// <exception cref="InvalidOperationException">if the call to register with the
/// raw input API fails for some reason</exception>
void BarcodeScannerListenerInteropHelper::HookRawInput(IntPtr hwnd)
{
    RAWINPUTDEVICE rid[1];

    rid[0].dwFlags = 0;
    rid[0].hwndTarget = (HWND)hwnd.ToPointer();
    rid[0].usUsage = 0x06;     // Keyboard Usage ID
    rid[0].usUsagePage = 0x01; // USB HID Generic Desktop Page

    if (!RegisterRawInputDevices(rid, 1, sizeof(RAWINPUTDEVICE)))
    {
        InvalidOperationException^ e;

        e = gcnew InvalidOperationException(
            "The barcode scanner listener could not register for raw input devices.",
            gcnew Win32Exception());
        throw e;
    }
}

/// <summary>
/// Returns a dictionary of barcode device handles to information about
/// the device.
/// </summary>
Dictionary<IntPtr, BarcodeScannerDeviceInfo^>^ BarcodeScannerListenerInteropHelper::InitializeBarcodeScannerDeviceHandles(IEnumerable<String^>^ hardwareIds)
{
    Dictionary<IntPtr, BarcodeScannerDeviceInfo^>^ devices;
    UINT uiNumDevices;
    UINT cbSize;

    devices = gcnew Dictionary<IntPtr, BarcodeScannerDeviceInfo^>();
    uiNumDevices = 0;
    cbSize = sizeof(RAWINPUTDEVICELIST);

    if (GetRawInputDeviceList(NULL, &uiNumDevices, cbSize) != -1)
    {
        PRAWINPUTDEVICELIST pRawInputDeviceList;

        if (pRawInputDeviceList = (PRAWINPUTDEVICELIST)malloc(cbSize * uiNumDevices))
        {
            if (GetRawInputDeviceList(pRawInputDeviceList, &uiNumDevices, cbSize) != -1)
            {
                for (UINT i = 0; i < uiNumDevices; ++i)
                {
                    UINT pcbSize;
                    RAWINPUTDEVICELIST rid;

                    rid = pRawInputDeviceList[i];

                    if (GetRawInputDeviceInfo(rid.hDevice, RIDI_DEVICENAME, NULL, &pcbSize) >= 0 &&
                        pcbSize > 0)
                    {
                        WCHAR* deviceName;

                        deviceName = (WCHAR*)malloc(sizeof(WCHAR) * (pcbSize + 1));
                        if (GetRawInputDeviceInfo(rid.hDevice, RIDI_DEVICENAME, deviceName, &pcbSize) >= 0)
                        {
                            bool add;
                            IntPtr deviceHandle;
                            BarcodeScannerDeviceInfo^ info;
                            String^ managedDeviceName;

                            add = false;
                            deviceHandle = IntPtr(rid.hDevice);
                            managedDeviceName = gcnew String(deviceName);

                            for each (String^ hardwareId in hardwareIds)
                            {
                                if (managedDeviceName->IndexOf(hardwareId, StringComparison::OrdinalIgnoreCase) >= 0)
                                {
                                    add = true;
                                    break;
                                }
                            }

                            if (add)
                            {
                                info = gcnew BarcodeScannerDeviceInfo(
                                    managedDeviceName,
                                    BarcodeScannerListenerInteropHelper::GetBarcodeScannerDeviceType(rid.dwType),
                                    deviceHandle);

                                devices->Add(deviceHandle, info);
                            }
                        }

                        free(deviceName);
                    }
                }
            }

            free(pRawInputDeviceList);
        }
    }

    return devices;
}

/// <summary>
/// Converts a native raw input type into our version.
/// </summary>
/// <param name="rawInputType">The raw input type.</param>
/// <returns>Our version of the type.</returns>
BarcodeScannerDeviceType BarcodeScannerListenerInteropHelper::GetBarcodeScannerDeviceType(DWORD rawInputType)
{
    BarcodeScannerDeviceType type;

    switch (rawInputType)
    {
        case RIM_TYPEHID:
            type = BarcodeScannerDeviceType::HumanInterfaceDevice;
            break;
        case RIM_TYPEKEYBOARD:
            type = BarcodeScannerDeviceType::Keyboard;
            break;
        default:
            type = BarcodeScannerDeviceType::Unknown;
            break;
    }

    return type;
}

END_INTEROP_NAMESPACE

BarcodeScannerListener.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Windows.Forms;
using HFSLIB.Barcode.Infrastructure.BarcodeScannerListener;
using HFSLIB.Barcode.Interop;

namespace HFSLIB.Barcode
{
/// <summary>
/// This class uses Windows's native Raw Input API to listen for input from
/// a certain set of barcode scanners and devices. This way, the application
/// can receive input from a barcode scanner without the user having to
/// worry about whether or not a certain text field has focus, which was a
/// big problem
/// </summary>
public class BarcodeScannerListener : NativeWindow
{
    /// <summary>
    /// A mapping of device handles to information about the barcode scanner
    /// devices.
    /// </summary>
    private Dictionary<IntPtr, BarcodeScannerDeviceInfo> devices;

    /// <summary>
    /// The WM_KEYDOWN filter.
    /// </summary>
    private BarcodeScannerKeyDownMessageFilter filter;

    /// <summary>
    /// The barcode currently being read.
    /// </summary>
    private StringBuilder keystrokeBuffer;

    /// <summary>
    /// The interop helper.
    /// </summary>
    private BarcodeScannerListenerInteropHelper interopHelper =
        new BarcodeScannerListenerInteropHelper();

    /// <summary>
    /// Event fired when a barcode is scanned.
    /// </summary>
    public event EventHandler BarcodeScanned;

    /// <summary>
    /// Attaches the listener to the given form.
    /// </summary>
    /// <param name="form">The form to attach to.</param>
    public void Attach(Form form)
    {
        IntPtr hwnd;

        if (form == null)
        {
            throw new ArgumentNullException("form");
        }

        hwnd = form.Handle;

        this.keystrokeBuffer = new StringBuilder();

        this.InitializeBarcodeScannerDeviceHandles();
        this.interopHelper.HookRawInput(hwnd);
        this.HookHandleEvents(form);

        this.AssignHandle(hwnd);

        this.filter = new BarcodeScannerKeyDownMessageFilter();
        Application.AddMessageFilter(this.filter);
    }

    /// <summary>
    /// Hook into the form's WndProc message. We listen for WM_INPUT and do
    /// special processing on the raw data.
    /// </summary>
    /// <param name="m">the message</param>
    [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
    protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            case NativeMethods.WM_INPUT:
                if (this.ProcessRawInputMessage(m.LParam))
                {
                    this.filter.FilterNext = true;
                }

                break;
        }

        base.WndProc(ref m);
    }

    /// <summary>
    /// Fires the barcode scanned event.
    /// </summary>
    /// <param name="deviceInfo">information about the device that generated
    /// the barcode</param>
    private void FireBarcodeScanned(BarcodeScannerDeviceInfo deviceInfo)
    {
        string barcode;
        EventHandler handler;

        barcode = this.keystrokeBuffer.ToString();

        if (barcode != null && barcode.Length > 0)
        {
            handler = this.BarcodeScanned;

            this.keystrokeBuffer = new StringBuilder();

            if (handler != null)
            {
                handler(this, new BarcodeScannedEventArgs(barcode, deviceInfo));
            }
        }
    }

    /// <summary>
    /// Hooks into the form's HandleCreated and HandleDestoryed events
    /// to ensure that we start and stop listening at appropriate times.
    /// </summary>
    /// <param name="form">the form to listen to</param>
    private void HookHandleEvents(Form form)
    {
        form.HandleCreated += this.OnHandleCreated;
        form.HandleDestroyed += this.OnHandleDestroyed;
    }

    /// <summary>
    /// Initializes the barcode scanner device handles.
    /// </summary>
    private void InitializeBarcodeScannerDeviceHandles()
    {
        BarcodeScannerListenerConfigurationSection config;
        BarcodeScannerListenerConfigurationElementCollection hardwareIdsConfig;
        IEnumerable<string> hardwareIds;

        config = BarcodeScannerListenerConfigurationSection.GetConfiguration();
        hardwareIdsConfig = config.HardwareIds;
        hardwareIds = from hardwareIdConfig in hardwareIdsConfig.Cast<BarcodeScannerListenerConfigurationElement>()
                      select hardwareIdConfig.Id;

        this.devices = this.interopHelper.InitializeBarcodeScannerDeviceHandles(hardwareIds);
    }

    /// <summary>
    /// When the form's handle is created, let's hook into it so we can see
    /// the WM_INPUT event.
    /// </summary>
    /// <param name="sender">the form whose handle was created</param>
    /// <param name="e">the event arguments</param>
    private void OnHandleCreated(object sender, EventArgs e)
    {
        this.AssignHandle(((Form)sender).Handle);
    }

    /// <summary>
    /// When the form's handle is destroyed, let's unhook from it so we stop
    /// listening and allow the OS to free up its resources.
    /// </summary>
    /// <param name="sender">the form whose handle was destroyed</param>
    /// <param name="e">the event arguments</param>
    private void OnHandleDestroyed(object sender, EventArgs e)
    {
        this.ReleaseHandle();
    }

    /// <summary>
    /// Process the given WM_INPUT message.
    /// </summary>
    /// <param name="rawInputHeader">the rawInputHeader of the message</param>
    /// <returns>whether or not the keystroke was handled</returns>
    private bool ProcessRawInputMessage(IntPtr rawInputHeader)
    {
        BarcodeScannerDeviceInfo deviceInfo;
        bool handled;
        bool keystroke;
        string localBuffer;
        IntPtr rawInputDeviceHandle;

        handled = false;
        keystroke = false;
        localBuffer = string.Empty;
        rawInputDeviceHandle = IntPtr.Zero;

        this.interopHelper.GetRawInputInfo(
            rawInputHeader,
            ref rawInputDeviceHandle,
            ref keystroke,
            ref localBuffer);

        if (this.devices.TryGetValue(rawInputDeviceHandle, out deviceInfo) && keystroke)
        {
            handled = true;

            if (localBuffer.Length == 1 && localBuffer[0] == 0xA)
            {
                this.FireBarcodeScanned(deviceInfo);
            }
            else
            {
                this.keystrokeBuffer.Append(localBuffer);
            }
        }

        return handled;
    }
}
}