NeighborInfoDlg.cpp
上传用户:jnhwc1
上传日期:2022-07-09
资源大小:100k
文件大小:14k
源码类别:

进程与线程

开发平台:

Visual C++

  1. // NeighborInfoDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "NeighborInfo.h"
  5. #include "NeighborInfoDlg.h"
  6. #include "resource.h"
  7. #include "TLHELP32.H"
  8. #include "psapi.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. #define NBTSTATPORT 137 //nbtstat name port
  15. #define OWNERPORT 4321
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CAboutDlg dialog used for App About
  18. //--------------------------------var-------------------------------
  19. BYTE bs[50]=
  20. {0x0,0x00,0x0,0x10,0x0,0x1,0x0,0x0,0x0,0x0,0x0,0x0,0x20,0x43,0x4b,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x0,0x0,0x21,0x0,0x1};
  21. HANDLE wait_handle;
  22. UINT NbtstatThread(LPVOID param);
  23. class CAboutDlg : public CDialog
  24. {
  25. public:
  26. CAboutDlg();
  27. // Dialog Data
  28. //{{AFX_DATA(CAboutDlg)
  29. enum { IDD = IDD_ABOUTBOX };
  30. //}}AFX_DATA
  31. // ClassWizard generated virtual function overrides
  32. //{{AFX_VIRTUAL(CAboutDlg)
  33. protected:
  34. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  35. //}}AFX_VIRTUAL
  36. // Implementation
  37. protected:
  38. //{{AFX_MSG(CAboutDlg)
  39. //}}AFX_MSG
  40. DECLARE_MESSAGE_MAP()
  41. };
  42. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  43. {
  44. //{{AFX_DATA_INIT(CAboutDlg)
  45. //}}AFX_DATA_INIT
  46. }
  47. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  48. {
  49. CDialog::DoDataExchange(pDX);
  50. //{{AFX_DATA_MAP(CAboutDlg)
  51. //}}AFX_DATA_MAP
  52. }
  53. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  54. //{{AFX_MSG_MAP(CAboutDlg)
  55. // No message handlers
  56. //}}AFX_MSG_MAP
  57. END_MESSAGE_MAP()
  58. /////////////////////////////////////////////////////////////////////////////
  59. // CNeighborInfoDlg dialog
  60. CNeighborInfoDlg::CNeighborInfoDlg(CWnd* pParent /*=NULL*/)
  61. : CDialog(CNeighborInfoDlg::IDD, pParent)
  62. {
  63. //{{AFX_DATA_INIT(CNeighborInfoDlg)
  64. //}}AFX_DATA_INIT
  65. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  66. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  67. }
  68. void CNeighborInfoDlg::DoDataExchange(CDataExchange* pDX)
  69. {
  70. CDialog::DoDataExchange(pDX);
  71. //{{AFX_DATA_MAP(CNeighborInfoDlg)
  72. DDX_Control(pDX, IDC_LISTPROCSS, m_ListProcess);
  73. DDX_Control(pDX, IDC_IPADDRESS1, m_123);
  74. DDX_Control(pDX, IDC_LISTINFOR, m_ListView);
  75. //}}AFX_DATA_MAP
  76. }
  77. BEGIN_MESSAGE_MAP(CNeighborInfoDlg, CDialog)
  78. ON_WM_CONTEXTMENU()
  79. //{{AFX_MSG_MAP(CNeighborInfoDlg)
  80. ON_WM_SYSCOMMAND()
  81. ON_WM_PAINT()
  82. ON_WM_QUERYDRAGICON()
  83. ON_BN_CLICKED(IDC_BEGINSCAN, OnBeginScan)
  84. ON_BN_CLICKED(IDC_STOPSCAN, OnStopScan)
  85. ON_BN_CLICKED(IDC_MYHELP, OnMyHelp)
  86. ON_BN_CLICKED(IDC_EMPTY, OnEmpty)
  87. ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
  88. ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
  89. ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
  90. ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
  91. ON_WM_TIMER()
  92. //}}AFX_MSG_MAP
  93. END_MESSAGE_MAP()
  94. /////////////////////////////////////////////////////////////////////////////
  95. // CNeighborInfoDlg message handlers
  96. BOOL CNeighborInfoDlg::OnInitDialog()
  97. {
  98. CDialog::OnInitDialog();
  99. // Add "About..." menu item to system menu.
  100. // IDM_ABOUTBOX must be in the system command range.
  101. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  102. ASSERT(IDM_ABOUTBOX < 0xF000);
  103. CMenu* pSysMenu = GetSystemMenu(FALSE);
  104. if (pSysMenu != NULL)
  105. {
  106. CString strAboutMenu;
  107. strAboutMenu.LoadString(IDS_ABOUTBOX);
  108. if (!strAboutMenu.IsEmpty())
  109. {
  110. pSysMenu->AppendMenu(MF_SEPARATOR);
  111. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  112. }
  113. }
  114. // Set the icon for this dialog.  The framework does this automatically
  115. //  when the application's main window is not a dialog
  116. SetIcon(m_hIcon, TRUE); // Set big icon
  117. SetIcon(m_hIcon, FALSE); // Set small icon
  118. // TODO: Add extra initialization here
  119. wait_handle=CreateEvent(NULL,true,false,"receive data");
  120. GetDlgItem(IDC_STOPSCAN)->EnableWindow(false);
  121. DWORD dwStyle=GetWindowLong(m_ListView.GetSafeHwnd(),GWL_STYLE);
  122. dwStyle&=~LVS_TYPEMASK;
  123. dwStyle|=LVS_REPORT;
  124. SetWindowLong(m_ListView.GetSafeHwnd(),GWL_STYLE,dwStyle);
  125. SetWindowLong(m_ListProcess.GetSafeHwnd(),GWL_STYLE,dwStyle);
  126. //初始化视图列表1
  127.     m_ListView.InsertColumn(3,"MAC地址",LVCFMT_LEFT,127);
  128. m_ListView.InsertColumn(2,"主机",LVCFMT_LEFT,127);
  129. m_ListView.InsertColumn(1,"工作组",LVCFMT_LEFT,128);
  130. m_ListView.InsertColumn(0,"IP地址",LVCFMT_LEFT,128); 
  131. m_ListView.SetExtendedStyle(LVS_EX_GRIDLINES);
  132.     ::SendMessage(m_ListView.m_hWnd, LVM_SETEXTENDEDLISTVIEWSTYLE,
  133.       LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);
  134. //初始化视图列表2
  135. m_ListProcess.InsertColumn(0,"进程",LVCFMT_LEFT,127);
  136. m_ListProcess.InsertColumn(1,"ID",LVCFMT_LEFT,127);
  137. m_ListProcess.InsertColumn(2,"父进程",LVCFMT_LEFT,128);
  138. m_ListProcess.SetExtendedStyle(LVS_EX_GRIDLINES);
  139.     ::SendMessage(m_ListProcess.m_hWnd, LVM_SETEXTENDEDLISTVIEWSTYLE,
  140. LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);
  141. //初始化端口
  142. if(AfxSocketInit(NULL)==FALSE)
  143. {
  144. AfxMessageBox("Error Init Socket");
  145. }
  146. m_socket.Init(this);
  147. if(m_socket.Create(OWNERPORT,SOCK_DGRAM)==FALSE)
  148. {
  149. AfxMessageBox("Socket Create Error");
  150. }
  151.     
  152. //设置计时器
  153. SetTimer(0,100,NULL);
  154. m_bStop=FALSE;
  155. return TRUE;  // return TRUE  unless you set the focus to a control
  156. }
  157. void CNeighborInfoDlg::OnSysCommand(UINT nID, LPARAM lParam)
  158. {
  159. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  160. {
  161. CAboutDlg dlgAbout;
  162. dlgAbout.DoModal();
  163. }
  164. else
  165. {
  166. CDialog::OnSysCommand(nID, lParam);
  167. }
  168. }
  169. // If you add a minimize button to your dialog, you will need the code below
  170. //  to draw the icon.  For MFC applications using the document/view model,
  171. //  this is automatically done for you by the framework.
  172. void CNeighborInfoDlg::OnPaint() 
  173. {
  174. if (IsIconic())
  175. {
  176. CPaintDC dc(this); // device context for painting
  177. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  178. // Center icon in client rectangle
  179. int cxIcon = GetSystemMetrics(SM_CXICON);
  180. int cyIcon = GetSystemMetrics(SM_CYICON);
  181. CRect rect;
  182. GetClientRect(&rect);
  183. int x = (rect.Width() - cxIcon + 1) / 2;
  184. int y = (rect.Height() - cyIcon + 1) / 2;
  185. // Draw the icon
  186. dc.DrawIcon(x, y, m_hIcon);
  187. }
  188. else
  189. {
  190. CDialog::OnPaint();
  191. }
  192. }
  193. // The system calls this to obtain the cursor to display while the user drags
  194. //  the minimized window.
  195. HCURSOR CNeighborInfoDlg::OnQueryDragIcon()
  196. {
  197. return (HCURSOR) m_hIcon;
  198. }
  199. void CNeighborInfoDlg::OnBeginScan() 
  200. {
  201. GetDlgItem(IDC_BEGINSCAN)->EnableWindow(FALSE);
  202. GetDlgItem(IDC_STOPSCAN)->EnableWindow(TRUE);
  203. //开始扫描
  204. AfxBeginThread(NbtstatThread,(LPVOID)this,THREAD_PRIORITY_NORMAL);
  205. }
  206. void CNeighborInfoDlg::OnStopScan() 
  207. {
  208. // TODO: Add your control notification handler code here
  209. m_bStop=TRUE;
  210. }
  211. void CNeighborInfoDlg::ReceiveMessage()
  212. {
  213. BYTE Buf[500];
  214. static CString strOldIP;
  215. CString str,strIP,strHost,strHex,strMac,Host,Group;
  216. UINT dport;
  217. m_socket.ReceiveFrom(Buf,500,strIP,dport,0);
  218. if(strIP==(char)NULL||strIP==strOldIP)return;
  219. strOldIP=strIP;
  220. //ip
  221. int index=m_ListView.InsertItem(0,strIP);
  222. strHost="";
  223. strHex="";
  224. Host="\";
  225. int tem=0,num=0;
  226. bool bAdd=true;
  227. for(int i=0;i<500;i++)
  228. {
  229. if(i%50==0)
  230. TRACE("n");
  231. TRACE("%c",Buf[i]);
  232. }
  233. //nbtstat前面的57个字节是
  234. for(i=57;i<500;i++) //57-72
  235. {
  236. //end
  237. if(Buf[i]==0xcc)
  238. break; 
  239. if(Buf[i]==0x20)
  240. bAdd=false;
  241. if(bAdd)
  242. {
  243. str.Format("%c",Buf[i]);
  244. if(Buf[i]>=' ')strHost+=str;
  245. str.Format("%02x.",Buf[i]);
  246. strHex+=str;
  247. }
  248. if((++tem)%18==0)
  249. {
  250.             bAdd=true; 
  251. strHost.TrimRight((char)NULL);
  252. if(strHost=="")
  253. {
  254.     strMac.Delete(17,strMac.GetLength()-17);
  255. m_ListView.SetItem(index,3,LVIF_TEXT,strMac, 0, 0, 0,0);
  256. break;
  257. }
  258. if(num==0&&strHost!="")
  259. {
  260. m_ListView.SetItem(index,2,LVIF_TEXT,strHost, 0, 0, 0,0);
  261. Host=strHost;
  262. num++;
  263. }
  264. else
  265. {
  266. if(Host!=strHost&&num==1&&strHost!="")
  267. {
  268. m_ListView.SetItem(index,1,LVIF_TEXT,strHost, 0, 0, 0,0);
  269. Group=strHost;
  270.     num++;
  271. }
  272. }
  273. strMac=strHex;
  274. strHost="";
  275.      strHex="";
  276. }
  277. }
  278. SetEvent(wait_handle);
  279. }
  280. void CNeighborInfoDlg::OnMyHelp() 
  281. {
  282. // TODO: Add your control notification handler code here
  283. CDialog dlg(IDD_ABOUTBOX);
  284. dlg.DoModal();
  285. }
  286. //--------------------------nbtstat线程----------------------
  287. UINT NbtstatThread(LPVOID param)
  288. {
  289. CNeighborInfoDlg * dlg=(CNeighborInfoDlg *)param;
  290. BYTE begin[4],end[4];
  291. begin[0]=192;
  292. begin[1]=168;
  293. begin[2]=1;
  294. begin[3]=100;
  295. end[0]=192;
  296. end[1]=168;
  297. end[2]=1;
  298. end[3]=200;
  299. CString ip;
  300. do
  301. {
  302. if(dlg->m_bStop)
  303. {
  304. AfxMessageBox("终止扫描!");
  305. dlg->GetDlgItem(IDC_STOPSCAN)->EnableWindow(false);
  306.         dlg->GetDlgItem(IDC_BEGINSCAN)->EnableWindow(true);
  307. dlg->m_bStop=FALSE;
  308. return 1;
  309. }
  310. ip.Format("%d.%d.%d.%d",begin[0],begin[1],begin[2],begin[3]);
  311. if(begin[3]!=0&&begin[2]!=0)
  312. dlg->m_socket.SendTo((void*)bs,50,NBTSTATPORT,ip,0);
  313.   WaitForSingleObject(wait_handle,100);
  314. ResetEvent(wait_handle);
  315. //=============================================
  316. if(begin[2]<=end[2])
  317. {
  318.    if(begin[3]<end[3])
  319.    begin[3]++;
  320.    else if(begin[2]<end[2]&&begin[3]<255)
  321.    begin[3]++;
  322.    else if(begin[2]<end[2]&&begin[3]==255)
  323.    {
  324.    begin[3]=0;
  325.    begin[2]++;
  326.    }
  327. }
  328. else 
  329. break;
  330. if(begin[3]>=end[3]&&begin[2]>=end[2])
  331. break;
  332. }while(begin[2]<=255&&begin[3]<=255);
  333. dlg->GetDlgItem(IDC_STOPSCAN)->EnableWindow(false);
  334. dlg->GetDlgItem(IDC_BEGINSCAN)->EnableWindow(true);
  335. return 0;
  336. }
  337. //-----------------------------------------------------------
  338. void CNeighborInfoDlg::OnEmpty() 
  339. {
  340. m_ListView.DeleteAllItems();
  341. }
  342. void CNeighborInfoDlg::OnContextMenu(CWnd*, CPoint point)
  343. {
  344. // CG: This block was added by the Pop-up Menu component { if (point.x == -1 && point.y == -1){ //keystroke invocation CRect rect; GetClientRect(rect); ClientToScreen(rect); point = rect.TopLeft(); point.Offset(5, 5); } CMenu menu; VERIFY(menu.LoadMenu(CG_IDR_POPUP_NEIGHBOR_INFO_DLG)); CMenu* pPopup = menu.GetSubMenu(0); ASSERT(pPopup != NULL); CWnd* pWndPopupOwner = this; while (pWndPopupOwner->GetStyle() & WS_CHILD) pWndPopupOwner = pWndPopupOwner->GetParent(); pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, pWndPopupOwner); }
  345. }
  346. void CNeighborInfoDlg::OnButton2() 
  347. {
  348. POSITION pos = m_ListView.GetFirstSelectedItemPosition();
  349. if(pos)
  350. {
  351. int nItem = m_ListView.GetNextSelectedItem(pos);
  352. CString strUserinfo;
  353. strUserinfo.Format("IP地址:%s",
  354. m_ListView.GetItemText(nItem,0));
  355. GetDlgItem(IDC_IP)->SetWindowText(strUserinfo);
  356. }
  357. }
  358. void CNeighborInfoDlg::OnButton3() 
  359. {
  360. POSITION pos = m_ListView.GetFirstSelectedItemPosition();
  361. if(pos)
  362. {
  363. int nItem = m_ListView.GetNextSelectedItem(pos);
  364. CString   strIP;
  365. strIP.Format("%s",m_ListView.GetItemText(nItem,0));
  366. DWORD   dwIP;   
  367. dwIP   =   inet_addr(strIP);   
  368. unsigned   char   *pIP   =   (unsigned   char*)&dwIP;   
  369.     m_123.SetAddress(*pIP,   *(pIP+1),   *(pIP+2),   *(pIP+3));
  370. }
  371. }
  372. void CNeighborInfoDlg::OnButton4() 
  373. {
  374. m_ListProcess.DeleteAllItems();
  375. HANDLE toolhelp=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);//使用必须加上KERNEL32.LIB链接库
  376. if(toolhelp==NULL)
  377. {
  378. AfxMessageBox("扫描进程失败!!");
  379. }
  380. m_ListProcess.SetRedraw(FALSE);
  381. PROCESSENTRY32 processinfo;
  382. int i=0;
  383. CString str;
  384. BOOL start=Process32First(toolhelp,&processinfo);
  385. while(start)
  386. {
  387. m_ListProcess.InsertItem(i,"");
  388. m_ListProcess.SetItemText(i,0,processinfo.szExeFile);
  389. str.Format("%08x",processinfo.th32ProcessID);
  390. m_ListProcess.SetItemText(i,1,str);
  391. str.Format("%08x",processinfo.th32ParentProcessID);
  392. m_ListProcess.SetItemText(i,2,str);
  393. start=Process32Next(toolhelp,&processinfo);
  394. i++;
  395. }
  396. m_ListProcess.SetRedraw(TRUE);
  397. }
  398. //查找进程
  399. DWORD CNeighborInfoDlg::FindProcess(char *strProcessName)
  400. {
  401.     DWORD aProcesses[1024], cbNeeded, cbMNeeded;
  402.     HMODULE hMods[1024];
  403.     HANDLE hProcess;
  404.     char szProcessName[MAX_PATH];
  405.     if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))  
  406. {
  407. return 0;
  408. }
  409.     for(int i=0; i< (int) (cbNeeded / sizeof(DWORD)); i++)
  410.     {
  411.         hProcess = OpenProcess(  PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, aProcesses[i]);
  412.         EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbMNeeded);
  413.         GetModuleFileNameEx( hProcess, hMods[0], szProcessName,sizeof(szProcessName));
  414.         
  415.         if(strstr(szProcessName, strProcessName))
  416.         {
  417.             return(aProcesses[i]);
  418.         }
  419.     }
  420.     return 0;
  421. }
  422. //查杀进程
  423. void CNeighborInfoDlg::KillProcess()
  424. {
  425. HANDLE hYourTargetProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, FindProcess("gamemd.exe"));
  426. if(hYourTargetProcess == NULL)
  427. {
  428. return;
  429. }
  430. GetDebugPriv();//提高权限
  431. TerminateProcess(hYourTargetProcess,0);
  432.     MessageBox("没有辰爷的允许禁止运行红警游戏-.-","警告",MB_ICONEXCLAMATION|MB_OK); 
  433.     return;
  434. }
  435. //提高权限
  436. BOOL CNeighborInfoDlg::GetDebugPriv()
  437. {
  438. HANDLE hToken;
  439. LUID sedebugnameValue;
  440. TOKEN_PRIVILEGES tkp;
  441. if (!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &hToken))
  442.     {
  443. return FALSE;
  444.     }
  445. if (!LookupPrivilegeValue(NULL,SE_DEBUG_NAME,&sedebugnameValue))
  446. {
  447. CloseHandle( hToken );
  448. return FALSE;
  449. }
  450. tkp.PrivilegeCount = 1;
  451. tkp.Privileges[0].Luid = sedebugnameValue;
  452. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  453. if (!AdjustTokenPrivileges( hToken, FALSE,&tkp,sizeof tkp, NULL,NULL))
  454.     {
  455.         CloseHandle( hToken );
  456.         return FALSE;
  457.     }
  458.     return TRUE;
  459. }
  460. void CNeighborInfoDlg::OnButton5() 
  461. {
  462.    KillProcess();
  463. }
  464. void CNeighborInfoDlg::OnTimer(UINT nIDEvent) 
  465. {
  466.     KillProcess();
  467. CDialog::OnTimer(nIDEvent);
  468. }