Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Closing Window

Posted on 1999-07-21
6
Medium Priority
?
167 Views
Last Modified: 2010-04-04
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,


Daniel
0
Comment
Question by:daniel_c
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 

Expert Comment

by:sandman_br
ID: 1390230
Have you tried sending the WM_QUIT message to the handle you got with FindWindow()?
0
 
LVL 15

Accepted Solution

by:
simonet earned 200 total points
ID: 1390231
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;
begin
  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,
    False, CREATE_DEFAULT_ERROR_MODE + CREATE_NEW_CONSOLE +
NORMAL_PRIORITY_CLASS,
    nil, nil, StartUpInfos, Result);
end;

Yours,

Alex
Athena's Place: http://www.bhnet.com.br/~simonet
0
 
LVL 7

Author Comment

by:daniel_c
ID: 1390232
Wow!!!
Thanks Alex for your help!!!
I will try it immediately!
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 7

Author Comment

by:daniel_c
ID: 1390233
Hi Alex, there's one problem.
How can I get the window handle from your function?

Thanks
0
 
LVL 15

Expert Comment

by:simonet
ID: 1390234
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.

Yours,

Alex
0
 
LVL 7

Author Comment

by:daniel_c
ID: 1390235
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...

Thanks

Daniel
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question