실제 윈도우 메시지 보내기
1. 목적: A라는 대상 Utility의 기능들을 B라는 다른 Utility에서
Window Message(SendMessage)를 통해 제어하는 방법과 실험을 실시.
2. 구현 - 간략 설명.
A.EXT : SPY++로 Caption Title을 찾아 FindWindow로 핸들을 찾아와,
SendMessage로 해당 핸들에 메세지 및 W/L param를 전송한다.
3. 구현 - 코드
A.EXE :
A.h 선언
afx_msg long OnReceiveExecuteMsg(WPARAM Wparam, LPARAM LParam);
A.cpp
message 선언
ON_MESSAGE(WM_USER_MSG,OnReceiveExecuteMsg)
// WM_USER_MSG 0x401
long A::OnReceiveExecuteMsg(WPARAM WParam, LPARAM LParam)
{
switch(WParam)
{
case 0: // B Utility에서 W Param으로 보내오는 데이터가 0인 경우 실행.
OnManStart();
break;
case 1:
OnManClear();
break;
default:
break;
}
return TRUE;
}
//////////////////////////////////////////////////////////////////////////////////////////
B.exe : C#으로 작성된 Utility
namespace RemoteExecute
{
public partial class Form1 : Form
{
// #define 정의
const int WM_USER = 0x0400;
const int WM_USER_MSG = WM_USER + 1;
int hwnd = 0;
public class AnotherWindowCtrl
{
[DllImport("user32.dll")]
public static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern int SendMessage(int hwnd, int wMsg, int wParam, int lParam);
}
public Form1()
{
InitializeComponent();
}
private void btnStart_Click(object sender, EventArgs e)
{
if(hwnd==0) // 핸들 없으면 찾아서... WindowName은 Spy로 찾아서 기록.
hwnd = AnotherWindowCtrl.FindWindow(null, "System name");
AnotherWindowCtrl.SendMessage(hwnd,WM_USER_MSG, 0, 0);
}
private void btnClear_Click(object sender, EventArgs e)
{
if (hwnd == 0) hwnd = AnotherWindowCtrl.FindWindow(null, "System name");
AnotherWindowCtrl.SendMessage(hwnd, WM_USER_MSG, 1, 0);
}
}
}