C#将输入发送到未对焦的窗口

时间:2016-05-05 17:30:55

标签: c# input hook

Upfront:我还是C#的新手。我已经完成了所有我可以做到的研究。这是我第一次在网上寻求帮助。

我正在尝试在C#中创建一个位于后台并接收扫描仪输入的程序。根据我迄今为止所研究的内容,扫描仪的作用类似于键盘楔形。这是作为控制台应用程序创建的。该程序应该将扫描的部件号与先前扫描的部件号进行比较。如果新零件编号与最后一个零件编号不同,则会发送一封电子邮件,说明前一个零件编号已完成并已输出,并且新零件编号正在开始生产。除2个问题外,它有效:

  1. 我尝试使用Hook来拦截输入。我的资源在哪里: https://blogs.msdn.microsoft.com/toub/2006/05/03/low-level-keyboard-hook-in-c/
  2. http://null-byte.wonderhowto.com/how-to/create-simple-hidden-console-keylogger-c-sharp-0132757/

    虽然我的代码在后台发送输入到控制台,但它每行打印一个字符以及我遇到的另一个问题...

    1. 将其输入到空白控制台,每行一个字符,或者在没有挂钩功能的情况下运行其余代码。
    2. 我已经在这个项目上工作了好几天而且我很茫然。任何帮助将不胜感激......

      using System;
      using System.Diagnostics;
      using System.Windows.Forms;
      using System.Configuration;
      using System.IO;
      using System.Net;
      using System.Net.Mail;
      using System.Runtime.InteropServices;
      using Outlook = Microsoft.Office.Interop.Outlook;
      using System.Windows.Input;
      
      namespace Test1
      {
      
          class Program
          {           
              private const int WH_KEYBOARD_LL = 13;
              private const int WM_KEYDOWN = 0x0100;
              public static LowLevelKeyboardProc _proc = HookCallBack;
              public static IntPtr _hookID = IntPtr.Zero;
      
      
      
          #region Import DLLs 
          [DllImport("user32.dll", CharSet = CharSet.Auto,SetLastError = true)]
          private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc Ipfn, IntPtr hMod, uint dwThreadId);
      
          [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
          [return: MarshalAs(UnmanagedType.Bool)]
          private static extern bool UnhookWindowsHookEx(IntPtr hhk);
      
          [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
          private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr IParam);
      
          [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
          private static extern IntPtr GetModuleHandle(string IpModuleName);
      
          #endregion
      
          #region Key Tracker Delegates/Functions
      
          public static IntPtr SetHook(LowLevelKeyboardProc proc)
              {
                  using (Process curProcess = Process.GetCurrentProcess())
                  using (ProcessModule curModule = curProcess.MainModule)
                  {
                      return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName),0);
                  }
              }
      
          public delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr IParam);   
      
              public static class PreviousDriver
              {
                  public static string previousDriver{get;set;}
              }
          #endregion
      
          #region  Email functionality
              public static void CreateMail()
              {
                  Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
      
                  Microsoft.Office.Interop.Outlook.MailItem mailItem = (Outlook.MailItem)app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
      
                  mailItem.Subject="Driver Update";
                  //mailItem.To = ; email here
                  mailItem.Body = "Your " + PreviousDriver.previousDriver + "is finished and is out for delivery.\n\n";
                  mailItem.Display(false);
                  mailItem.Send();
      
              }
      
          #endregion
      
              public static class CurrentDriverCount
              {
                  public static int currentDriverCount{get;set;}
              }
      
      
              #region Main
              public static void Main()
              {
      
              //var handle = GetConsoleWindow();
      
              //ShowWindow(handle, SW_HIDE);
      
              _hookID = SetHook(_proc);
      
              Application.Run();
      
              UnhookWindowsHookEx(_hookID);
      
              DriverStart();
      
              }
      
              #endregion
      
              #region DriverStart()
      
              public static void DriverStart()
              {
                  //string PreviousDriver = null;
      
      
                      Console.WriteLine("What is your driver?\n");
      
      
                      string CurrentDriver = Console.ReadLine(); //Scan driver
      
      
                      Console.WriteLine("Your current driver is " + CurrentDriver + "\n");
      
                      if(CurrentDriver == PreviousDriver.previousDriver)
                      {
                          //When it's the same driver
      
                          Console.WriteLine("Your previous driver was " + PreviousDriver.previousDriver + "\n");
      
                          CurrentDriverCount.currentDriverCount +=1;
      
                          Console.WriteLine("Your driver count is " + CurrentDriverCount.currentDriverCount + ".\n");
      
                          DriverStart();
                      } else {
      
                          //When the driver is different
      
                          Console.WriteLine("Your " + PreviousDriver.previousDriver + "is finished and is out for delivery.\n\n" +
                                            "Your "+ CurrentDriver + "are starting now!\n");
      
                          CreateMail();
      
                          PreviousDriver.previousDriver = CurrentDriver;
      
                          CurrentDriverCount.currentDriverCount = 0;
                          CurrentDriverCount.currentDriverCount +=1;
      
                          Console.WriteLine("Your driver count is " + CurrentDriverCount.currentDriverCount + ".\n");
      
                          DriverStart();
      
                      }
              }
      
              #endregion
      
      
      
          private static IntPtr HookCallBack(int nCode, IntPtr wParam, IntPtr IParam)
          {
              if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
              {
                  int vkCode = Marshal.ReadInt32(IParam);
                  Console.WriteLine((Keys)vkCode);
                  StreamWriter sw = new StreamWriter(Application.StartupPath,true);
                  sw.Write((Keys)vkCode);
                  sw.Close();
      
      
              }
              return CallNextHookEx(_hookID, nCode, wParam, IParam);
          }
      
          }
      }
      

0 个答案:

没有答案