find running program handle, knowing it's name and path

I need to be able to close a running program. I know how to do it, knowing the program, handle. to do so i can use FindHandle .. if I  KNOW the tittle in the program bar. but I only know the exe name and path..

how can I find the handle ?
LVL 3
bryan7Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
duke_nConnect With a Mentor Commented:
I got a way if you know the path:

Uses tlhelp32;
{ADD this to uses clause}

Function KillTask(FileName:String):integer;
//0-Couldn't Kill
var
    ContinueLoop:BOOL;
    FSnapshotHandle:THandle;
    FProcessEntry32:TProcessEntry32;
const
    PROCESS_TERMINATE=$0001;
begin
    FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
    FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
    ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32);
    while integer(ContinueLoop)<>0 do
    begin
        if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile))=UpperCase(FileName))
            or (UpperCase(FProcessEntry32.szExeFile)=UpperCase(FileName)))  then
          Result:=Integer(TerminateProcess(OpenProcess(PROCESS_TERMINATE,BOOL(0),          
                                    FProcessEntry32.th32ProcessID),0));
          ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
    end;
    CloseHandle(FSnapshotHandle);
end;

0
 
EdHillmannCommented:
There are two ways to go about this, depending on the operation system in which your app is going to run.

For Win9x and Win2000 environments, you want to use the CreateToolhelp32Snapshot to get a snapshot of the current processes.  Then you can use the Process32First and Process32Next functions to enumerate through the processes.  A data structure is returned as part of those functions which contains (among other things) the Handle.  I don't know if it contains the title of the program bar, but it does provide the path and name of the executable running.  I don't know if it's possible to get the title of the program bar icon with the processes handle, although I wouldn't be surprised if it were possible.

For WinNT flavors, I think the function you need to use is EnumWindows.  It's a bit more involved to use it.  My memory of the NT bit isn't very good at the moment, so I'll hesitate to attempt to describe how to do it in code examples.

In either case, check out MSDN online for information about the OS functions.  If there's an API, then there's a 98% chance that there's a stub for it in Delphi's Windows units.
0
 
MadshiCommented:
Hi guys,

yes, Ed is right, the toolhelp functions is the way to go - though, there's no handle in the process structure, at least not as far as I know.

Bryan, how about using my free unit "enumStuff.pas" from my homepage:

http://beam.to/madshi

With this unit you can simply enumerate processes in ALL systems. What you get there is the processID for a exefile string.

What kind of handle to you need? The process handle (for calling TerminateProcess) or the window handle of the main window (for posting a WM_CLOSE message)?

Regards, Madshi.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
MadshiCommented:
P.S: My unit works only for D4 or higher.
0
 
yk030299Commented:
listen
0
 
intheCommented:
i have made a utility to do this which should work on any system,(i think ;-)
it has 2 buttons

button 1: uses madshis enumstuff unit for nt5/win2000 ,win95,win98

button 2:
uses createtoolhelp32snapshot for nt3,nt4,win95,win98.
i use a opendialog to select a filename and if its running it will find it and kill it.
i am selling it for $20.00
(im kidding)
 if you want the project shout .
one day i make it with 1 button that checks for op system first then kills file using the appropriate method..
Regards Barry


0
 
MadshiCommented:
Barry, my enumStuff unit works for *EVERY* win32 system, including win95, win95osr2, win98, win98se, winNT3, winNT4, win2000.

While the toolhelp functions do *NOT* work for nt3 and nt4!!! The toolhelp functions are only available under win9x and win2000, but neither under winNT3 nor under winNT4.

So you can simply leave away your second button...    :-)

Regards, Madshi.
0
 
MadshiCommented:
P.S: *ALL* my stuff works for all systems...   :-)
0
 
intheCommented:
hi ,madshi,
first i know your enumstuff works on all systems :-)
the demo was to show what systems the toolhelp worked on and when i read your comments i was suprised cause i knew it (the button) worked on nt4 for a fact.
the buttons have a label above them that says what system it will work on
so i went back to the source code and
guess what ..
i had the labels above the wrong buttons  (so never assume the button on top is buton1 ;-)
0
 
bryan7Author Commented:
hey can I check it then ? I'll check it in win95 and NT 4 server

thanks
0
 
intheCommented:
leave your email address
i thought i had it but cant find it now.
download madshis enumstuff unit too save me sending everything..
0
 
bryan7Author Commented:
Aerisdata7@hotmail.com

oh... no.. I already have madshi's.. but I have Delphi 3 c/s
0
 
intheCommented:
well have sent see how it goes ;-)


0
 
MadshiCommented:
Hey bryan, time to update to D4 or D5...   :-)   Really, the new features are worth the price. I'm so into these new features that I even have no D3 support anymore in any of my units.
0
 
intheCommented:
hey madshi dont drop d4 support will ya ,cant afford d5 for a while yet :-)
0
 
MadshiCommented:
No, I never ever will drop D4 support - well, at least not in the next 2 weeks...   :-)

No no, the reason why I dropped D3 support was simply because I can't live without overloading and predefined parameters and dynamic arrays and int64 anymore...
0
 
bryan7Author Commented:
i tried it and i got lots of errors...
anything for D3 ?
0
 
MadshiCommented:
This is alright, but works not for NT3/NT4...
0
 
intheCommented:
this question was answered in about the second comment (my was only a demo) the points should be madshi's of course ;-)
0
 
MadshiCommented:
Well, thanx Barry, but I think my comment did not help too much, since my stuff doesn't work for D3...   :-(
0
 
duke_nCommented:
wait a sec,madshi!
I looked all over the helpfile for all the commands in my  answer and it says they work on [among others] NT4 or higher!
0
 
MadshiCommented:
No, duke_n, sorry, but the toolhelp functions work definetely only for win9x and win2000, NOT for winNT4. You can try it if you want. I know that for sure (I have all systems on my harddisk).
0
 
duke_nCommented:
WOW!
You're  just a damn multi-OS expert, hehe.

Unfortunately, I don't have the convenience of having all the OSes on my HD...
0
 
TAZICommented:
Hi...

Make use of WinSight32 .. I don't know if it comes with Delphi 3/4 but I make use of Delphi 5 and it is part of it.  You can debug to find the handle of all running applications.  You can get the name of the programs handle from there for example for MS Word it is "OpusApp"
To Find the Handle

var
   hndl : integer;
begin
hndl := FindWindow(PChar('OpusApp'), nil); //Gets Handle of MS WORD
end;

Regards
Tazi
0
All Courses

From novice to tech pro — start learning today.