comment/*汇编语言实现的进程管理器，使用了 sysenter 进行快速系统调用
作者CPMorn.Dxmier QQ:233276111 Mail:3.vc@163.com */


.386
.model flat, stdcall  ;32 bit memory model
option casemap :none  ;case sensitive

include Win32Dlg.inc

.code

start:
	invoke GetModuleHandle,NULL
	mov	hInstance,eax
	invoke InitCommonControls
	invoke DialogBoxParam,hInstance,IDD_DIALOG1,NULL,addr DlgProc,NULL
	invoke ExitProcess,0
;########################################################################

DlgProc proc hWin:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
mov		eax,uMsg
.if eax==WM_INITDIALOG
	invoke InitDialog,hWin
.elseif eax==WM_COMMAND
	mov eax,wParam
	.if ax==IDB_BUTTON1002
		invoke   OnButton1002
	.elseif ax ==IDS_SUBMENU2 
		invoke OnMenu2
        .elseif ax==IDS_SUBMENU3
                invoke OnAbout
        .elseif ax==IDS_SUBMENU10
                mov bNeedRef,1
		invoke GetProcessList
		invoke CheckProcessEx
        .elseif ax==IDS_SUBMENU11   
                mov btRef,2
                mov btct,0
        .elseif ax==IDS_SUBMENU12   
                mov btRef,5
                mov btct,0
        .elseif ax==IDS_SUBMENU13 
                mov btRef,10
                mov btct,0
        .elseif ax==IDS_SUBMENU14
                mov btRef,20
                mov btct,0
        .elseif ax==IDS_SUBMENU15
                mov btRef,-1
                mov btct,0
	.endif
.elseif eax==WM_NOTIFY
	mov edx,lParam
        mov eax,[edx].NMHDR.hwndFrom
        .if eax==hList
                mov eax,[edx].NMHDR.code
		.if eax==NM_RCLICK
			invoke OnRClck
		.endif
        .endif 
.elseif eax==WM_TIMER
	invoke OnTimer
.elseif eax==WM_CLOSE
	invoke EndDialog,hWin,0
.elseif eax==WM_SIZE
	invoke OnSize
.else
	mov eax,FALSE
ret
.endif
mov eax,TRUE
ret
DlgProc endp

;--------------------------------------------------------------------------------------------------------------------------------------------------------------

UpdateProcessPrivilege proc 
local  hToken:HANDLE
local   hProcess:HANDLE
local destLuid:LUID 
local hTokenPrivileges:TOKEN_PRIVILEGES
invoke GetCurrentProcess
mov hProcess,eax
invoke OpenProcessToken,hProcess,TOKEN_ALL_ACCESS,addr hToken
      .if eax
            invoke LookupPrivilegeValue,0,offset szToken,addr destLuid
            .if eax
                  mov hTokenPrivileges.PrivilegeCount ,1;
                  mov hTokenPrivileges.Privileges[0].Attributes , SE_PRIVILEGE_ENABLED;
                  mov eax,destLuid.LowPart
                  mov hTokenPrivileges.Privileges[0].Luid.LowPart,eax
                  mov eax,destLuid.HighPart
                  mov hTokenPrivileges.Privileges[0].Luid.HighPart,eax
                  invoke AdjustTokenPrivileges,hToken,0,addr hTokenPrivileges,0,0,0
                  .if eax
                       jmp @exit   
                  .endif
            .endif
      .endif
xor eax,eax
@exit:
ret
UpdateProcessPrivilege endp

;--------------------------------------------------------------------------------------------------------------------------------------------------------------

InitDialog Proc hWin:HWND 
mov eax,hWin
mov hMain,eax
invoke LoadIconA,hInstance,IDI_ICON0
invoke SendMessageA,hWin,WM_SETICON,0,eax
invoke UpdateProcessPrivilege
invoke GetDlgItem,hWin,IDL_LISTVIEW1001
mov hList,eax
invoke SendMessage,hList, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT
invoke InsertItem,addr szColu3,245
invoke InsertItem,addr szColu2,75
invoke InsertItem,addr szColu1,150
invoke SetTimer,hWin,100,100,0
ret 
InitDialog endp 

