using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace skypehost {
class Skypehost {
private static volatile bool held = false;
private static volatile bool enabled = false;
private static bool m = false;
private static int speed = 16;
private static int nMHook;
private static int nKHook;
private readonly static Random random = new Random();
public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
private static HookProc mHookProc = MHookProc;
private static HookProc kHookProc = KHookProc;
static void Main(string[] args) {
AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnProcessExit);
try {
new Skypehost().init();
}catch(Exception exception) { Application.Exit(); }
}
static void OnProcessExit(object sender, EventArgs e) {
if (nMHook != 0) UnhookWindowsHookEx(nMHook);
if (nKHook != 0) UnhookWindowsHookEx(nKHook);
}
public static int MHookProc(int nCode, IntPtr wParam, IntPtr lParam) {
if (nCode >= 0) {
var hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
if ((IntPtr) 0x0201 == wParam && !held && hookStruct.flags == 0 && enabled) {
held = true;
m = false;
Task.Factory.StartNew(() => Tick());
} else if ((IntPtr) 0x0202 == wParam && hookStruct.flags == 0) held = false;
hookStruct.flags = 0;
Marshal.StructureToPtr(hookStruct, lParam, true);
}
return CallNextHookEx(nMHook, nCode, wParam, lParam);
}
public static int KHookProc(int nCode, IntPtr wParam, IntPtr lParam) {
if (nCode >= 0 && (IntPtr)0x0100 == wParam && ((Keys)Marshal.ReadInt32(lParam)) == Keys.End) {
held = false;
Application.Exit();
} else if (nCode >= 0 && (IntPtr)0x0100 == wParam && ((Keys)Marshal.ReadInt32(lParam)) == Keys.Home) enabled = !enabled;
else if (nCode >= 0 && (IntPtr)0x0100 == wParam && ((Keys)Marshal.ReadInt32(lParam)) == Keys.PageUp) speed++;
else if (nCode >= 0 && (IntPtr)0x0100 == wParam && ((Keys)Marshal.ReadInt32(lParam)) == Keys.PageDown) speed--;
return CallNextHookEx(nKHook, nCode, wParam, lParam);
}
private void init() {
speed += speed > 14 ? 4 : 3;
nMHook = SetWindowsHookEx(14, mHookProc, (IntPtr) 0, 0);
using (Process currentProcess = Process.GetCurrentProcess())
using (ProcessModule currentModule = currentProcess.MainModule) {
nKHook = SetWindowsHookEx(13, kHookProc, GetModuleHandle(currentModule.ModuleName), 0);
}
Application.Run();
}
private async static void Tick() {
INPUT input;
int result;
while (enabled && held) {
result = random.Next(100);
input = new INPUT() { Type = 0 };
if (result > 62) await Task.Delay((1000 / (speed * 2)) - random.Next(12));
else if (result > 28) await Task.Delay(1000 / (speed * 2) + (random.Next(1) == 0 ? random.Next(5) : (random.Next(6) - 5)));
else await Task.Delay((1000 / (speed * 2)) + random.Next(12));
if (m && held && enabled) {
input.Data.Mouse = new MOUSEINPUT() { X = Cursor.Position.X, Y = Cursor.Position.Y, MouseData = 0, Flags = 0x0002, Time = 0, ExtraInfo = (IntPtr) GetMessageExtraInfo() };
SendInput(1, new INPUT[] { input }, Marshal.SizeOf(typeof(INPUT)));
} else if (held && enabled) {
input.Data.Mouse = new MOUSEINPUT() { X = Cursor.Position.X, Y = Cursor.Position.Y, MouseData = 0, Flags = 0x0004, Time = 0, ExtraInfo = (IntPtr)GetMessageExtraInfo() };
SendInput(1, new INPUT[] { input }, Marshal.SizeOf(typeof(INPUT)));
}
m = !m;
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(int idHook);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("user32.dll", SetLastError = true)]
private static extern uint SendInput(uint numberOfInputs, INPUT[] inputs, int sizeOfInputStructure);
[DllImport("user32.dll", SetLastError = true)]
private static extern long GetMessageExtraInfo();
}
[StructLayout(LayoutKind.Sequential)]
internal struct POINT {
public int x;
public int y;
}
[StructLayout(LayoutKind.Sequential)]
internal struct MSLLHOOKSTRUCT {
public POINT pt;
public uint mData;
public uint flags;
public uint time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
internal struct INPUT {
public uint Type;
public MOUSEKEYBDHARDWAREINPUT Data;
}
[StructLayout(LayoutKind.Explicit)]
internal struct MOUSEKEYBDHARDWAREINPUT {
[FieldOffset(0)]
public HARDWAREINPUT Hardware;
[FieldOffset(0)]
public KEYBDINPUT Keyboard;
[FieldOffset(0)]
public MOUSEINPUT Mouse;
}
[StructLayout(LayoutKind.Sequential)]
internal struct HARDWAREINPUT {
public uint Msg;
public ushort ParamL;
public ushort ParamH;
}
[StructLayout(LayoutKind.Sequential)]
internal struct KEYBDINPUT {
public ushort Vk;
public ushort Scan;
public uint Flags;
public uint Time;
public IntPtr ExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
internal struct MOUSEINPUT {
public int X;
public int Y;
public uint MouseData;
public uint Flags;
public uint Time;
public IntPtr ExtraInfo;
}
}