Process management for example - an SDK

  I have just learning SDK programming, to a part of the SDK for process management, but are not on-line, has had no choice but to write.    Inadequate, 

  Please expert guidance, Thank you! 

  Below is the code: 

  # Include <windows.h> 
  # Include <commdlg.h> 
  # Include <commctrl.h> 
  # Include "resource.h" 
  # Include <Tlhelp32.h> 
  # Include <stdio.h> 
  # Include "psapi.h" 
  BOOL Init (HWND); 
  Void RefreshProcess (HWND); 
  BOOL EndProcess (HWND); 
  LRESULT CALLBACK Test (HWND, UINT, WPARAM, LPARAM); 
  Int APIENTRY WinMain (HINSTANCE hInstance, 
  HINSTANCE hPrevInstance, 
  LPSTR lpCmdLine, 
  Int nCmdShow) 
  ( 
  / / TODO: Place code here. 
  InitCommonControls (); 

  DialogBox (NULL, (LPCTSTR) IDD_DIALOG, NULL, (DLGPROC) Test); 

  Return 1; 
  ) 

  LRESULT CALLBACK Test (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) 
  ( 

  Switch (message) 
  ( 
  Case WM_INITDIALOG: 
  Init (hDlg); 
  Return TRUE; 
  Case WM_COMMAND: 
  Switch (LOWORD (wParam)) 
  (Case IDCANCEL: 
  EndDialog (hDlg, 0); 
  KillTimer (hDlg, 1000); 
  Return TRUE; 
  Break; 
  Case IDOK: 
  Return (EndProcess (hDlg)); 
  Break; 
  Case IDREFRESH: 
  RefreshProcess (hDlg); 
  Return TRUE; 
  Break; 

  ) 

  ) 
  Return FALSE; 
  ) 

  /************************************************* ***********************/ 
  / * Set the import process parameters dialog handle * / 
  / * * / 
  /************************************************* ***********************/ 

  Void RefreshProcess (HWND hDlg) 
  ( 
  HANDLE hSnapshot, h_Process; 
  PROCESSENTRY32 pe; 
  Char lpszAdd [255]; 
  Int nIndex; 
  DWORD Memory; 
  LVITEM Lv; 
  PROCESS_MEMORY_COUNTERS PMC; 
  Pe.dwSize = sizeof (pe); 
  HSnapshot = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0); 
  Process32First (hSnapshot, & pe); 
  Sprintf (lpszAdd, "% s", pe.szExeFile); 
  Lv.mask = LVIF_TEXT; 
  Lv.iItem = 0; 
  Lv.iSubItem = 0; 
  Lv.pszText = lpszAdd; 
  SendDlgItemMessage (hDlg, IDC_LISTVIEW, LVM_DELETEALLITEMS, 0, 0); 
  SendDlgItemMessage (hDlg, IDC_LISTVIEW, LVM_INSERTITEM, 0, (LPARAM) & Lv); 
  Sprintf (lpszAdd, "% i", pe.th32ProcessID); 
  Lv.iSubItem = 1; 
  SendDlgItemMessage (hDlg, IDC_LISTVIEW, LVM_SETITEM, 0, (LPARAM) & Lv); 
  Sprintf (lpszAdd, "% i", pe.pcPriClassBase); 
  Lv.iSubItem = 2; 
  SendDlgItemMessage (hDlg, IDC_LISTVIEW, LVM_SETITEM, 0, (LPARAM) & Lv); 
  / / System process of memory is always 20 K 
  Memory = 20; 
  Sprintf (lpszAdd, "% 2i K", Memory); 
  Lv.iSubItem = 3; 
  SendDlgItemMessage (hDlg, IDC_LISTVIEW, LVM_SETITEM, 0, (LPARAM) & Lv); 
  While (Process32Next (hSnapshot, & pe)) 
  ( 
  Sprintf (lpszAdd, "% s", pe.szExeFile); 
  Lv.iSubItem = 0; 
  NIndex = SendDlgItemMessage (hDlg, IDC_LISTVIEW, LVM_INSERTITEM, 0, 
  (LPARAM) & Lv); 
  If (nIndex! = -1) 
  ( 

  Lv.iItem = nIndex; 
  Lv.iSubItem = 1; 
  Sprintf (lpszAdd, "% i", pe.th32ProcessID); 
  SendDlgItemMessage (hDlg, IDC_LISTVIEW, LVM_SETITEM, 0, 
  (LPARAM) & Lv); 
  Lv.iSubItem = 2; 
  Sprintf (lpszAdd, "% i", pe.pcPriClassBase); 
  SendDlgItemMessage (hDlg, IDC_LISTVIEW, LVM_SETITEM, 0, 
  (LPARAM) & Lv); 
  H_Process = OpenProcess (PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pe.th32ProcessID); 
  / / Get the process memory size 
  GetProcessMemoryInfo (h_Process, & PMC, sizeof (PMC)); 
  Memory = PMC.WorkingSetSize/1024; 
  Sprintf (lpszAdd, "% i K," Memory); 
  Lv.iSubItem = 3; 
  SendDlgItemMessage (hDlg, IDC_LISTVIEW, LVM_SETITEM, 0, (LPARAM) & Lv); 

  ) 
  ) 

  ) 

  / * 
  /************************************************* ***********************/ 
  / * Initialize function, mainly to add controls in the first list * / 
  /************************************************* ***********************/ 

  BOOL Init (HWND hDlg) 
  ( 
  LVCOLUMN lv; 
  Static char * name szText []={" image "," PID "," priorities "," memory "); 
  Static int cx 100,100,80,120 [] = (); 
  Int i = 0; 
  For (i = 0; i <4; i + +) 
  ( 
  Lv.mask = LVCF_TEXT | LVCF_WIDTH; 
  If (i <2) 
  Lv.fmt = LVCFMT_LEFT; 
  Else 
  Lv.fmt = LVCFMT_RIGHT; 
  Lv.cx = cx [i]; 
  Lv.pszText = szText [i]; 
  SendDlgItemMessage (hDlg, IDC_LISTVIEW, LVM_INSERTCOLUMN, (WPARAM) i, (LPARAM) & lv); 
  ) 
  SendDlgItemMessage (hDlg, IDC_LISTVIEW, LVM_SETEXTENDEDLISTVIEWSTYLE, (WPARAM) LVS_EX_FULLROWSELECT, (LPARAM) LVS_EX_FULLROWSELECT); 
  RefreshProcess (hDlg); 
  Return TRUE; 
  ) 

  /************************************************* ***********************/ 
  / * End of the process function, used to view the end of the list of the selected process * / 
  /************************************************* ***********************/ 

  BOOL EndProcess (HWND hDlg) 
  ( 
  Int hProcess = SendDlgItemMessage (hDlg, IDC_LISTVIEW, LVM_GETNEXTITEM, -1, (LPARAM) MAKELPARAM ((UINT) LVNI_SELECTED, 0)); 
  If (hProcess! = -1) 
  ( 
  LVITEM Lv; 
  Char Buffer [256]; 
  HANDLE h_process; 
  DWORD ProcessId; 
  BOOL iRet; 
  Memset (Buffer, 0, sizeof (Buffer)); 
  Lv.iSubItem = 1; 
  Lv.pszText = Buffer; 
  Lv.cchTextMax = sizeof (Buffer); 
  SendDlgItemMessage (hDlg, IDC_LISTVIEW, LVM_GETITEMTEXT, (WPARAM) hProcess, (LPARAM) & Lv); 
  ProcessId = atol (Buffer); 
  H_process = OpenProcess (PROCESS_TERMINATE, NULL, ProcessId); 
  If (h_process == NULL) 
  (MessageBox (hDlg, "the process can not be accessed," and "wrong", MB_OK); 
  Return FALSE; 
  ) 
  Else 
  ( 
  If (MessageBox (hDlg, "the end of this process?", "Warning", MB_OKCANCEL) == IDOK) 
  IRet = (TerminateProcess (h_process, 0); 
  If (! IRet) 
  (MessageBox (hDlg, "the end of the process of failure," and "wrong", MB_OK); 
  Return TRUE; 
  ) 
  Else 
  (MessageBox (hDlg, "the successful conclusion of the process", "successful", MB_OK); 
  Return FALSE; 
  ) 

  ) 
  Else 
  Return FALSE; 

  ) 

  ) 
  Else 
  (Return FALSE; 
  ) 
  ) 

Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Google
  • DotNetKicks
  • DZone
  • Furl
  • Netvouz

Releated Articles

  • Popuklar Articles

0 Comments to “Process management for example - an SDK”

No Comments. Send your comment.

Leave a Reply

You must be logged in to post a comment.