;--------------------------------------------------------------------------------------------------------------------------------------------------------------

InsertItem proc szStr:dword ,len:dword 
local nItem:LV_COLUMN

mov eax,len
mov nItem.lx,eax
mov nItem.imask,LVCF_TEXT or LVCF_WIDTH
mov eax,szStr
mov nItem.pszText,eax
mov nItem.cchTextMax,256
mov nItem.fmt,0 
invoke SendMessageA,hList,LVM_INSERTCOLUMN,0,addr nItem 
ret
InsertItem endp 

;--------------------------------------------------------------------------------------------------------------------------------------------------------------

OnTimer proc 
inc btct
mov eax ,btct 
.if eax>=btRef
mov btct,0
mov bNeedRef,1
invoke GetProcessList
invoke CheckProcessEx
.endif
ret
OnTimer endp 

;--------------------------------------------------------------------------------------------------------------------------------------------------------------

GetProcessImageFile proc ProcessID:dword ,szPath:dword ,nSize:dword 
  local hProcess:dword 
  local hMod:HMODULE
  local rets:dword 
  invoke OpenProcess,PROCESS_ALL_ACCESS,0,ProcessID
  mov hProcess,eax
  .if eax!=0FFFFFFFFh
	invoke EnumProcessModules,hProcess,addr hMod,sizeof HMODULE,addr rets
	.if eax
	       invoke GetModuleFileNameExA,hProcess,hMod,szPath,nSize
        .endif
  .endif 
  invoke CloseHandle,hProcess
ret 
GetProcessImageFile endp 

;--------------------------------------------------------------------------------------------------------------------------------------------------------------

GetProcessList proc 
LOCAL snopt:HANDLE
local pList:PROCESSENTRY32
local szStr[320]:byte 
local nItem:dword 
local bnEnd:BOOL 
;int 3
invoke CreateToolhelp32Snapshot,TH32CS_SNAPALL,0
mov snopt,eax
mov pList.dwSize,sizeof PROCESSENTRY32
invoke Process32First,snopt,addr pList
mov bnEnd,eax 
@loops:
.if bnEnd
	invoke CheckProc,pList.th32ProcessID,addr pList.szExeFile
         .if eax==0 
                 invoke crt_sprintf,addr szStr,addr szFmt,pList.th32ProcessID
	         invoke InserItemText,0,addr pList.szExeFile
                 mov nItem,eax
                 invoke SetItemText,nItem,1,addr szStr 
                 lea  eax,szStr 
                 mov  byte ptr [eax],0
                 invoke GetProcessImageFile,pList.th32ProcessID,addr szStr ,320
                 invoke SetItemText,nItem,2,addr szStr 
         .endif
	invoke Process32Next,snopt,addr pList
        mov bnEnd,eax 
        jmp @loops
.endif
invoke CloseHandle,snopt
ret 
GetProcessList endp 

;--------------------------------------------------------------------------------------------------------------------------------------------------------------

GetItemText proc nID:dword ,nSub:dword ,szText:dword ,nSize:dword 
local lItem :LVITEM 
invoke crt_memset,addr lItem,0,sizeof LVITEM
mov eax,nID
mov lItem.iItem,eax 
mov eax,nSub
mov lItem.iSubItem,eax 
mov eax,szText
mov lItem.pszText,eax
mov eax,nSize
mov lItem.cchTextMax,eax
invoke SendMessageA,hList,LVM_GETITEMTEXTA,nID,addr lItem
ret 
GetItemText endp 

;--------------------------------------------------------------------------------------------------------------------------------------------------------------

