Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » Can someone help me with the implementation of KillProcessByName function
Closed Thread
Old 07-22-2005, 03:11 PM   #1 (permalink)
 
Newb Techie

Join Date: Jul 2005

Posts: 3

Neophyte185

Question Can someone help me with the implementation of KillProcessByName function

My original question was: How do I access menu commands in one program from a Delphi Form?

I have figured out how to generate a simple Delphi 7 form with a button in it which will run an executable.

procedure TForm1.Button1Click(Sender: TObject);
begin
ShellExecute
(Form1.Handle, 'open', 'c:\E3238s\bin\E3238s.exe',
nil, nil, SW_SHOWNORMAL);
end;
end.

My problem is I need to know how to access the menus that are a part of this executable. For example how would I access the FILE menu in order to select EXIT.

In light of the lack of a simple solution I am considering a different approach. I will use the SHELLEXECUTABLE and a timer from Delphi in order to start the external program. After the timer runs out I would like to use KillProcessByName in order to exit the external program. So now can any one help me with the implementation and syntax for the KillProcessByName function? Thank You
Neophyte185 is offline  
Old 07-22-2005, 08:49 PM   #2 (permalink)
 
Ultra Techie

Join Date: Jul 2005

Posts: 530

TheHeadFL

Send a message via AIM to TheHeadFL
Default

OK, here is a rough description of how it works in C++ using WinAPI calls, so I am sure its similar in Delphi.

First you need to get the window handle of the target process, lets say you were trying to get the Window handle of AOL IM.

You tell WinAPI that you want it to enumerate all active windows for you and call your callback function for each:

Code:
EnumWindows(MyEnumWindows_CallbackFunc,NULL);
Then, for every window, your callback is called:

Code:
BOOL CALLBACK MyEnumWindows_CallbackFunc(HWND hwnd, LPARAM lParam)
{
CWnd *temp;
char buffer[255];

temp = CWnd::FromHandle(hwnd);
if (!temp) return 0;
GetClassName(hwnd,buffer,254);
// Check and see if buffer = the window you want, i.e. AIM_ChatWnd
// If it is, then you can grab the title bar like such:
temp->GetWindowText(buffer);
return 1;
}
Now that you have the hwnd to your target window, save it somewhere as a long unsigned int. (Pointer)

Then you have to find hWnd handles to each of the components of the menus or buttons within. You can do this with a program called Spy++.

So, lets say I wanted to grab a button from an AIM chat window and click it.

I could do:

Code:
hChatBtn = temp->FindWindowEx(temp->m_hWnd,NULL,"_Oscar_IconBtn",NULL);
if (!hChatBtn) return 0;

// Since you do this in the background you want to save the current focused window
hFocus = CWnd::GetFocus();

// Now you click it if you want by sending WinAPI messages
hChatBtn->SendMessage(WM_LBUTTONDOWN, 0, 0);
hChatBtn->SendMessage(WM_LBUTTONUP, 0, 0);

// Return the window focus
if (hFocus) hFocus->SetFocus();
You can use or manipulate any program this way as long as you can search through its Window handles in Spy++. Try it for yourself.

I hope this helps you with doing it in Delphi, even though this is C++ crap.
__________________
Desktop machine: 2 x Opteron 246, Asus K8N-DL, 2GB PC3200 ECC Reg., XFX GeForce 6600GT, 74gb WD Raptor, 2 x 19\" LCDs, Windows XP x64
Server machine: Intel P4 3.0GHz 2MB EM64T, ECS i865pe, 1GB PC3200, 36gb WD Raptor, Windows Server 2003
Laptop: Dell Inspiron 9100 (Intel P4 3.2GHz 1MB Prescott, i865pe, 512MB PC3200, Mobility Radeon 9700, DVD+R/DL Burner), Windows XP
Linux: P3 450Mhz, 386MB ram, Slackware 10.1 (Running mySQL/Apache)
TheHeadFL is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On