카테고리 없음2008. 10. 17. 17:17
[C#] 메시지 후킹하는 방법
조회 (144)
 
메시지 필터를 이용하여 F1, F2, F3 .. 키 입력을 후킹하기.
 
 
public class MessageFilter : System.Windows.Forms.IMessageFilter
{
    public bool PreFilterMessage(ref Message m)
    {
        switch(m.Msg)
        {
            case 256 : // WM_KEYDOWN     <==== windows message 를 C# 형태로 쓰고 싶었지만.. 딱히 만들어져있는게 없더군요 그래서
                            //                                    인터넷에서 windows.h 파일을 검색해서 찾았습니다. WM_KETDOWN = 256 번이더군요.
                System.Console.WirteLine(m.LParam.ToInt32());        // 이걸로 F1 부터 여러가지 키를 차례데로 눌러본 값들이
                if( m.LParam.ToInt32() == 3866625 ) // F1 Key            // <===  이 값들입니다. 3866625 가 F1 이군요.. 이런식으로 Key 값을 찾아서
                {                                                                            // 계속 추가하면 키보드 후킹.. 그다지 어려운건 아닐거 같군요 ^^;;
                    System.Console.WriteLine("F1 Press");
                    return true;
                }
                else if( m.LParam.ToInt32() == 3932161 )    // F2 Key
                {
                    System.Console.WriteLine("F2 Press");
                    return true;
                }
                else if( m.LParam.ToInt32() == 3997697 )    // F3 Key
                {
                    System.Console.WriteLine("F3 Press");
                    return true;
                }
                break;
        }
        return false;
    }
}
 
//
//     만들어 놓은 MessageFilter 는 반드시 AddMessageFilter 메서드를 통해 추가해줘야 합니다.
//    static void Main 함수는 기본 Form 에 포함되어 있습니다.  :) 아시죠?
 
static void Main()
{
    Application.AddMessageFilter(new MessageFilter());
    Application.Run(new JapExamples());
}
 
 
아래 있는 것은 windows.h 파일에 정의된 windos message 들입니다. windows.h 파일은 visual studio 안에 INCLUDE_ROOT 에 있습니다.
어딘지는 아시죠?
 
 

Posted by penguindori
2008/C#2008. 10. 14. 16:51

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Threading;

namespace Login
{
    public partial class LoginForm : Form
    {
        private int index = 12;
        private int index2 = 12;

        bool isSleep = false;

        Thread t1;

        System.Windows.Forms.Timer timer1;
        System.Windows.Forms.Timer timer2;


        public LoginForm()
        {
            InitializeComponent();       
           
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            axWebBrowser1.Navigate(Application.StartupPath + @"\0_main.swf");

            timer1 = new System.Windows.Forms.Timer();
            timer2 = new System.Windows.Forms.Timer();

            timer1.Tick += new EventHandler(timer1_Tick);
            timer2.Tick += new EventHandler(timer2_Tick);

            timer1.Interval = 10;
            timer2.Interval = 10;

            timer1.Enabled =true;
            timer2.Enabled =true;   
        }

        void timer2_Tick(object sender, EventArgs e)
        {
            index2++;

            if (index2 == 60) index2 = 12;

            button2.Location = new Point(index2, 183);


        }

        void timer1_Tick(object sender, EventArgs e)
        {
            // isSleep(bool)변수가 false일때만 실행

            if (!isSleep)
            {

                index++;

 

                // index가 30이면 isSleep을 true로 바꾸고

                // 쓰레드를 실행합니다.

                if (index == 30)
                {

                    isSleep = true;

                    t1 = new Thread(new ThreadStart(TimerStart));

                    t1.Start();

                }

 

                // index가 60이면 12로(앞으로) 되돌리고 애니메이션~!

                if (index == 60) index = 12;

                button2.Location = new Point(index, 12);

            }


        }

        private void TimerStart()
        {

            // 간단하게 0.5초를 쉬고 isSleep변수값을 바꿉니다.

            Thread.Sleep(500);

            isSleep = false;

      }
      
    }
}

Posted by penguindori