InserItemText proc ,nSub:dword ,szText:dword 
local lItem :LVITEM
mov lItem.imask,LVIF_TEXT or LVIF_PARAM or LVIF_IMAGE or LVIF_STATE or LVCF_SUBITEM 
invoke SendMessageA,hList,LVM_GETITEMCOUNT,0,0
mov lItem.iItem,eax 
mov eax,nSub
mov lItem.iSubItem,eax 
mov eax,szText
mov lItem.pszText,eax
mov lItem.cchTextMax,256
mov lItem.state,LVIS_FOCUSED
invoke SendMessageA,hList,LVM_INSERTITEM,0,addr lItem
ret 
InserItemText endp 

;--------------------------------------------------------------------------------------------------------------------------------------------------------------

SetItemText proc nID:dword ,nSub:dword ,szText:dword 
local lItem :LVITEM
;int 3
mov lItem.imask,LVIF_TEXT or LVIF_PARAM or LVIF_IMAGE or LVIF_STATE or LVCF_SUBITEM 
mov eax,nID
mov lItem.iItem,eax 
mov eax,nSub
mov lItem.iSubItem,eax 
mov eax,szText
mov lItem.pszText,eax
mov lItem.cchTextMax,256
mov lItem.state,LVIS_SELECTED or LVIS_FOCUSED
invoke SendMessageA,hList,LVM_SETITEMTEXT,nID,addr lItem
ret 
SetItemText endp 

;--------------------------------------------------------------------------------------------------------------------------------------------------------------

CheckProc proc nPID:dword ,szStr:dword 
local nCount:dword 
local szName[256]:byte 
local pId[8]:byte 
;int 3
invoke SendMessageA,hList,LVM_GETITEMCOUNT,0,0
mov nCount,eax
mov nIDCount,eax
.if bNeedRef==1
	shl eax,2
	invoke crt_malloc,eax
	mov lOldPid,eax
	mov eax,nCount
	shl eax,2
	invoke crt_memset,lOldPid,0FFh,eax
        mov bNeedRef,2
.endif 
.while nCount>0
	dec nCount
	invoke GetItemText,nCount,1,addr pId,8
        invoke crt_atol, addr pId
        .if eax==nPID
		.if bNeedRef==2
			mov edx,lOldPid
			mov ecx,nCount
			shl ecx,2
			add edx,ecx    
			mov [edx],eax        
		.endif
        	invoke GetItemText,nCount,0,addr szName,256
                invoke crt__stricmp,addr szName,szStr
                .if eax==0
                        mov eax,1
                        jmp @exit;
                .endif
        .endif
.endw
xor eax,eax
@exit:
ret
CheckProc endp 

;--------------------------------------------------------------------------------------------------------------------------------------------------------------

CheckProcessEx Proc 
.while nIDCount>0
	mov eax,lOldPid
	dec nIDCount
	mov ecx,nIDCount
        shl ecx,2
        add eax,ecx
        .if dword ptr [eax] == 0FFFFFFFFh 
                invoke SendMessageA,hList,LVM_DELETEITEM,nIDCount,0
        .endif
.endw
invoke crt_free,lOldPid
mov lOldPid,0
ret 
CheckProcessEx endp 

;--------------------------------------------------------------------------------------------------------------------------------------------------------------

TerminateSelectItemProcess proc
local pId[8]:byte 
local nItem:dword 
local hProess:dword 
local ProcessId:dword 
invoke MessageBoxA,hMain,addr szQuest,addr szTitle,0134h 
.if eax!=6
jmp @exit 
.endif
invoke SendMessage,hList, LVM_GETNEXTITEM, -1, LVIS_SELECTED
.while eax 
	mov nItem,eax
	invoke GetItemText,nItem,1,addr pId,8
        invoke crt_atol,addr pId 
        .if ProcessId==eax
                jmp @exit
        .endif
        mov ProcessId,eax
	invoke OpenProcess,PROCESS_TERMINATE,0,eax
        mov hProess,eax 
        .if hProess!=0FFFFFFFFh
                invoke NtTerminateProcess,hProess,2
                invoke CloseHandle,hProess
        .endif
        invoke SendMessage,hList, LVM_GETNEXTITEM, nItem, LVIS_SELECTED
