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

x
?
Solved

Closing a window

Posted on 1999-07-22
9
Medium Priority
?
148 Views
Last Modified: 2012-05-04
How can I close a window created by :

HandleOpened:=ShellExecute(0, 'open','test.xls', nil, nil, SW_SHOWNORMAL);

I tried Destroywindow(handleOpened) but it does nothing

info : (HandleOpened>32)
0
Comment
Question by:gaelle
[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
  • 2
  • +2
9 Comments
 
LVL 7

Expert Comment

by:RBertora
ID: 1390357
Gaelle, Anybody, I tried this:

procedure TForm1.Button1Click(Sender: TObject);
var
  HandleOpened : HWnd;
begin
  HandleOpened:=ShellExecute(0, 'open','test.xls', nil, nil, SW_SHOWNORMAL);
end;

But delphi does not recognise ShellExecute, Do you need a special uses clause to get it to work or what?

Rob.

0
 
LVL 15

Accepted Solution

by:
simonet earned 100 total points
ID: 1390358
The return value is the instance handle of the application that was run, not of a window. This is documented in the Win32 SDK help file that comes with Delphi.

For what you want to do, I suggest you use CreateProcess instead of ShellExecute.

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 20

Expert Comment

by:Madshi
ID: 1390359
Rob, you need to include ShellAPI.

Alex is right, though TerminateProcess is a quite hard method to terminate a process. I would prefer sending WM_QUIT to the main thread (with PostThreadMessage(threadID,WM_QUIT,0,0)). The question is: How can you get the threadID of the main thread? You could enumerate the windows (EnumWindows). Then call GetWindowThreadProcessID(windowHandleOfFirstEnumeratedWindow,nil). Now you have the threadID. If the process does not have any windows, you can call TerminateProcess as the last escape.

Regards, Madshi.

P.S: Please don't forget to close the process and thread handle when you don't need them anymore!!
  CloseHandle(processInformation.hProcess);
  CloseHandle(processInformation.hThread);
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:inthe
ID: 1390360
add shellAPI to uses section.
also use createprocess as alex said in answer for this purpose where you need the hwnd to close the file again .

Regards Barry
0
 

Author Comment

by:gaelle
ID: 1390361
Simonet : When I create a process using your method, the application is launch but closes immedialtly . Why?

Madshi : Could you send a whole sample code?

Thanks to all.

0
 
LVL 15

Expert Comment

by:simonet
ID: 1390362
>the application is launch but closes immedialtly . Why?

Check your code. The code I posted was taken from a working application and runs fine. I've compiled the application with Delphi 3 and 4 and in all cases it worked fine. I believe there might be something in your own code that might be causing the problem.

Alex
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1390363
type TEnumWindowRec  = record
                         windowFound : boolean;
                         processID   : dword;
                       end;
     TPEnumWindowRec = ^TEnumWindowRec;

function EnumWindowsProc(window: HWND; ewRec: TPEnumWindowRec) : BOOL; stdcall;
var pid,tid : dword;
begin
  tid:=GetWindowThreadProcessID(window,@pid);
  ewRec^.windowFound:=pid=ewRec^.processID;
  if ewRec^.windowFound then
    PostThreadMessage(tid,WM_QUIT,0,0);
  result:=not ewRec^.windowFound;
end;

procedure CloseProcess(processInfo: TProcessInformation);
var ewRec : TEnumWindowRec;
begin
  ewRec.windowFound:=false;
  ewRec.processID:=processInfo.dwProcessId;
  EnumWindows(@EnumWindowsProc,integer(@ewRec));
  if not ewRec.windowFound then
    TerminateProcess(processInfo.hProcess,0);
end;

0
 

Author Comment

by:gaelle
ID: 1390364
simonet :
if I do that : ExecCommandLine('test.doc'), will the function open the file "test.doc" with winword?)


0
 

Author Comment

by:gaelle
ID: 1390365
Madshi : Thanks very much.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

705 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