Closing Window

I wanna make my own window explorer.
I run every executable files (EXEs) with WINEXEC statement.
The question is that I can't close the program I've called with WinExec. Any experts know how to do it?
First, I've used "FindWindow" (API function) to get the window handle.
Second, I've used "CloseWindow" (API function), but it's just minimized.
Third, I've used "DestroyWindow" (API function), but when I called GetLastError, it returned error number 126 which indicated that the module does not exist.

How to solve this problem?

Thanks for the attention,

Who is Participating?
simonetConnect With a Mentor Commented:
You're doing it wrong.

First of all, you shouldn't be using WinExec to run the applications. In your specific case, you should be using CreateProcess.

When CreateProcess is called, it returns an identifier for the process created. Then you save that process (in a TList, for example, along with more information about the process) and, whenever you need to terminate that application, you just call TerminateProcess with the identifier returned by CreateProcess.

For your specific purpose, you sholdn't be doing it using FindWindow, because a process' main window may not be showing or the process can be entirely "windowless".

Using CreateProcess gives you total control on how the application will be started, but it's a pain in the ass to use. So I posted here a small function that encapsulates the call to CreateProcess. It's not the definite solution, because you'll have to adapt it to your specific needs, although it's pretty good to get anyone started:

function ExecCommandLine(CommandLine: string): TProcessInformation;
var StartUpInfos: TStartUpInfo;
  StartUpInfos.cb:= SizeOf(StartUpInfos);
  StartUpInfos.lpReserved:= nil;
  StartUpInfos.lpDesktop:= nil;
  StartUpInfos.lpTitle:= nil;
  StartUpInfos.dwFlags:= 0;
  StartUpInfos.cbReserved2:= 0;
  StartUpInfos.lpReserved2:= nil;
  CreateProcess(nil, PChar(CommandLine), nil, nil,
    nil, nil, StartUpInfos, Result);


Athena's Place:
Have you tried sending the WM_QUIT message to the handle you got with FindWindow()?
daniel_cAuthor Commented:
Thanks Alex for your help!!!
I will try it immediately!
Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

daniel_cAuthor Commented:
Hi Alex, there's one problem.
How can I get the window handle from your function?

You don't need the window handle. Just use the TProcessInformation record to get information about the running process (including its PID, which you can use to call TerminateProcess).

Now, if you really need the Window handle, you can use the EnumThreadWindows with the thread identifier for the process that is returned in the TProcessInformation record.


daniel_cAuthor Commented:
Here's the problem why I need Window handle:
1. I use your createprocess to show the window
2. The user minimizes the window.
3. When user double click again the program name (I've make program like Window Explorer), I want to show the window that user minimized, instead of executing the program again.

That's the problem Alex.
Need your help...


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.