ReKeyDlg.cpp
上传用户:jinbin898
上传日期:2022-06-24
资源大小:89k
文件大小:18k
- // ReKeyDlg.cpp : 实现文件
- //
- #include "stdafx.h"
- #include "ReKey.h"
- #include "ReKeyDlg.h"
- #include "tlhelp32.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- // 用于应用程序“关于”菜单项的 CAboutDlg 对话框
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // 对话框数据
- enum { IDD = IDD_ABOUTBOX };
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
- // 实现
- protected:
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- END_MESSAGE_MAP()
- // CReKeyDlg 对话框
- #define WM_SHOWTASK WM_USER+101
- TCHAR g_szConfigFile[MAX_PATH];
- HWND hWar3;
- HHOOK hHook;
- void* g_GameDllbase; //进程war3中Game.dll的加载地址
- CString g_war3InstallPath;
- TCHAR g_szOutDebugFile[MAX_PATH] = _T("Rekey_OutDebug.txt");
- CStdioFile g_DebugFile;
- unsigned char g_numPAD[6][2] = {
- 0,VK_NUMPAD1,
- 0,VK_NUMPAD2,
- 0,VK_NUMPAD4,
- 0,VK_NUMPAD5,
- 0,VK_NUMPAD7,
- 0,VK_NUMPAD8};
- void WriteDebugFile(const CString& str);
- void* GetProcessDllBase(DWORD dwPID, const CString& moduleName);
- LRESULT CALLBACK LowKeyboardHookProc( int code,
- WPARAM wParam,
- LPARAM lParam
- )
- {
- HWND topWnd = ::GetForegroundWindow();
- DWORD processid;
- /*DWORD threadid = */::GetWindowThreadProcessId(hWar3,&processid);
- HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS,false,processid);
- // War3 各版本聊天判别地址如下
- // 1.20、1.21:
- // 0x0045CB8C
- //
- // 1.22:
- // 0x6FABDFE0
- //
- // 1.23:
- // 0x6FAD6E30
- //
- // 1.24
- // 0x6FAE8450
- //
- // 其中,后三个的 6F 是 Game.dll 的基址。但是 DLL 的基址并不是总是固定不变的,
- // 所以实际中,可靠的方法是获取 hDll,然后加上 0x00XXXXXX
- BOOL bchatSuccess = FALSE;
- LPCVOID pchatbase = (LPCVOID)0x0045CB8C; //判断当前是否为聊天状态
- int chatStatus=0;
- bchatSuccess = ::ReadProcessMemory(hProcess,pchatbase,&chatStatus,4,NULL);
- CloseHandle(hProcess);
- if (topWnd == hWar3)
- {
- //对话框上开启debug的情况下,输出debug信息
- CReKeyDlg *dlg = (CReKeyDlg*)AfxGetMainWnd();
- if (dlg->m_bIsDebug && WM_KEYDOWN==(DWORD)wParam)
- {
- CString outLog;
- TCHAR keyname[10];
- DWORD param = ((PKBDLLHOOKSTRUCT)lParam)->scanCode<<16;
- GetKeyNameText(param,keyname,_countof(keyname));
- outLog.Format(_T(" -- War3=0x%08X bChat=[%d]%d Key=[%s]n"),hProcess,bchatSuccess,chatStatus,keyname);
- WriteDebugFile(outLog);
- }
- //聊天状态,不做改建。
- if ( bchatSuccess && chatStatus!=0 )
- {
- TRACE0("chating...n");
- return CallNextHookEx(hHook,code,wParam,lParam);
- }
- //根据键映射更改按键
- PKBDLLHOOKSTRUCT kbStruct;
- if (WM_KEYDOWN==(DWORD)wParam)
- {
- kbStruct = (PKBDLLHOOKSTRUCT)lParam;
- for (int i=0;i<6;i++)
- {
- //TRACE2("g[i][0] = %d, vkCode = %dn", g_numPAD[i][0], kbStruct->vkCode);
- if (g_numPAD[i][0] == kbStruct->vkCode)
- {
- keybd_event(g_numPAD[i][1],0,0,0);
- keybd_event(g_numPAD[i][1],0,KEYEVENTF_KEYUP,0);
-
- return 1;
- }
- }
- }
- }
- return CallNextHookEx(hHook,code,wParam,lParam);
- }
- CReKeyDlg::CReKeyDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CReKeyDlg::IDD, pParent)
- , m_bIsDebug(FALSE)
- {
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CReKeyDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- DDX_Control(pDX, IDC_STATUS, m_status);
- DDX_Control(pDX, IDC_EDIT7, m_edit7);
- DDX_Control(pDX, IDC_EDIT8, m_edit8);
- DDX_Control(pDX, IDC_EDIT4, m_edit4);
- DDX_Control(pDX, IDC_EDIT5, m_edit5);
- DDX_Control(pDX, IDC_EDIT1, m_edit1);
- DDX_Control(pDX, IDC_EDIT2, m_edit2);
- DDX_Control(pDX, IDC_WAR3TOOLTIP, m_staticTooltip);
- DDX_Check(pDX, IDC_DEBUGSWITCH, m_bIsDebug);
- DDX_Control(pDX, IDC_STARTWARIII, m_StartWar3Ctrl);
- }
- BEGIN_MESSAGE_MAP(CReKeyDlg, CDialog)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- //}}AFX_MSG_MAP
- ON_WM_CLOSE()
- ON_WM_TIMER()
- ON_EN_CHANGE(IDC_EDIT1, &CReKeyDlg::OnEnChangeEdit1)
- ON_EN_CHANGE(IDC_EDIT2, &CReKeyDlg::OnEnChangeEdit2)
- ON_EN_CHANGE(IDC_EDIT4, &CReKeyDlg::OnEnChangeEdit4)
- ON_EN_CHANGE(IDC_EDIT5, &CReKeyDlg::OnEnChangeEdit5)
- ON_EN_CHANGE(IDC_EDIT7, &CReKeyDlg::OnEnChangeEdit7)
- ON_EN_CHANGE(IDC_EDIT8, &CReKeyDlg::OnEnChangeEdit8)
- ON_MESSAGE(WM_SHOWTASK,&CReKeyDlg::OnShowTask)
- ON_STN_CLICKED(IDC_STARTWARIII, &CReKeyDlg::OnStnClickedStartwariii)
- ON_COMMAND(IDM_NF_MENU_EXIT,&CReKeyDlg::OnNFMenuExit)
- ON_COMMAND(IDM_SETWAR3PATH,&CReKeyDlg::OnSetWar3Path)
- ON_BN_CLICKED(IDC_DEBUGSWITCH, &CReKeyDlg::OnBnClickedDebugswitch)
- END_MESSAGE_MAP()
- // CReKeyDlg 消息处理程序
- BOOL CReKeyDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // 将“关于...”菜单项添加到系统菜单中。
- // IDM_ABOUTBOX 必须在系统命令范围内。
- 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);
- }
- }
- // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
- // 执行此操作
- SetIcon(m_hIcon, TRUE); // 设置大图标
- SetIcon(m_hIcon, FALSE); // 设置小图标
- // TODO: 在此添加额外的初始化代码
- //输出调试信息 --开关
-
- //提升权限
- HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION,FALSE,GetCurrentProcessId());
- if (hProcess)
- {
- HANDLE hToken;
- if (OpenProcessToken(hProcess,TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken))
- {
- LUID luid;
- if (LookupPrivilegeValue(NULL,SE_DEBUG_NAME,&luid))
- {
- TOKEN_PRIVILEGES tp;
- tp.PrivilegeCount = 1;
- tp.Privileges[0].Luid = luid;
- tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
- if(!AdjustTokenPrivileges(hToken,FALSE,&tp,sizeof(TOKEN_PRIVILEGES),NULL,NULL)) //AdjustTokenPrivileges
- MessageBox(_T("AdjustTokenPrivileges失败。"),_T("提升权限"));
- }
- else //LookupPrivilegeValue
- {
- MessageBox(_T("LookupPrivilegeValue失败。"),_T("提升权限"));
- }
- CloseHandle(hToken);
- }
- else //OpenProcessToken
- {
- MessageBox(_T("OpenProcessToken失败。"),_T("提升权限"));
- }
- }
- //得到应用程序全路径
- TCHAR fullPath[MAX_PATH];
- GetModuleFileName(NULL,fullPath,MAX_PATH);
- //解析路径,得到应用程序目录
- TCHAR drive[_MAX_DRIVE];
- TCHAR dir[_MAX_DIR];
- _tsplitpath_s(fullPath,drive,_countof(drive),dir,_countof(dir),NULL,0,NULL,0);
- CString strAppPath;
- strAppPath.Format(_T("%s%s"),drive,dir);
- //生成配置路径
- CString configFile = _T("ReKey.conf"); //配置文件名
- CString configPath = strAppPath+configFile;
- TCHAR* szConfigFile = configPath.GetBuffer(configPath.GetLength());
- _tcscpy_s(g_szConfigFile,_countof(g_szConfigFile),szConfigFile);
- configPath.ReleaseBuffer();
- m_nid.cbSize = 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; //user defined
- m_nid.hIcon = LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));
- _tcscpy_s(m_nid.szTip,_countof(m_nid.szTip),L"War3改键工具");
- Shell_NotifyIcon(NIM_ADD,&m_nid);
- m_edit1.SetLimitText(1);
- m_edit2.SetLimitText(1);
- m_edit4.SetLimitText(1);
- m_edit5.SetLimitText(1);
- m_edit7.SetLimitText(1);
- m_edit8.SetLimitText(1);
- m_hTimer = SetTimer(1,2000,NULL);
- CFile fileRead;
- if (fileRead.Open(g_szConfigFile,CFile::modeRead))
- {
- CArchive arLoad(&fileRead,CArchive::load);
- arLoad>>g_war3InstallPath;
- for (int i=0;i<6;i++)
- {
- arLoad>>g_numPAD[i][0];
- }
- fileRead.Close();
- m_edit1.SetWindowText(CString((char)g_numPAD[0][0]));
- m_edit2.SetWindowText(CString((char)g_numPAD[1][0]));
- m_edit4.SetWindowText(CString((char)g_numPAD[2][0]));
- m_edit5.SetWindowText(CString((char)g_numPAD[3][0]));
- m_edit7.SetWindowText(CString((char)g_numPAD[4][0]));
- m_edit8.SetWindowText(CString((char)g_numPAD[5][0]));
- }
- if ( 0 == g_war3InstallPath.GetLength() )
- {
- m_staticTooltip.SetWindowText(_T("<-点击图标设置War3路径"));
- }
- else
- {
- m_staticTooltip.SetWindowText(_T("<-点击图标启动War3"));
- }
- //设置war3图标
- CWar3Container *war3IconContainer = (CWar3Container*)GetDlgItem(IDC_STARTWARIII);
- HICON icon = (HICON)LoadImage(theApp.m_hInstance,MAKEINTRESOURCE(IDI_WAR3X),IMAGE_ICON,48,48,LR_DEFAULTCOLOR);
- war3IconContainer->SetIcon(icon);
- GetDlgItem(IDC_DEBUGSWITCH)->ShowWindow(SW_HIDE);
- if (__argc == 2)
- {
- CString debugFlag = _T("/debug");
- LPCTSTR flag= __targv[1];
- if (0 == debugFlag.CompareNoCase(flag)) //有debug参数
- {
- GetDlgItem(IDC_DEBUGSWITCH)->ShowWindow(SW_SHOW);
- }
- }
- return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
- }
- void CReKeyDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- // else if ((nID & 0xFFF0) == SC_CLOSE/*SC_ICON*/)
- // {
- // ShowWindow(SW_HIDE);
- // }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // 如果向对话框添加最小化按钮,则需要下面的代码
- // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
- // 这将由框架自动完成。
- void CReKeyDlg::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
- {
- CDialog::OnPaint();
- }
- }
- //当用户拖动最小化窗口时系统调用此函数取得光标
- //显示。
- HCURSOR CReKeyDlg::OnQueryDragIcon()
- {
- return static_cast<HCURSOR>(m_hIcon);
- }
- void CReKeyDlg::OnClose()
- {
- // TODO: 在此添加消息处理程序代码和/或调用默认值
- if (hHook)
- {
- if (!UnhookWindowsHookEx(hHook))
- {
- MessageBox(L"UnHook Failed!");
- }
- }
- if ( g_DebugFile.m_pStream )
- {
- g_DebugFile.Close();
- }
- Shell_NotifyIcon(NIM_DELETE,&m_nid);
- //序列化设置
- CFile fileStroe;
- if (!fileStroe.Open(g_szConfigFile,CFile::modeWrite|CFile::modeCreate))
- {
- MessageBox(_T("序列化失败,文件没有找到!"));
- CDialog::OnClose();
- return ;
- }
- CArchive arStore(&fileStroe,CArchive::store);
- //保存war3运行路径
- arStore<<g_war3InstallPath;
- //保存按键设置
- for (int i=0;i<6;i++)
- {
- arStore<<g_numPAD[i][0];
- }
- arStore.Flush(); //不flush总是出现错误
- fileStroe.Flush();
-
- fileStroe.Close();
- CDialog::OnClose();
- }
- void CReKeyDlg::OnTimer(UINT_PTR nIDEvent)
- {
- // TODO: 在此添加消息处理程序代码和/或调用默认值
- hWar3 = ::FindWindow(NULL,L"Warcraft III");
- if (hWar3)
- {
- ::SendMessage(hWar3,WM_KEYDOWN,VK_OEM_4,0);
- ::SendMessage(hWar3,WM_KEYDOWN,VK_OEM_6,0);
- m_status.SetWindowText(L"War3状态:运行中");
- if (hHook == NULL)
- {
- hHook = SetWindowsHookEx(WH_KEYBOARD_LL,LowKeyboardHookProc,GetModuleHandle(NULL),0);
- HWND topWnd = ::GetForegroundWindow();
- DWORD processid;
- ::GetWindowThreadProcessId(hWar3,&processid);
- CString str = L"Game.dll";
- g_GameDllbase = ::GetProcessDllBase(processid,str);
- }
- }
- else
- {
- m_status.SetWindowText(L"War3状态:未启动");
- if (hHook != NULL)
- {
- if (UnhookWindowsHookEx(hHook))
- {
- hHook = NULL;
- }
- }
- }
- CDialog::OnTimer(nIDEvent);
- }
- void CReKeyDlg::OnEnChangeEdit1()
- {
- UniFormKey(m_edit1);
- CString key;
- m_edit1.GetWindowText(key);
- char *str = (char*)(LPCTSTR)key;
- g_numPAD[0][0] = *str;
- }
- void CReKeyDlg::OnEnChangeEdit2()
- {
- UniFormKey(m_edit2);
- CString key;
- m_edit2.GetWindowText(key);
- char *str = (char*)(LPCTSTR)key;
- g_numPAD[1][0] = *str;
- }
- void CReKeyDlg::OnEnChangeEdit4()
- {
- UniFormKey(m_edit4);
- CString key;
- m_edit4.GetWindowText(key);
- char *str = (char*)(LPCTSTR)key;
- g_numPAD[2][0] = *str;
- }
- void CReKeyDlg::OnEnChangeEdit5()
- {
- UniFormKey(m_edit5);
- CString key;
- m_edit5.GetWindowText(key);
- char *str = (char*)(LPCTSTR)key;
- g_numPAD[3][0] = *str;
- }
- void CReKeyDlg::OnEnChangeEdit7()
- {
- UniFormKey(m_edit7);
- CString key;
- m_edit7.GetWindowText(key);
- char *str = (char*)(LPCTSTR)key;
- g_numPAD[4][0] = *str;
- }
- void CReKeyDlg::OnEnChangeEdit8()
- {
- // TODO: 如果该控件是 RICHEDIT 控件,它将不
- // 发送此通知,除非重写 CDialog::OnInitDialog()
- // 函数并调用 CRichEditCtrl().SetEventMask(),
- // 同时将 ENM_CHANGE 标志“或”运算到掩码中。
- // TODO: 在此添加控件通知处理程序代码
- UniFormKey(m_edit8);
- CString key;
- m_edit8.GetWindowText(key);
- char *str = (char*)(LPCTSTR)key;
- g_numPAD[5][0] = *str;
- }
- //使各键不至于重复
- bool CReKeyDlg::UniFormKey(CEdit& edit)
- {
- CString strc;
- edit.GetWindowText(strc);
- //空表示无设置,此时不应检查其唯一性,这是可能还有好几个按键是无设置的呢
- if (strc.Compare(_T(""))==0)
- {
- return true;
- }
- //由于TCHAR字符是2位,而且它按小尾的字节顺序排列,所以第一字节是char值
- //考虑 TCHAR tc = 'T';
- // char c = (char)(int)tc;
- char * c= (char*)(LPCTSTR)strc;
- HWND hWndEdit = edit.GetSafeHwnd();
- int iEdit = -1;
- if (hWndEdit == m_edit1.GetSafeHwnd())
- {
- iEdit = 0;
- }else if (hWndEdit == m_edit2.GetSafeHwnd())
- {
- iEdit = 1;
- }else if (hWndEdit == m_edit4.GetSafeHwnd())
- {
- iEdit = 2;
- }else if (hWndEdit == m_edit5.GetSafeHwnd())
- {
- iEdit = 3;
- }else if (hWndEdit == m_edit7.GetSafeHwnd())
- {
- iEdit = 4;
- }else if (hWndEdit == m_edit8.GetSafeHwnd())
- {
- iEdit = 5;
- }
- if (iEdit==-1)
- {
- return false;
- }
- int i=0;
- for (;i<6;i++)
- {
- if (*c == g_numPAD[i][0])
- {
- if (iEdit==i)
- continue;
- else
- break;
- }
- }
- switch (i)
- {
- case 0:
- m_edit1.SetWindowText(_T(""));
- break;
- case 1:
- m_edit2.SetWindowText(_T(""));
- break;
- case 2:
- m_edit4.SetWindowText(_T(""));
- break;
- case 3:
- m_edit5.SetWindowText(_T(""));
- break;
- case 4:
- m_edit7.SetWindowText(_T(""));
- break;
- case 5:
- m_edit8.SetWindowText(_T(""));
- break;
- default:
- break;
- }
- return true;
- }
- LRESULT CReKeyDlg::OnShowTask(WPARAM wParam, LPARAM lParam)
- {
- if(wParam != IDR_MAINFRAME)
- return 1;
- CMenu menu;
- LPPOINT lpoint;
- switch (lParam)
- {
- case WM_RBUTTONUP:
- lpoint = new tagPOINT;
- ::GetCursorPos(lpoint);
- menu.CreatePopupMenu();
- menu.AppendMenu(MF_STRING,IDM_NF_MENU_EXIT,L"退出ReKey");
- menu.TrackPopupMenu(TPM_LEFTALIGN,lpoint->x,lpoint->y,this);
- menu.Detach();
- menu.DestroyMenu();
- delete lpoint;
- break;
- case WM_LBUTTONDBLCLK:
- this->ShowWindow(SW_SHOWNORMAL);
- SetForegroundWindow();
- break;
- default:
- break;
- }
- return 0;
- }
- //托盘菜单退出选项处理
- void CReKeyDlg::OnNFMenuExit()
- {
- this->SendMessage(WM_CLOSE);
- }
- void CReKeyDlg::OnSetWar3Path()
- {
- CFileDialog fileDLg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,_T("可执行文件(*.exe)|*.exe||"));
- if (fileDLg.DoModal() == IDOK)
- {
- //更改war3路径
- m_staticTooltip.SetWindowText(_T("<-点击图标启动War3"));
- g_war3InstallPath = fileDLg.GetPathName();
- }
- //维持g_war3InstallPath不变
- }
- void* GetProcessDllBase(DWORD dwPID, const CString& moduleName)
- {
- HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
- MODULEENTRY32 me32;
- hModuleSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,dwPID);
- if (hModuleSnap == INVALID_HANDLE_VALUE)
- {
- DWORD dwErr = GetLastError();
- return 0;
- }
- me32.dwSize = sizeof(MODULEENTRY32);
- if (!Module32First(hModuleSnap,&me32))
- {
- CloseHandle(hModuleSnap);
- return 0;
- }
- CString strModule;
- void* base=0;
- do
- {
- strModule = CString(me32.szModule);
- if (moduleName == strModule)
- {
- base = (void*)me32.modBaseAddr;
- break;
- }
- } while (Module32Next(hModuleSnap,&me32));
- CloseHandle(hModuleSnap);
- return base;
- }
- void CReKeyDlg::OnStnClickedStartwariii()
- {
- // 检查war3路径并设置
- if ( !g_war3InstallPath.GetLength() )
- {
- CFileDialog fileDLg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,_T("可执行文件(*.exe)|*.exe||"));
- if (fileDLg.DoModal() == IDOK)
- {
- m_staticTooltip.SetWindowText(_T("<-点击图标启动War3"));
- g_war3InstallPath = fileDLg.GetPathName();
- return ; //设置完毕不直接启动war3,感觉这样好些
- }
- else
- {
- return ; //没有设置路径,返回
- }
- }
- //war3已经启动,切换过去
- hWar3 = ::FindWindow(NULL,L"Warcraft III");
- if (hWar3)
- {
- ::ShowWindow(hWar3,SW_SHOWNORMAL);
- ::SetForegroundWindow(hWar3);
- return ;
- }
-
- //启动war3
- //g_war3InstallPath += _T(" -window");
- TCHAR szPath[MAX_PATH];
- TCHAR *pathBuff = g_war3InstallPath.GetBuffer(g_war3InstallPath.GetLength());
- _tcscpy_s(szPath,_countof(szPath),pathBuff);
- g_war3InstallPath.ReleaseBuffer();
- STARTUPINFO si;
- PROCESS_INFORMATION ppiProcess;
- memset(&si,0,sizeof(STARTUPINFO));
- si.cb = sizeof(STARTUPINFO);
- si.wShowWindow = SW_SHOWNORMAL;
- si.lpDesktop = NULL;
- si.dwFlags = STARTF_USESHOWWINDOW;
- if(!CreateProcess(NULL,szPath,NULL,NULL,TRUE,0,NULL,NULL,&si,&ppiProcess))
- {
- MessageBox(_T("War3进程创建失败!n"));
- return ;
- }
- CloseHandle(ppiProcess.hThread);
- CloseHandle(ppiProcess.hProcess);
- }
- void CReKeyDlg::OnBnClickedDebugswitch()
- {
- // TODO: 在此添加控件通知处理程序代码
- UpdateData(TRUE);
- //取得系统当前时间
- CString sTime;
- SYSTEMTIME st;
- GetLocalTime(&st);
- sTime.Format(_T("%d-%d-%d %d:%d:%d "),st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
- CString outLog;
- CString strValue;
- //创建输出信息
- if ( m_bIsDebug )
- {
- outLog = sTime + _T("Start Debug Outputn");
- strValue.Format(_T("%20s: 0x%08Xn%20s: 0x%08Xn%20s: 0x%08Xn"),
- _T("War3 Window Handle"),hWar3,
- _T("Hook Handle"),hHook,
- _T("Game.dll Addr"),(DWORD)g_GameDllbase);
- outLog += strValue;
- //负责打开文件
- g_DebugFile.Open(g_szOutDebugFile,CFile::modeWrite|CFile::modeCreate|CFile::modeNoTruncate|CFile::shareDenyWrite);
- g_DebugFile.SeekToEnd();
- //写入信息
- //g_DebugFile.WriteString((LPCTSTR)outLog);
- WriteDebugFile(outLog);
- }
- else
- {
- outLog = sTime + _T("Stop Debug Output.nn");
- //写入信息
- //g_DebugFile.WriteString((LPCTSTR)outLog);
- WriteDebugFile(outLog);
- //负责关闭文件
- if (g_DebugFile.m_pStream)
- {
- g_DebugFile.Close();
- }
- }
- }
- void WriteDebugFile(const CString &str)
- {
- //没有打开文件,返回
- if (NULL == g_DebugFile.m_pStream)
- return ;
-
- g_DebugFile.WriteString((LPCTSTR)str);
- g_DebugFile.Flush();
- }