.endw
@exit:
xor eax,eax
ret
TerminateSelectItemProcess endp 

;--------------------------------------------------------------------------------------------------------------------------------------------------------------

OpenDirectoryForSelectProcess proc
local szPath[320]:byte 
local nItem:dword 
int 3
invoke SendMessage,hList, LVM_GETNEXTITEM, -1, LVIS_SELECTED
mov nItem,eax
.if nItem !=-1
	invoke crt_strncpy,addr szPath ,addr szSel,10
	invoke GetItemText,nItem,2,addr szPath +10,320
        invoke ShellExecute,0,addr szOpen,addr szExplorer,addr szPath,0,SW_SHOW
.endif
@exit:
xor eax,eax
ret
OpenDirectoryForSelectProcess endp 

;--------------------------------------------------------------------------------------------------------------------------------------------------------------

OnSize proc 
LOCAL hWRect :RECT
local hLRect :RECT
INVOKE GetClientRect,hMain,addr hWRect
;invoke ClientToScreen,hMain,addr hWRect
add hWRect.left,15
add hWRect.top,20
sub hWRect.right,30
sub hWRect.bottom,60
invoke SetWindowPos,hList,0,hWRect.left,hWRect.top,hWRect.right,hWRect.bottom,0
invoke GetDlgItem,hMain,IDB_BUTTON1002
mov hBtn,eax
sub hWRect.right,65
add hWRect.bottom,28
invoke SetWindowPos,hBtn,0,hWRect.right,hWRect.bottom,80,22,0

ret
OnSize endp  

;--------------------------------------------------------------------------------------------------------------------------------------------------------------

OnButton1002 proc
invoke TerminateSelectItemProcess
ret
OnButton1002 endp 

;--------------------------------------------------------------------------------------------------------------------------------------------------------------

OnMenu2 proc 
invoke OpenDirectoryForSelectProcess
ret
OnMenu2 endp

;--------------------------------------------------------------------------------------------------------------------------------------------------------------

OnRClck proc 
local pt:POINT 
local hSubMenu:HMENU
invoke GetCursorPos,addr pt
invoke GetMenu,hMain
invoke GetSubMenu,eax,0
mov hSubMenu,eax
invoke TrackPopupMenu,hSubMenu,TPM_LEFTALIGN or  TPM_RIGHTBUTTON,pt.x,pt.y,0,hMain,0
ret
OnRClck endp 

;--------------------------------------------------------------------------------------------------------------------------------------------------------------

AboutDlgProc proc hWin:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
	mov		eax,uMsg
	.if eax==WM_INITDIALOG   
	    invoke LoadIconA,hInstance,IDI_ICON0
	    invoke SendMessageA,hWin,WM_SETICON,0,eax
	.elseif eax==WM_COMMAND
         mov eax,wParam
         .if ax==IDB_BUTTON1001
                  invoke EndDialog,hWin,0
         .endif
	.elseif eax==WM_CLOSE
		invoke EndDialog,hWin,0
	.else
		mov		eax,FALSE
		ret
	.endif
	mov		eax,TRUE
	ret
AboutDlgProc endp

;--------------------------------------------------------------------------------------------------------------------------------------------------------------
sysenter macro
dw 340fh
endm 

sysexit macro
dw  350fh
endm 

KiFastSystemCall proc 
mov edx,esp
sysenter
ret
KiFastSystemCall endp

NtTerminateProcess proc hProcess:dword,uExitCode:dword 
pop ebp
mov eax,101h
call KiFastSystemCall
ret 
NtTerminateProcess endp 

OnAbout Proc 
invoke DialogBoxParam,hInstance,IDD_DIALOG0,hMain,addr AboutDlgProc,NULL
ret
OnAbout endp 

end start

