免责声明:内容仅供学习参考,请合法利用知识,禁止进行违法犯罪活动!
如果看不懂、不知道现在做的什么,那就跟着做完看效果
内容参考于:易道云信息技术研究院VIP课
上一个内容:30.数据搜索功能
码云地址(master 分支):https://gitee.com/dye_your_fingers/titan
码云版本号:6cd3ae3d3bf803e1e316b26b20b25660a7a45590
代码下载地址,在 titan 目录下,文件名为:titan-其它消息的实现与使用优化.zip
链接:https://pan.baidu.com/s/1W-JpUcGOWbSJmMdmtMzYZg
提取码:q9n5
--来自百度网盘超级会员V4的分享
HOOK引擎,文件名为:黑兔sdk升级版.zip
链接:https://pan.baidu.com/s/1IB-Zs6hi3yU8LC2f-8hIEw
提取码:78h8
--来自百度网盘超级会员V4的分享
以30.数据搜索功能 它的代码为基础进行修改
现在只能搞发送和接收
![]()
还有一个其它的没搞,如下图
![]()
给主页窗口添加关闭消息
输入框修改了属性:如下图
效果图:
得到Connect的数据
![]()
数据分析窗口全屏状态自动靠左
![]()
还有数据窗口搜索高亮没法展示,需要打开程序搜索一下,搜索的使用参考上一个内容(30.数据搜索功能)
CAnly.h文件的修改:使用工具修改,现在的配置信息如下图
DataAnlyDlg.h文件的修改:新加 OnClose函数(关闭消息处理函数),删除了确定按钮以及它的点击事件处理函数
// DataAnlyDlg.h: 头文件
//#pragma once
#include "CSet.h"
#include "MSG_DEC.h"
#include "CWndData.h"
#include "CWndSearch.h"typedef struct COPY_DATA {time_t ltime; // 时间unsigned MsgType; // 数据类型unsigned len; // 数据长度char buff[0x2000]; // 数据
}*PCOPYDATA;// CDataAnlyDlg 对话框
class CDataAnlyDlg : public CDialogEx
{
// 构造
public:CDataAnlyDlg(CWnd* pParent = nullptr); // 标准构造函数// 对话框数据
#ifdef AFX_DESIGN_TIMEenum { IDD = IDD_DATAANLY_DIALOG };
#endifprotected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持// 实现
protected:HICON m_hIcon;// 生成的消息映射函数virtual BOOL OnInitDialog();afx_msg void OnSysCommand(UINT nID, LPARAM lParam);afx_msg void OnPaint();afx_msg HCURSOR OnQueryDragIcon();DECLARE_MESSAGE_MAP()
public:CListCtrl lstView;afx_msg BOOL OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct);BOOL Pause;afx_msg void OnBnClickedCheck1();afx_msg void OnBnClickedButton1();CSet wndSet;CWndData wndData;CWndSearch wndSearch;afx_msg void OnBnClickedButton4();void CDataAnlyDlg::LoadConfig(CString inifile);afx_msg void OnNMCustomdrawList1(NMHDR* pNMHDR, LRESULT* pResult);CString LogName; // 数据包写到硬盘上的文件名CString mainlog; // 数据包保存目录CString productName; // 当前titlevoid Savelog(const wchar_t* logs, const wchar_t* file);afx_msg void OnBnClickedButton5();afx_msg void OnNMClickList1(NMHDR* pNMHDR, LRESULT* pResult);afx_msg void OnMove(int x, int y);afx_msg void OnBnClickedButton3();void Search(CString& txt, SEARCH_TYPE type, BOOL Hex);CString TxtBuff(unsigned char* buff, unsigned len);afx_msg void OnClose();
};
DataAnlyDlg.cpp文件的修改:删除 OnBnClickedOk函数(确定按钮点击事件处理函数),修改了 OnCopyData函数(进程通信)、OnMove函数(窗口移动、大小变化事件)、Search函数(数据搜索),新加 OnClose函数(关闭事件的消息处理函数,也就是上面添加的那个关闭消息)
// DataAnlyDlg.cpp: 实现文件
//#include "pch.h"
#include "framework.h"
#include "DataAnly.h"
#include "DataAnlyDlg.h"
#include "afxdialogex.h"
#include "extern_all.h"
#include <fstream> // 引入io流,用于写文件#ifdef _DEBUG
#define new DEBUG_NEW
#endif// 用于应用程序“关于”菜单项的 CAboutDlg 对话框class CAboutDlg : public CDialogEx
{
public:CAboutDlg();// 对话框数据
#ifdef AFX_DESIGN_TIMEenum { IDD = IDD_ABOUTBOX };
#endifprotected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持// 实现
protected:DECLARE_MESSAGE_MAP()
};CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{CDialogEx::DoDataExchange(pDX);
}BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()// CDataAnlyDlg 对话框CDataAnlyDlg::CDataAnlyDlg(CWnd* pParent /*=nullptr*/): CDialogEx(IDD_DATAANLY_DIALOG, pParent), Pause(FALSE)
{m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CDataAnlyDlg::DoDataExchange(CDataExchange* pDX)
{CDialogEx::DoDataExchange(pDX);DDX_Control(pDX, IDC_LIST1, lstView);DDX_Check(pDX, IDC_CHECK1, Pause);
}BEGIN_MESSAGE_MAP(CDataAnlyDlg, CDialogEx)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_WM_COPYDATA()ON_BN_CLICKED(IDC_CHECK1, &CDataAnlyDlg::OnBnClickedCheck1)ON_BN_CLICKED(IDC_BUTTON1, &CDataAnlyDlg::OnBnClickedButton1)ON_BN_CLICKED(IDC_BUTTON4, &CDataAnlyDlg::OnBnClickedButton4)ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST1, &CDataAnlyDlg::OnNMCustomdrawList1)ON_BN_CLICKED(IDC_BUTTON5, &CDataAnlyDlg::OnBnClickedButton5)ON_NOTIFY(NM_CLICK, IDC_LIST1, &CDataAnlyDlg::OnNMClickList1)ON_WM_MOVE()ON_BN_CLICKED(IDC_BUTTON3, &CDataAnlyDlg::OnBnClickedButton3)ON_WM_CLOSE()
END_MESSAGE_MAP()// CDataAnlyDlg 消息处理程序BOOL CDataAnlyDlg::OnInitDialog()
{CDialogEx::OnInitDialog();// 将“关于...”菜单项添加到系统菜单中。// IDM_ABOUTBOX 必须在系统命令范围内。ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != nullptr){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);}}// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动// 执行此操作SetIcon(m_hIcon, TRUE); // 设置大图标SetIcon(m_hIcon, FALSE); // 设置小图标// ShowWindow(SW_MAXIMIZE); // 设置最大号启动// TODO: 在此添加额外的初始化代码SetListView(&lstView);wchar_t buff[0xFF];/*获取当前程序名,这个名字是全路径,不只有程序名字(就是.exe文件的名字)它还可以获取别人的名字详细看微软的MSDN里的说明(百度搜索 “GetModuleFileName msdn” 关键字,就可以搜索出微软MSDN的说明)*/ GetModuleFileName(0, buff, sizeof(buff));int i;for (i = 0xFF - 1; buff[i] != L'\\'; i--);buff[++i] = 0;AppPath.Format(L"%s", buff);// AfxMessageBox(AppPath);ConfigPath = AppPath + L"Config\\";DataPath = AppPath + L"Datas\\";DefConfigIni = ConfigPath + L"titan.ini";SetIni = AppPath + L"Set.ini";/*读取一个Set.ini文件Set.ini文件目前存放了,当程序启动时默认加载什么配置文件的数据*/GetPrivateProfileString(L"main", L"defPro", L"", buff, 0xFF, SetIni);// 获取配置文件,也就是现在的titan.ini这个文件DefConfigIni = AppPath + L"Config\\" + buff + L".ini";LoadConfig(DefConfigIni);/*lstView.InsertColumn(0, L"消息类型", 0, 70);lstView.InsertColumn(1, L"数据类型", 0, 70);lstView.InsertColumn(2, L"内容", 0, 700);lstView.InsertColumn(3, L"长度", 0, 50);lstView.InsertColumn(4, L"时间", 0, 130);这几行代码意思是设置表头*/lstView.InsertColumn(0, L"消息类型", 0, 70);lstView.InsertColumn(1, L"数据类型", 0, 70);lstView.InsertColumn(2, L"内容", 0, 700);lstView.InsertColumn(3, L"长度", 0, 50);lstView.InsertColumn(4, L"时间", 0, 130);struct tm newtiem {};time_t t;time(&t);localtime_s(&newtiem, &t); // 获取时间LogName.Format(L"%.4d-%.2d-%.2d %.2d-%.2d-%.2d.log", newtiem.tm_year + 1900, newtiem.tm_mon+1, newtiem.tm_mday, newtiem.tm_hour, newtiem.tm_min, newtiem.tm_sec);wndSet.Create(IDD_SET);wndData.Create(IDD_DIALOG1);// wndSearch.Create(IDD_SEARCH); // wndSearch要成模态窗口所以不要创建return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}void CDataAnlyDlg::OnSysCommand(UINT nID, LPARAM lParam)
{if ((nID & 0xFFF0) == IDM_ABOUTBOX){CAboutDlg dlgAbout;dlgAbout.DoModal();}else{CDialogEx::OnSysCommand(nID, lParam);}
}// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。void CDataAnlyDlg::OnPaint()
{if (IsIconic()){CPaintDC dc(this); // 用于绘制的设备上下文SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);// 使图标在工作区矩形中居中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;// 绘制图标dc.DrawIcon(x, y, m_hIcon);}else{CDialogEx::OnPaint();}
}//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CDataAnlyDlg::OnQueryDragIcon()
{return static_cast<HCURSOR>(m_hIcon);
}BOOL CDataAnlyDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* p)
{UpdateData(true);if (Pause)return CDialogEx::OnCopyData(pWnd, p);PCOPYDATA pdata = (PCOPYDATA)p->lpData;struct tm newtiem {};localtime_s(&newtiem, &pdata->ltime); // 获取时间int type = p->dwData;if (type < MAX_MSG) {CString _result;CString _time;_time.Format(L"%.2d-%.2d-%.2d %.2d_%.2d_%.2d", newtiem.tm_year + 1900, newtiem.tm_mon + 1, newtiem.tm_mday, newtiem.tm_hour, newtiem.tm_min, newtiem.tm_sec);// 0数据包 1 char 2 wchar_tCString _tmp;CStringA _tmpA;unsigned char* buff;switch (_MsgPtr[type].Type){case 0: // 数据包// 转换为buffbuff = (unsigned char*)pdata->buff;for (int i = 0; i < pdata->len; i++) {_tmp.Format(L"%.2X ", buff[i]);_result = _result + _tmp;}break;case 1:_tmpA = pdata->buff;_result = _tmpA;break;case 2:_result = (wchar_t*)pdata->buff;break;default:break;}if (_MsgPtr[type].Type == 0) { }else {}CString wlen;PDATA _data = _MsgPtr[type].GetData(pdata->MsgType);bool Show = true;CString txtDetails;if (_data){Show = _data->IsUi;txtDetails.Format(L"%s(%X)", _data->Name, pdata->MsgType);}else {txtDetails.Format(L"%X", pdata->MsgType);}if (Show) {lstView.InsertItem(0, _MsgPtr[type].txtName);lstView.SetItemData(0, (DWORD_PTR)_data);lstView.SetItemText(0, 1, txtDetails);lstView.SetItemText(0, 2, _result);lstView.SetItemText(0, 4, _time);wlen.Format(L"%d", pdata->len);lstView.SetItemText(0, 3, wlen);}CString _log;CString _logfile;_log.Format(L"[%s][%s][%d][%s][%s]", _MsgPtr[type].txtName, _time.GetBuffer(), pdata->len, txtDetails, _result);_logfile = DataPath + productName + L"\\" + LogName;if(_MsgPtr[type].IsTxt) Savelog(_log.GetBuffer(), _logfile.GetBuffer()); // 写到文件if ((_data)&&(_data->IsTxt)) { // 所有这里存放所有类型的数据包_logfile.Format(L"%s%s\\%s.log", DataPath, productName, _MsgPtr[type].txtDefine);Savelog(_log.GetBuffer(), _logfile.GetBuffer());}if ((_data) && (_data->IsTxtAlone)) { // 单独输出,也就是相同类型的数据包放到一个文件中_logfile.Format(L"%s%s\\%s\\%X.log", DataPath, productName, _MsgPtr[type].txtDefine, _data->Id);Savelog(_log.GetBuffer(), _logfile.GetBuffer());}}else {AfxMessageBox(L"接收到无法识别的数据,需要重新根据配置生成头文件!");} return CDialogEx::OnCopyData(pWnd, p);
}void CDataAnlyDlg::OnBnClickedCheck1()
{
}void CDataAnlyDlg::OnBnClickedButton1()
{lstView.DeleteAllItems();
}void CDataAnlyDlg::OnBnClickedButton4()
{wndSet.ShowWindow(TRUE);wndSet.LoadConfig(DefConfigIni, productName);
}void CDataAnlyDlg::LoadConfig(CString inifile) {/*从配置文件中读取一个数据,意思是从main下的count读取一个数字int类型的数字GetPrivateProfileInt用来获取.ini文件里的内容,最后的Int表示读取的数据类型Int就表示读取int类型,String表示读取String类型,也就是一个字符串GetPrivateProfileString(L"main", L"title", L"", buff, 0xFF, inifile);里的L""是默认值GetPrivateProfileInt(L"main", L"count", 0, inifile);里的0是默认值默认值的意思,如果读取不到数据就把默认值返回给我们*/int icount = GetPrivateProfileInt(L"main", L"count", 0, inifile);if (_MsgPtr) delete[] _MsgPtr;if (icount < 1) return;MAX_MSG = icount;_MsgPtr = new MSG_DEC[icount];lstView.DeleteAllItems();wchar_t buff[0xFF];GetPrivateProfileString(L"main", L"title", L"", buff, 0xFF, inifile);this->SetWindowText(buff);GetPrivateProfileString(L"main", L"name", L"", buff, 0xFF, inifile);productName = buff;WritePrivateProfileString(L"main", L"defPro", productName, SetIni);/*CString App;CString AppData;CString _Name;它是用来初始化字符串的,利用 Foramt 函数把从.ini文件里获取的数据全部转成字符串*/CString App;CString AppData;CString _Name;for (int i = 0; i < icount; i++){App.Format(L"Message_%d", i);GetPrivateProfileString(App, L"name", L"", _MsgPtr[i].txtName, 0xFF, inifile);_Name.Format(L"%s", _MsgPtr[i].txtName);GetPrivateProfileString(App, L"define", L"", _MsgPtr[i].txtDefine, 0xFF, inifile);GetPrivateProfileString(App, L"type", L"", buff, 0xFF, inifile);_MsgPtr[i].Type = TxtToType(buff);GetPrivateProfileString(App, L"istxt", L"", buff, 0xFF, inifile);_MsgPtr[i].IsTxt = TxtToBool(buff);;int lcount = GetPrivateProfileInt(App, L"count", 0, inifile);_MsgPtr[i].Datas = new DATA_DEC[lcount];_MsgPtr[i].DataCount = lcount;for (int j = 0; j < lcount; j++){AppData.Format(L"%s_%d", _Name.GetBuffer(), j);PDATA dataDec = &(_MsgPtr[i].Datas[j]);dataDec->Msg = &(_MsgPtr[i]);GetPrivateProfileString(AppData, L"id", L"0", buff, 0xFF, inifile);dataDec->Id = _wtoi(buff);GetPrivateProfileString(AppData, L"name", L"0", dataDec->Name, 0xFF, inifile);GetPrivateProfileString(AppData, L"backcolor", L"0", buff, 0xFF, inifile);dataDec->BackColor = TxtToColor(buff);GetPrivateProfileString(AppData, L"fontcolor", L"0", buff, 0xFF, inifile);dataDec->FontColor = TxtToColor(buff);GetPrivateProfileString(AppData, L"isui", L"0", buff, 0xFF, inifile);dataDec->IsUi = TxtToBool(buff);GetPrivateProfileString(AppData, L"istxt", L"0", buff, 0xFF, inifile);dataDec->IsTxt = TxtToBool(buff);GetPrivateProfileString(AppData, L"istxtalone", L"0", buff, 0xFF, inifile);dataDec->IsTxtAlone = TxtToBool(buff);}}/*要做一个数据包保存到硬盘功能mainlog = DataPath + productName; 是用来获取保存到什么文件的也就是一个文件的绝对路径,这个文件用来保存数据包*/mainlog = DataPath + productName;
}void CDataAnlyDlg::OnNMCustomdrawList1(NMHDR* pNMHDR, LRESULT* pResult)
{LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);// TODO: 在此添加控件通知处理程序代码*pResult = 0;NMLVCUSTOMDRAW* lp = (NMLVCUSTOMDRAW*)pNMHDR;NMCUSTOMDRAW& ncmd = lp->nmcd;switch (ncmd.dwDrawStage){case CDDS_PREPAINT: {*pResult = CDRF_NOTIFYITEMDRAW;break;}case CDDS_ITEMPREPAINT: {PDATA _data = (PDATA)lstView.GetItemData(ncmd.dwItemSpec);if (_data) {lp->clrTextBk = _data->BackColor;lp->clrText = _data->FontColor;*pResult = CDRF_DODEFAULT;}break;}}}void CDataAnlyDlg::Savelog(const wchar_t* logs, const wchar_t* file)
{// 设置追加写入文件std::wofstream ofs(file, std::ios::app);// 设置语言集,防止写中文出错ofs.imbue(std::locale("chs")); if (ofs.bad())return;ofs << logs << std::endl;ofs.close();
}// 加载按钮
void CDataAnlyDlg::OnBnClickedButton5()
{LoadConfig(DefConfigIni);
}void CDataAnlyDlg::OnNMClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);// TODO: 在此添加控件通知处理程序代码*pResult = 0;NM_LISTVIEW* p = (NM_LISTVIEW*)pNMHDR;if (p->iItem != -1) {wndData.ShowWindow(TRUE);wndData.m_Edit.SetWindowTextW(lstView.GetItemText(p->iItem,2));}
}void CDataAnlyDlg::OnMove(int x, int y)
{CDialogEx::OnMove(x, y);CRect rect_main, rect_data;GetWindowRect(&rect_main);wndData.GetWindowRect(&rect_data);/*这个计算的原因是,GetWindowRect函数获取的left、right、top、bottom是坐标不是大小,rect_data.right - rect_data.left;这样一减就能得到窗口的宽度(这里的宽度是差值,left到right的差值)然后由于它们都是坐标,所以计算的结果也是坐标,就是宽度所在的坐标然后再由left坐标到right坐标绘制窗口,就能保证窗口宽度不会变化了*/unsigned w = rect_data.right - rect_data.left;if (rect_main.left < 100) {rect_data.left = rect_main.right - w;}else {rect_data.left = rect_main.left - w + 15;}rect_data.right = rect_data.left + w;rect_data.top = rect_main.top;rect_data.bottom = rect_main.bottom;wndData.MoveWindow(&rect_data, TRUE);
}// 搜索
void CDataAnlyDlg::OnBnClickedButton3()
{if (wndSearch.DoModal() == IDOK) {Search(wndSearch.txtSearch, wndSearch.selected, wndSearch.BHEX);}
}// 搜索
void CDataAnlyDlg::Search(CString& txt, SEARCH_TYPE type, BOOL Hex)
{CStringA tmpA;CString tmp;int len{};char* buff{};wchar_t* wbuff{};int val;float fval;double dbval;long long llval;short stval;char cval;int ilen{};_locale_t lcl = _create_locale(LC_ALL, "chs"); // 设置时区为中国switch (type){case SEARCH_TYPE::ASCII_STRING:tmpA = txt;buff = tmpA.GetBuffer();for (;buff[ilen]; ilen++);//当前是推断数据为ASCII类型,所以要使用单字节的形式转换为CString类型tmp = TxtBuff((unsigned char*)buff, ilen);break;case SEARCH_TYPE::UTF16_STRING:wbuff = txt.GetBuffer();for (; wbuff[ilen]; ilen++);tmp = TxtBuff((unsigned char*)wbuff, ilen);// 双字节数据转,CStringbreak;case SEARCH_TYPE::DOUBLE:dbval = _wtof_l (txt, lcl);tmp = TxtBuff((unsigned char*)&dbval, 8);break;case SEARCH_TYPE::INT64:if(Hex)llval = _wcstoll_l(txt, NULL , 16, lcl);else llval = _wtoll(txt);tmp = TxtBuff((unsigned char*)&llval, 8);break;case SEARCH_TYPE::INT32:if (Hex)val = wcstol(txt, NULL, 16);elseval = _wtoi(txt);tmp = TxtBuff((unsigned char*)&val, 4);break;case SEARCH_TYPE::FLOAT:if (Hex)fval = wcstol(txt, NULL, 16);elsefval = _wtoi(txt);tmp = TxtBuff((unsigned char*)&fval, 4);break;case SEARCH_TYPE::SHORT:if (Hex)stval = wcstol(txt, NULL, 16);elsestval = _wtoi(txt);tmp = TxtBuff((unsigned char*)&stval, 2);break;case SEARCH_TYPE::CHAR:if (Hex)cval = wcstol(txt, NULL, 16);elsecval = _wtoi(txt);tmp = TxtBuff((unsigned char*)&cval, 1);break;case SEARCH_TYPE::TXT:tmp = txt;break;default:break;}int nsel = lstView.GetSelectionMark();CString txtRead;if (nsel < 0) nsel = 0;else nsel++;wndData.m_Edit.GetWindowText(txtRead);int istart = txtRead.Find(tmp);if (istart >1) {// 当点击列表弹出数据分析窗口时分析窗口内容并选中wndData.m_Edit.SetFocus();// 窗口获得焦点,不获得焦点无法选中wndData.m_Edit.SetSel(istart, istart + tmp.GetLength(), TRUE); // 选中数据}for (int i = nsel; i< lstView.GetItemCount(); i++){// lstView.GetItemText(i, 2); 获取 lstView列表第i行第3列txtRead = lstView.GetItemText(i, 2);if (txtRead.Find(tmp) > -1) { // 查找字符串// 更改列表控件中的项的状态lstView.SetItemState(i, LVNI_FOCUSED | LVNI_SELECTED, LVNI_FOCUSED | LVNI_SELECTED);// 确保列表视图项至少部分可见,可以定位到选中的位置lstView.EnsureVisible(i, FALSE);// 设置焦点,不设置焦点无法选中行(高亮行)lstView.SetFocus();// return; // 放开return一次只能搜索出一个数据,注释return一次可以搜索出所有满足条件的数据}}
}CString CDataAnlyDlg::TxtBuff(unsigned char* buff, unsigned len)
{CString _result;CString _tmp;for (int i = 0; i < len; i++){_tmp.Format(L"%.2X ", buff[i]);_result = _result + _tmp;}return _result;
}void CDataAnlyDlg::OnClose()
{// TODO: 在此添加消息处理程序代码和/或调用默认值CDialogEx::OnClose();
}
CSet.cpp文件的修改:修改了OnBnClickedOk函数
// CSet.cpp: 实现文件
//#include "pch.h"
#include "DataAnly.h"
#include "CSet.h"
#include "afxdialogex.h"
#include "extern_all.h"
#include <fstream> // 引入io流,用于写文件// CSet 对话框IMPLEMENT_DYNAMIC(CSet, CDialogEx)CSet::CSet(CWnd* pParent /*=nullptr*/): CDialogEx(IDD_SET, pParent), txtName(_T("")), txtTitle(_T(""))
{}CSet::~CSet()
{
}void CSet::DoDataExchange(CDataExchange* pDX)
{CDialogEx::DoDataExchange(pDX);DDX_Control(pDX, IDC_LIST1, lstMsgType);DDX_Control(pDX, IDC_LIST2, lstDataType);DDX_Text(pDX, IDC_EDIT1, txtName);DDX_Text(pDX, IDC_EDIT2, txtTitle);DDX_Text(pDX, IDC_EDIT4, txtFolder);
}BOOL CSet::OnInitDialog()
{CDialogEx::OnInitDialog();SetListView(&lstMsgType);SetListView(&lstDataType);lstMsgType.InsertColumn(0, L"名称", 0, 100);lstMsgType.InsertColumn(1, L"宏定义", 0, 100);lstMsgType.InsertColumn(2, L"类型", 0, 100); // 数据和文本,文本直接显示,数据有点复杂看之后实现位置lstMsgType.InsertColumn(3, L"输出到文件", 0, 100);lstDataType.InsertColumn(0, L"消息类型", 0, 100);lstDataType.InsertColumn(1, L"编号", 0, 100);lstDataType.InsertColumn(2, L"名称", 0, 100);lstDataType.InsertColumn(3, L"背景颜色", 0, 100);lstDataType.InsertColumn(4, L"字体颜色", 0, 100);lstDataType.InsertColumn(5, L"显示", 0, 100);lstDataType.InsertColumn(6, L"输出", 0, 100);lstDataType.InsertColumn(7, L"单独输出", 0, 100);return TRUE;
}BEGIN_MESSAGE_MAP(CSet, CDialogEx)ON_BN_CLICKED(IDOK, &CSet::OnBnClickedOk)ON_BN_CLICKED(IDCANCEL, &CSet::OnBnClickedCancel)ON_NOTIFY(NM_RCLICK, IDC_LIST1, &CSet::OnNMRClickList1)ON_NOTIFY(NM_RCLICK, IDC_LIST2, &CSet::OnNMRClickList2)ON_COMMAND(ID_32771, &CSet::OnAddMsgType)ON_COMMAND(ID_32772, &CSet::OnSetMsg)ON_COMMAND(ID_32773, &CSet::OnDelMsg)ON_COMMAND(ID_32777, &CSet::OnAddDataType)ON_COMMAND(ID_32775, &CSet::OnSetDataType)ON_COMMAND(ID_32776, &CSet::OnDelData)ON_WM_CLOSE()ON_BN_CLICKED(IDC_BUTTON5, &CSet::OnBnClickedButton5)
END_MESSAGE_MAP()// CSet 消息处理程序// 保存按钮点击事件处理函数
void CSet::OnBnClickedOk()
{// TODO: 在此添加控件通知处理程序代码// CDialogEx::OnOK();UpdateData(TRUE);int icount = lstMsgType.GetItemCount();if (txtName == L"") {return;}CString inifile = ConfigPath + txtName + ".ini";CString txt;CString App;CString _Name;CString AppData;CString _Define;txt.Format(L"%d", icount);/*.ini是微软搞的配置文件(Windows系统配置文件所采用的结构(存储格式)).ini文件它有结构详细的结构看当前程序给生成的.ini文件内容WritePrivateProfileString是用来给.ini文件写数据的WritePrivateProfileString它是给.ini文件写一个字符串*/WritePrivateProfileString(L"main", L"count", txt, inifile);WritePrivateProfileString(L"main", L"title", txtTitle, inifile);WritePrivateProfileString(L"main", L"hfile", txtFolder, inifile);WritePrivateProfileString(L"main", L"name", txtName, inifile);// PathIsDirectory函数是用来验证路径是否为有效目录if (!PathIsDirectory(DataPath)) {// CreateDirectory函数是用来新建目录if (!CreateDirectory(DataPath, NULL)) {AfxMessageBox(L"创建Data文件夹失败,请检查文件目录权限");return;}}/*txtName用来表示一个游戏CString _folder = DataPath + txtName;// PathIsDirectory函数是用来验证路径是否为有效目录if (!PathIsDirectory(_folder)) {// CreateDirectory函数是用来新建目录if (!CreateDirectory(_folder, NULL)) {AfxMessageBox(L"创建数据保存文件夹失败,请检查文件目录权限");return;}}这几行代码的意思就是根据游戏创建文件夹,然后把要的数据放在这个文件夹里这样的好处,如果还有另一个游戏,那么它也会根据配置的名字创建文件然后把它里面我们想要的数据就放在它的文件夹里用来区分如果还不懂,那么看程序执行后的效果*/ CString _folder = DataPath + txtName;// PathIsDirectory函数是用来验证路径是否为有效目录if (!PathIsDirectory(_folder)) {// CreateDirectory函数是用来新建目录if (!CreateDirectory(_folder, NULL)) {AfxMessageBox(L"创建数据保存文件夹失败,请检查文件目录权限");return;}}// 设置区域,在文件操作是,可以用来支持中文std::locale& loc = std::locale::global(std::locale(std::locale(), "", LC_CTYPE));// 创建文件写出流std::wofstream ofs(txtFolder);if (!ofs.bad()) {ofs << L"#define Anly" << std::endl;ofs << L"#ifdef Anly" << std::endl;ofs << L"#pragma once" << std::endl;ofs << L"enum class TTYPE :int {" << std::endl;}for (int i = 0; i < icount; i++){_Name = lstMsgType.GetItemText(i, 0);_Define = lstMsgType.GetItemText(i, 1);App.Format(L"Message_%d", i);// 消息定义列表的名称列的内容WritePrivateProfileString(App, L"name", _Name, inifile);// 消息定义列表的宏定义列的内容WritePrivateProfileString(App, L"define", _Define, inifile);// 消息定义列表的类型列的内容WritePrivateProfileString(App, L"type", lstMsgType.GetItemText(i, 2), inifile);// 消息定义列表的输出到文件列的内容WritePrivateProfileString(App, L"istxt", lstMsgType.GetItemText(i, 3), inifile);int datacount = 0;/*根据消息的宏定义创建文件夹,用于保存对应的数据比如,现在的宏定义是 I_RECV 然后生成一个文件夹名字是 I_RECV 的,然后把所有 I_RECV 相关的数据全部放在这个文件夹里*/ _folder = DataPath + txtName + L"\\" + _Define;// PathIsDirectory函数是用来验证路径是否为有效目录if (!PathIsDirectory(_folder)) {// CreateDirectory函数是用来新建目录if (!CreateDirectory(_folder, NULL)) {AfxMessageBox(L"创建数据保存文件夹失败,请检查文件目录权限");return;}}if (!ofs.bad()) {ofs << L"\t" << _Define.GetBuffer() << L"=" << i << L"," << std::endl;}for (int j = 0; j < lstDataType.GetItemCount(); j++){if (lstDataType.GetItemText(j, 0) == _Name) {AppData.Format(L"%s_%d", _Name, datacount++);// 数据定义列表的编号列的内容 WritePrivateProfileString(AppData, L"id", lstDataType.GetItemText(j,1), inifile);// 数据定义列表的名称列的内容 WritePrivateProfileString(AppData, L"name", lstDataType.GetItemText(j,2), inifile);// 数据定义列表的背景颜色列的内容 WritePrivateProfileString(AppData, L"backcolor", lstDataType.GetItemText(j,3), inifile);// 数据定义列表的字体颜色列的内容 WritePrivateProfileString(AppData, L"fontcolor", lstDataType.GetItemText(j,4), inifile);// 数据定义列表的显示列的内容 WritePrivateProfileString(AppData, L"isui", lstDataType.GetItemText(j,5), inifile);// 数据定义列表的输出列的内容 WritePrivateProfileString(AppData, L"istxt", lstDataType.GetItemText(j,6), inifile);// 数据定义列表的单独输出列的内容 WritePrivateProfileString(AppData, L"istxtalone", lstDataType.GetItemText(j,7), inifile);}}txt.Format(L"%d", datacount);WritePrivateProfileString(App, L"count", txt, inifile);}if (!ofs.bad()) {ofs << L"\tMAX" << std::endl;ofs << L"};" << std::endl;ofs << L"#define _TITLE L\"" << txtTitle .GetBuffer()<<L"\"" << std::endl;ofs << L"typedef struct COPY_DATA {" << std::endl;ofs << L"\ttime_t ltime; // 时间" << std::endl;ofs << L"\tunsigned MsgType; // 数据类型" << std::endl;ofs << L"\tunsigned len; // 数据长度" << std::endl;ofs << L"\tchar buff[0x2000]; // 数据" << std::endl;ofs << L"}*PCOPYDATA;" << std::endl;ofs << L"class CAnly{" << std::endl;ofs << L"public:" << std::endl;ofs << L"LRESULT SendData(TTYPE type, unsigned MsgType, void* buff, unsigned len);" << std::endl;ofs << L"};" << std::endl;ofs << L"#endif" << std::endl;}// 还原区域std::locale::global(loc);// 关闭流ofs.close();DefConfigIni = inifile; // 设置要加载的配置文件CDialogEx::OnOK();
}void CSet::OnBnClickedCancel()
{// CDialogEx::OnCancel();// 声明一个文件选择对话框里的筛选TCHAR szFilters[] = _T("数据分析配置(*.ini)|*.ini||");// 创建一个文件选择对话框CFileDialog dialog(TRUE, NULL, NULL, 6, szFilters);// 设置文件选择对话框打开之后默认地址dialog.m_ofn.lpstrInitialDir = ConfigPath;if (dialog.DoModal() == IDOK) {// 获取选择的文件的目录,获取的是绝对路径CString inifile = dialog.GetPathName();LoadConfig(inifile, dialog.GetFileTitle());}
}void CSet::OnNMRClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{LPNMITEMACTIVATE p = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);// TODO: 在此添加控件通知处理程序代码*pResult = 0;nMsgSel = p->iItem;/*GetMessagePos函数可以,获取光标位置返回值低16位是x,高16位是y*/DWORD dwPos = GetMessagePos();CPoint point(LOWORD(dwPos), HIWORD(dwPos));CMenu menu;menu.LoadMenuW(IDR_MENU1);// 根据菜单窗口id获取一个菜单窗口对象CMenu* pop = menu.GetSubMenu(0);// 获取第一个菜单,也就是现在的消息设置这个菜单// 在指定位置显示浮动弹出菜单,并跟踪弹出菜单上项的选择情况。pop->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON, point.x, point.y, this);
}void CSet::OnNMRClickList2(NMHDR* pNMHDR, LRESULT* pResult)
{LPNMITEMACTIVATE p = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);// TODO: 在此添加控件通知处理程序代码*pResult = 0;nDataSel = p->iItem;/*GetMessagePos函数可以,获取光标位置返回值低16位是x,高16位是y
*/DWORD dwPos = GetMessagePos();CPoint point(LOWORD(dwPos), HIWORD(dwPos));CMenu menu;menu.LoadMenuW(IDR_MENU1);// 根据菜单窗口id获取一个菜单窗口对象CMenu* pop = menu.GetSubMenu(1);// 获取第一个菜单,也就是现在的消息设置这个菜单// 在指定位置显示浮动弹出菜单,并跟踪弹出菜单上项的选择情况。pop->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);
}void CSet::OnAddMsgType()
{if (wndMsgSet.DoModal() == IDOK) {int icount = lstMsgType.GetItemCount();for (int i = 0; i < icount; i++){if (lstMsgType.GetItemText(i, 0) == wndMsgSet.txtName) {AfxMessageBox(L"消息名已存在!");return;}if (lstMsgType.GetItemText(i, 1) == wndMsgSet.txtDefine) {AfxMessageBox(L"宏定义已存在!");return;}}lstMsgType.InsertItem(icount, wndMsgSet.txtName);lstMsgType.SetItemText(icount, 1, wndMsgSet.txtDefine);lstMsgType.SetItemText(icount, 2, wndMsgSet.txtTypeName);lstMsgType.SetItemText(icount, 3, txtBool[wndMsgSet.IsTxt]);}
}void CSet::OnSetMsg()
{if (nMsgSel > -1) {wndMsgSet.SetDatas(lstMsgType.GetItemText(nMsgSel, 0), lstMsgType.GetItemText(nMsgSel, 1), lstMsgType.GetItemText(nMsgSel, 2), lstMsgType.GetItemText(nMsgSel, 3)==L"是");if (wndMsgSet.DoModal() == IDOK) {int icount = lstMsgType.GetItemCount();for (int i = 0; i < icount; i++){if (lstMsgType.GetItemText(i, 0) == wndMsgSet.txtName) {if (i != nMsgSel) {AfxMessageBox(L"消息名已存在!");return;}}if (lstMsgType.GetItemText(i, 1) == wndMsgSet.txtDefine) {if (i != nMsgSel) {AfxMessageBox(L"宏定义已存在!");return;}}}lstMsgType.SetItemText(nMsgSel, 0, wndMsgSet.txtName);lstMsgType.SetItemText(nMsgSel, 1, wndMsgSet.txtDefine);lstMsgType.SetItemText(nMsgSel, 2, wndMsgSet.txtTypeName);lstMsgType.SetItemText(nMsgSel, 3, txtBool[wndMsgSet.IsTxt]);}}
}void CSet::OnDelMsg()
{if (nMsgSel > -1) {lstMsgType.DeleteItem(nMsgSel);}
}void CSet::OnAddDataType()
{if (nMsgSel > -1) {// 给消息数据窗口里消息名称输入框赋值wndDataSet.txtName = lstMsgType.GetItemText(nMsgSel, 0);if (wndDataSet.DoModal() == IDOK) {if (!IsExitMsg(wndDataSet.txtName)) {AfxMessageBox(L"消息不存在,可能已经被删除!");return;}if (IsExitData(wndDataSet.txtName, wndDataSet.txtId)) {AfxMessageBox(L"消息对应的数据编号已经存在!");return;}int icount = lstDataType.GetItemCount();lstDataType.InsertItem(icount, wndDataSet.txtName);lstDataType.SetItemText(icount,1, wndDataSet.txtId);lstDataType.SetItemText(icount,2, wndDataSet.txtDataName);lstDataType.SetItemText(icount,3, wndDataSet.txtBackColor);lstDataType.SetItemText(icount,4, wndDataSet.txtFontColor);lstDataType.SetItemText(icount,5, txtBool[wndDataSet.IsShowUI]);lstDataType.SetItemText(icount,6, txtBool[wndDataSet.IsTxt]);lstDataType.SetItemText(icount,7, txtBool[wndDataSet.IsTxtAlone]);}}
}void CSet::OnSetDataType()
{if (nDataSel > -1) {wndDataSet.txtName = lstDataType.GetItemText(nDataSel, 0),wndDataSet.txtId = lstDataType.GetItemText(nDataSel, 1),wndDataSet.txtDataName = lstDataType.GetItemText(nDataSel, 2),wndDataSet.txtBackColor = lstDataType.GetItemText(nDataSel, 3),wndDataSet.txtFontColor = lstDataType.GetItemText(nDataSel, 4),wndDataSet.IsShowUI = lstDataType.GetItemText(nDataSel, 5) == L"是";wndDataSet.IsTxt = lstDataType.GetItemText(nDataSel, 6) == L"是";wndDataSet.IsTxtAlone = lstDataType.GetItemText(nDataSel, 7) == L"是";if (wndDataSet.DoModal() == IDOK) {int icount = nDataSel;if (!IsExitMsg(wndDataSet.txtName)) {AfxMessageBox(L"消息不存在,可能已经被删除!");lstDataType.DeleteItem(nDataSel);return;}if (IsExitData(wndDataSet.txtName, wndDataSet.txtId, icount)) {AfxMessageBox(L"消息对应的数据编号已经存在!");return;}lstDataType.SetItemText(icount, 0, wndDataSet.txtName);lstDataType.SetItemText(icount, 1, wndDataSet.txtId);lstDataType.SetItemText(icount, 2, wndDataSet.txtDataName);lstDataType.SetItemText(icount, 3, wndDataSet.txtBackColor);lstDataType.SetItemText(icount, 4, wndDataSet.txtFontColor);lstDataType.SetItemText(icount, 5, txtBool[wndDataSet.IsShowUI]);lstDataType.SetItemText(icount, 6, txtBool[wndDataSet.IsTxt]);lstDataType.SetItemText(icount, 7, txtBool[wndDataSet.IsTxtAlone]);}}
}void CSet::OnDelData()
{if (nDataSel > -1) {lstDataType.DeleteItem(nDataSel);}
}bool CSet::IsExitMsg(CString& _name)
{for (int i = 0; i < lstMsgType.GetItemCount(); i++){if (lstMsgType.GetItemText(i, 0) == _name) {return true;}}return false;
}bool CSet::IsExitData(CString& _name, CString& _id)
{for (int i = 0; i < lstDataType.GetItemCount(); i++){if ((lstDataType.GetItemText(i, 0) == _name)&&(lstDataType.GetItemText(i, 1) == _id)) {return true;}}return false;
}bool CSet::IsExitData(CString& _name, CString& _id, int index)
{for (int i = 0; i < lstDataType.GetItemCount(); i++){if ((lstDataType.GetItemText(i, 0) == _name) && (lstDataType.GetItemText(i, 1) == _id)&&(index != i)) {return true;}}return false;
}void CSet::OnClose()
{// TODO: 在此添加消息处理程序代码和/或调用默认值CDialogEx::OnCancel();
}void CSet::LoadConfig(CString inifile, CString _name)
{lstDataType.DeleteAllItems(); // 清空数据定义列表lstMsgType.DeleteAllItems(); // 清空消息定义列表// 获取选择的文件的文件名txtName = _name;/*从配置文件中读取一个数据,意思是从main下的count读取一个数字int类型的数字GetPrivateProfileInt用来获取.ini文件里的内容,最后的Int表示读取的数据类型Int就表示读取int类型,String表示读取String类型,也就是一个字符串GetPrivateProfileString(L"main", L"title", L"", buff, 0xFF, inifile);里的L""是默认值GetPrivateProfileInt(L"main", L"count", 0, inifile);里的0是默认值默认值的意思,如果读取不到数据就把默认值返回给我们*/int icount = GetPrivateProfileInt(L"main", L"count", 0, inifile);wchar_t buff[0xFF];GetPrivateProfileString(L"main", L"title", L"", buff, 0xFF, inifile);txtTitle = buff;GetPrivateProfileString(L"main", L"hfile", L"", buff, 0xFF, inifile);txtFolder = buff;/*CString App;CString AppData;CString _Name;它是用来初始化字符串的,利用 Foramt 函数把从.ini文件里获取的数据全部转成字符串*/CString App;CString AppData;CString _Name;for (int i = 0; i < icount; i++){App.Format(L"Message_%d", i);GetPrivateProfileString(App, L"name", L"", buff, 0xFF, inifile);_Name.Format(L"%s", buff);lstMsgType.InsertItem(i, buff);GetPrivateProfileString(App, L"define", L"", buff, 0xFF, inifile);lstMsgType.SetItemText(i, 1, buff);GetPrivateProfileString(App, L"type", L"", buff, 0xFF, inifile);lstMsgType.SetItemText(i, 2, buff);GetPrivateProfileString(App, L"istxt", L"", buff, 0xFF, inifile);lstMsgType.SetItemText(i, 3, buff);int lcount = GetPrivateProfileInt(App, L"count", 0, inifile);int datacount = lstDataType.GetItemCount();for (int j = 0; j < lcount; j++){AppData.Format(L"%s_%d", _Name.GetBuffer(), j);lstDataType.InsertItem(datacount, _Name);GetPrivateProfileString(AppData, L"id", L"0", buff, 0xFF, inifile);lstDataType.SetItemText(datacount, 1, buff);GetPrivateProfileString(AppData, L"name", L"0", buff, 0xFF, inifile);lstDataType.SetItemText(datacount, 2, buff);GetPrivateProfileString(AppData, L"backcolor", L"0", buff, 0xFF, inifile);lstDataType.SetItemText(datacount, 3, buff);GetPrivateProfileString(AppData, L"fontcolor", L"0", buff, 0xFF, inifile);lstDataType.SetItemText(datacount, 4, buff);GetPrivateProfileString(AppData, L"isui", L"0", buff, 0xFF, inifile);lstDataType.SetItemText(datacount, 5, buff);GetPrivateProfileString(AppData, L"istxt", L"0", buff, 0xFF, inifile);lstDataType.SetItemText(datacount, 6, buff);GetPrivateProfileString(AppData, L"istxtalone", L"0", buff, 0xFF, inifile);lstDataType.SetItemText(datacount, 7, buff);}}UpdateData(FALSE);
}void CSet::OnBnClickedButton5()
{// 创建一个文件夹选择对话框//CFolderPickerDialog dialog;CFileDialog dialog(TRUE, NULL, NULL, 6, L"");if (dialog.DoModal() == IDOK) {txtFolder = dialog.GetPathName();UpdateData(FALSE);}
}
GameWinSock.cpp文件的修改:修改了 OnConnect函数
#include "pch.h"
#include "GameWinSock.h"
#include "extern_all.h"GameWinSock::PROC GameWinSock::_OnConnect{};
GameWinSock::PROC GameWinSock::_OnSend{};
GameWinSock::PROC GameWinSock::_OnRecv{};
// 这个函数拦截了游戏的连接
bool GameWinSock::OnConnect(char* ip, unsigned port)
{
#ifdef Anlyanly->SendData(TTYPE::I_DIS, 0, ip, 16);// 长度24的原因,它是宽字节要,一个文字要2个字节,一共是10个文字加上结尾的0是11个// 所以 11 乘以2,然后再加2 anly->SendData(TTYPE::I_LOG, 0, L"服务器正在连接。。。", 24);
#endif// this是ecx,HOOK的点已经有ecx了WinSock = this;bool b = (this->*_OnConnect)(ip, port);// 下方注释的代码时为了防止多次注入,导致虚函数地址不恢复问题导致死循环,通过一次性HOOK也能解决/*unsigned* vtable = (unsigned*)this;vtable = (unsigned*)vtable[0];union {unsigned value;bool(GameWinSock::* _proc)(char*, unsigned);} vproc;vproc._proc = _OnConnect;DWORD oldPro, backProc;VirtualProtect(vtable, 0x10x00, PAGE_EXECUTE_READWRITE, &oldPro);vtable[0x34 / 4] = vproc.value;VirtualProtect(vtable, 0x10x00, oldPro, &backProc);*/return b;
}bool GameWinSock::OnSend(char* buff, unsigned len)
{/*这里就可以监控游戏发送的数据了*/#ifdef Anlyanly->SendData(TTYPE::I_SEND, buff[0], buff, len);
#endifreturn (this->*_OnSend)(buff, len);
}bool GameWinSock::OnRecving(char* buff, unsigned len)
{// MessageBoxA(0, "11111111111111", "0", MB_OK);/*监控游戏接收的数据包*/
#ifdef Anlyanly->SendData(TTYPE::I_RECV, 0, buff, len);
#endifreturn true;
}bool GameWinSock::OnRecv(char* buff, unsigned len)
{
//#ifdef Anly
// anly->SendData(1, buff, len);
//#endifreturn (this->*_OnRecv)(buff, len);
}