Skip to content

Instantly share code, notes, and snippets.

@chaohui0
Created November 14, 2012 08:00
Show Gist options
  • Save chaohui0/4070902 to your computer and use it in GitHub Desktop.
Save chaohui0/4070902 to your computer and use it in GitHub Desktop.
云同步哦
// hookDlg.cpp : implementation file
#include "stdafx.h"
#include "hook.h"
#include "hookDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CAboutDlg dialog used for App About
// ChookApp construction
bool WriteLogMessage(int i,CString szLogMessage)
{
MessageBox(NULL,szLogMessage,NULL,0);
return true;
}
bool IsKeyExists(HKEY hKeyRoot, LPTSTR lpSubKey)
{
LONG lResult = RegQueryValueEx(hKeyRoot, lpSubKey, 0, NULL, NULL, NULL);
if (lResult != ERROR_SUCCESS)
{
if (lResult == ERROR_FILE_NOT_FOUND)
return false;
}
return true;
}
BOOL ChookDlg::SetStartupWithReboot(BOOL bStartup)
{
HKEY hKey;
LONG lResult;
DWORD dwp = REG_OPENED_EXISTING_KEY;
WCHAR exePath[MAX_PATH] = L"";
//获得程序所在路径
if(GetModuleFileName(GetModuleHandle(0), exePath, MAX_PATH)==0)
return false;
//写在HKEY_CURRENT_USER而不是HKEY_LOCAL_MACHIE
//是因为在Vista、Win7上需要权限,这里没必要
lResult = RegCreateKeyEx(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\",0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS | KEY_SET_VALUE, NULL, &hKey, &dwp);
if (lResult!=ERROR_SUCCESS)
{
MessageBox(L"error:不能打开注册表");
return false;
}
if(IsKeyExists(hKey, L"KeyHelper"))
{
m_button.SetCheck(1);
}
else
{
m_button.SetCheck(0);
}
if (bStartup)//开机运行
{
if(!IsKeyExists(hKey, L"KeyHelper"))
{
lResult = RegSetValueEx(hKey, L"KeyHelper", 0, REG_SZ, (const BYTE*)exePath, wcslen(exePath)*sizeof(WCHAR));
if (lResult!=ERROR_SUCCESS)
{
m_button.SetCheck(0);
return false;
}
}
m_button.SetCheck(1);
}
else//不自动运行
{
if(IsKeyExists(hKey, L"KeyHelper"))
{
lResult = RegDeleteValue(hKey, L"KeyHelper");
if (lResult!=ERROR_SUCCESS)
{
//m_button.SetCheck(1);
return false;
}
m_button.SetCheck(0);
}
}
RegCloseKey(hKey);
return true;
}
void ChookDlg::ListenBroad(LPVOID p)
{
WSADATA wsaData; //指向WinSocket信息结构的指针
SOCKET sockListener;
SOCKADDR_IN sin,saClient;
char cRecvBuff[1024];
int nSize,nbSize;
int iAddrLen=sizeof(saClient);
if(WSAStartup(MAKEWORD( 1, 1 ), &wsaData )!=0)//进行WinSocket的初始化
{
//printf("Can´t initiates windows socket!Program stop.\n");//初始化失败返回-1
}
sockListener=socket(AF_INET, SOCK_DGRAM,0);
sin.sin_family = AF_INET;
sin.sin_port = htons(6111);//发送端使用的发送端口,可以根据需要更改
sin.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind( sockListener, (SOCKADDR FAR *)&sin, sizeof(sin))!=0)
{
printf("Can´t bind socket to local port!Program stop.\n");//初始化失败返回-1
}
while(1)
{
nSize = sizeof ( SOCKADDR_IN );
if((nbSize=recvfrom (sockListener,cRecvBuff,102-1,0,(SOCKADDR FAR *) &saClient,&nSize))==SOCKET_ERROR)
{
}
//cRecvBuff[nbSize] = 0;
::MessageBoxA(NULL,"get broadcast:",NULL,NULL);
printf("%s\n",cRecvBuff);
}
}
void ChookDlg::StartSocketServer(LPVOID port)
{
int m_port =(int)(LPVOID)port;
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return ;
}
if ( LOBYTE( wsaData.wVersion ) != 1 || HIBYTE( wsaData.wVersion ) != 1 )
{
WSACleanup( );
//MessageBox(L"error:不能打开服务器,程序是否重复打开");
return;
}
SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(m_port);
bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
listen(sockSrv,5);
SOCKADDR_IN addrClient;
int len=sizeof(SOCKADDR);
while(1)
{
SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);
//MessageBox(L"连接成功");
//Sleep(1000*3);
//for(int i=0;i<100;i++)
while(1)
{
//Sleep(300);
HWND topWnd = ::GetForegroundWindow();
//通过读取内存信息来判断是否为聊天模式
//获取窗口进程ID
DWORD processid;
::GetWindowThreadProcessId(topWnd,&processid);
//打开指定进程
HANDLE processH=::OpenProcess(PROCESS_ALL_ACCESS,false,processid);
//读指定进程 内存数据
LPCVOID pbase=(LPCVOID)0x6FAE8450; //数据基址 通过此数据来判断聊天模式,通过CE查找。
int chatStatus;
//LPVOID nbuffer=(LPVOID)&chatNum;
::ReadProcessMemory(processH,pbase,&chatStatus,4,NULL);
/*
if (chatStatus)
{
::PostMessage(topWnd,WM_KEYDOWN, 'O', 0L); //发送ESC按下的键盘消息
::PostMessage(topWnd,WM_KEYDOWN,13,0);
continue;
}*/
char recvBuf[50];
memset(recvBuf,0,50);
int rec = recv(sockConn,recvBuf,50,0);
if(rec <=0 )
{
closesocket(sockConn);
break;
}
//printf("%s\n",recvBuf);
//if(recvBuf[0] == 'A')
//{
for(int i=0;i<rec;i++)
{
if(recvBuf[i]<='z' && recvBuf[i]>='a')
{
recvBuf[i] +='A'-'a';
}
keybd_event(recvBuf[i],0,0,0);
keybd_event(recvBuf[i],0, KEYEVENTF_KEYUP,0);
}
// }
//::PostMessage(topWnd,WM_KEYDOWN, VK_CONTROL, 0L); //发送ESC按下的键盘消息
//::PostMessage(topWnd,WM_KEYDOWN, 'V', 0L); //发送ESC按下的键盘消息
//::PostMessage(topWnd,WM_KEYDOWN, 'O', 0L); //发送ESC按下的键盘消息
//::PostMessage(topWnd,WM_KEYDOWN,13,0);
/*
keybd_event(VK_CONTROL,0, 0,0);
keybd_event('V',0,0,0);
keybd_event(VK_CONTROL,0, KEYEVENTF_KEYUP,0);
keybd_event(13,0,0,0);
*/
//char sendBuf[50];
//sprintf(sendBuf,"Welcome %s to here!",inet_ntoa(addrClient.sin_addr));
//send(sockConn,sendBuf,strlen(sendBuf)+1,0);
//char recvBuf[50];
//recv(sockConn,recvBuf,50,0);
//printf("%s\n",recvBuf);
}
}
return ;
}
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// Dialog Data
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// ChookDlg dialog
ChookDlg::ChookDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(ChookDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void ChookDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_CHECK1, m_button);
}
BEGIN_MESSAGE_MAP(ChookDlg, CDialogEx)
ON_MESSAGE(WM_SHOWTASK,OnShowTask)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDOK, &ChookDlg::OnBnClickedOk)
ON_BN_CLICKED(IDC_BUTTON1, &ChookDlg::OnBnClickedButton1)
ON_WM_SIZE()
END_MESSAGE_MAP()
// ChookDlg message handlers
BOOL ChookDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
SetStartupWithReboot(true);
int port = 6000;
HANDLE hThread = CreateThread(NULL, 0,(LPTHREAD_START_ROUTINE)StartSocketServer, (LPVOID)port, 0, NULL);
HANDLE hTh = CreateThread(NULL, 0,(LPTHREAD_START_ROUTINE)ListenBroad, (LPVOID)port, 0, NULL);
//托盘显示
m_nid.cbSize = (DWORD)sizeof(NOTIFYICONDATA);
m_nid.hWnd = this->m_hWnd;
m_nid.uID = IDR_MAINFRAME;
m_nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP ;
m_nid.uCallbackMessage = WM_SHOWTASK; // 自定义的消息名称
m_nid.hIcon = LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));
_tcscpy(m_nid.szTip, L"手机键盘助手"); // 信息提示条为"服务器程序"
Shell_NotifyIcon(NIM_ADD, &m_nid); // 在托盘区添加图标
return TRUE; // return TRUE unless you set the focus to a control
}
void ChookDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void ChookDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR ChookDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void ChookDlg::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
if (m_button.GetCheck()) //设置自动运行
{
SetStartupWithReboot(true);
}
else //取消自动运行
{
SetStartupWithReboot(false);
}
OnOK();
}
void ChookDlg::OnBnClickedButton1()//启动
{
WSADATA wsaData; //指向WinSocket信息结构的指针
SOCKET sockListener;
SOCKADDR_IN sin,saUdpServ;
BOOL fBroadcast = TRUE;
char sendBuff[1024];
int nSize;
// int ncount=0;
if(WSAStartup(MAKEWORD( 1, 1 ), &wsaData )!=0)//进行WinSocket的初始化
{
printf("Can´t initiates windows socket!Program stop.\n");//初始化失败返回-1
}
sockListener=socket(PF_INET,SOCK_DGRAM,0);
setsockopt ( sockListener,SOL_SOCKET,SO_BROADCAST, (CHAR *)&fBroadcast,sizeof ( BOOL ));
sin.sin_family = AF_INET;
sin.sin_port = htons(6112);
sin.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind( sockListener, (SOCKADDR *)&sin, sizeof(sin))!=0)
{
printf("Can´t bind socket to local port!Program stop.\n");//初始化失败返回-1
}
saUdpServ.sin_family = AF_INET;
saUdpServ.sin_addr.s_addr = htonl ( INADDR_BROADCAST );
saUdpServ.sin_port = htons (6111);//发送用的端口,可以根据需要更改
nSize = sizeof ( SOCKADDR_IN );
sprintf(sendBuff,"BroadCast");
sendto ( sockListener,sendBuff, strlen (sendBuff),
0,
(SOCKADDR *) &saUdpServ,
sizeof ( SOCKADDR_IN ));
//::MessageBoxA(NULL,"send",NULL,NULL);
}
void ChookDlg::OnSize(UINT nType, int cx, int cy)
{
CDialogEx::OnSize(nType, cx, cy);
if(nType == SIZE_MINIMIZED)
{
ShowWindow(SW_HIDE); // 当最小化市,隐藏主窗口
}
// TODO: Add your message handler code here
}
BOOL ChookDlg::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
// 在托盘区删除图标
Shell_NotifyIcon(NIM_DELETE, &m_nid);
return CDialog::DestroyWindow();
}
LRESULT ChookDlg::OnShowTask(WPARAM wParam, LPARAM lParam)
{
if(wParam != IDR_MAINFRAME)
return 1;
switch(lParam)
{
case WM_RBUTTONUP: // 右键起来时弹出菜单
{
LPPOINT lpoint = new tagPOINT;
::GetCursorPos(lpoint); // 得到鼠标位置
CMenu menu;
menu.CreatePopupMenu(); // 声明一个弹出式菜单
menu.AppendMenu(MF_STRING, WM_DESTROY, L"关闭");
menu.TrackPopupMenu(TPM_LEFTALIGN, lpoint->x ,lpoint->y, this);
HMENU hmenu = menu.Detach();
menu.DestroyMenu();
delete lpoint;
}
break;
case WM_LBUTTONDBLCLK: // 双击左键的处理
{
this->ShowWindow(SW_SHOWNORMAL); // 显示主窗口
SetForegroundWindow();
}
break;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment