September 26, 2022, 06:19:52 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Messing with other processes

Started by WayneA, January 04, 2009, 08:12:06 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

WayneA

I plan to start experimenting with accessing other processes as part of my long on-hold project iHexMemory (hex editor/debugger for apps in memory). So I threw this together to help speed up the implementation of proof of concepts (for functions I write). It's nothing to special itself, but I think it's a good platform for anyone who wants to do something similar to iHexMem. Here you are:

#autodefine "off"
#define GW_HWNDNEXT 2
#define GW_HWNDFIRST 0
#define PROCESS_ALL_ACCESS 0x1F0FFF
import int GetForegroundWindow();
import int GetWindow(int hWnd,unsigned int uCmd);
import unsigned int GetWindowThreadProcessId(int hWnd,unsigned int *lpdwProcessId);
import int GetWindowText alias GetWindowTextA(int hWnd,string lpString,int nMaxCount);
import int OpenProcess(int dwDesiredAccess,int bInheritHandle,int dwProcessId);
import int TerminateProcess(int hProcess,int uExitCode);
import int FlashWindow(int hWnd,int bInvert);
import int GetSysColor(int nIndex);

//{ MainWindow
class MainWindow:CWindow{
declare virtual OnClose(),int;
declare virtual OnControl(int nID,int nNotifyCode,unsigned int hControl),int;
declare virtual OnCreate(),int;
CListBox lstWindows;
CButton chkBlnkTitles;
CComboBox cboActions;
CButton btnAction;
int width;
int height;
}

MainWindow::OnClose(),int{
Destroy();
return 0;
}

MainWindow::OnControl(int nID,int nNotifyCode,unsigned int hControl),int{
switch(nID){
case 2:
if(nNotifyCode=0){
lstWindows.ResetContent();
wnd_pop_lstbox(lstWindows,chkBlnkTitles.GetCheck());
}
case 4:
if(nNotifyCode=0){
if(cboActions.GetItemText(cboActions.GetCurSel())="kill"){
int selcnt=0;
for(int i=0;i<lstWindows.GetCount()-1;i++){
if(selcnt=lstWindows.GetSelCount())
break;
if(lstWindows.GetSel(i)>0){
selcnt++;
prockill(extractpid(lstWindows,i));
}
}
} else if(cboActions.GetItemText(cboActions.GetCurSel())="flash"){
selcnt=0;
for(i=0;i<lstWindows.GetCount()-1;i++){
if(selcnt=lstWindows.GetSelCount())
break;
if(lstWindows.GetSel(i)>0){
selcnt++;
wndflash(extracthwnd(lstWindows,i));
}
}
} else {
MessageBox(this,cboActions.GetItemText(cboActions.GetCurSel()),"Item Selected",MB_OK);
}
}
}
return 0;
}

MainWindow::OnCreate(),int{
RECT r=GetClientRect();
width=r.right;
height=r.bottom;
SetWindowColor(GetSysColor(15));
CenterWindow();
lstWindows.Create(5,5,width-10,height-50,ALBS_USETABSTOPS|ALBS_MULTIPLESEL|AWS_TABSTOP|AWS_VSCROLL|AWS_HSCROLL|AWS_VISIBLE,1,"",this);
chkBlnkTitles.Create(width-190,height-35,175,25,ABS_CHECKBOX|AWS_VISIBLE|AWS_TABSTOP,2,"Include windows with no titles?",this);
cboActions.Create(5,height-35,175,128,ACBS_AUTOHSCROLL|ACBS_DROPDOWN|AWS_HSCROLL|AWS_TABSTOP|AWS_VISIBLE,3,"",this);
btnAction.Create(180,height-35,50,26,AWS_TABSTOP|AWS_VISIBLE,4,"Go",this);
for(int i=1;i<5;i++){
SetFont("Gill Sans MT Condensed",null,null,null,i);
}
wnd_pop_lstbox(lstWindows,chkBlnkTitles.GetCheck());
cboActions.AddString("kill");
cboActions.AddString("flash");
return 0;
}

//}

global sub main(){
MainWindow wndMain;
wndMain.Create(0,0,550,250,AWS_CAPTION|AWS_VISIBLE|AWS_BORDER|AWS_SYSMENU|AWS_MINIMIZEBOX,null,"Window List",null);
do{
wait();
}until(wndMain.m_hwnd=0);
return;
}

sub wnd_pop_lstbox(CListBox box,opt int inc_blank_titles=false){
int hwnd,pwnd;
string title="";
hwnd=GetWindow(GetForegroundWindow(),GW_HWNDFIRST);
while(hwnd){
GetWindowThreadProcessId(hwnd,pwnd);
GetWindowText(hwnd,title,75);
if((title="" AND inc_blank_titles=true) OR (title!="")){
if(title=""){
title="blank";
}
box.AddString(using("PID:#\tHWND:#\tTXT:",pwnd,hwnd)+title);
}
hwnd=GetWindow(hwnd,GW_HWNDNEXT);
}
return;
}

sub prockill(int pid),int{
int pnd=OpenProcess(PROCESS_ALL_ACCESS,null,pid);
return TerminateProcess(pnd,null);
}

sub wndflash(int hWnd),int{
return FlashWindow(hWnd,true);
}

sub extractpid(CListBox ctrl,int item),int{
return strtonum(nthfield(nthfield(ctrl.GetItemText(item),"\t",1),":",2));
}

sub extracthwnd(CListBox ctrl,int item),int{
return strtonum(nthfield(nthfield(ctrl.GetItemText(item),":",3),"\t",1));
}

sub nthfield(string src,string delimiter,int fldnum),string{
int dlpos=0,nxpos=strfind(src,delimiter),cnt=1;
while(nxpos){
if(cnt=fldnum){
if(cnt=1)
dlpos=1-len(delimiter);
return strmid(src,dlpos+len(delimiter),nxpos-dlpos-len(delimiter));
} else {
dlpos=nxpos;
nxpos=strfind(src,delimiter,dlpos+1);
cnt++;
}
}
return strmid(src,dlpos+len(delimiter));
}

99 little bugs in the code,
99 bugs in the code,
Fix one bug,
Compile again,
104 little bugs in the code...

All code I post is in the public domain.

Haim

WayneA,
Theres much to learn from this sample (for me  :) ).
Thanks for sharing it.

Haim

pistol350

Regards,

Peter B.