List Info

Thread: Re: Windows processes




Re: Windows processes
country flaguser name
United States
2008-03-12 10:07:41

What i am trying to do is to open automatically some files each specific time.
I am opening such files with Shellexecute instruction as is showed next:

var
vpchar : arrar [0..800] of char;
begin
StrPcopy(vpchar, table1filename.value);
Shellexecute(application.handle, 'open', vpchar, nil, nil, SW_NORMAL)

.....
.....

end;

When a file is opened, after certain time it should be closed and then another file should
be opened.

Thank you !

--- In delphi-en%40yahoogroups.com">delphi-enyahoogroups.com, "Charlie Chambers&quot; <chambers120...> wrote:
&gt;
> Hi Francisco,
> What Rob says is correct and i totally agree with.
&gt;
> Tell us how you "open" the file. Many times you can get the window handle of the
process you started and "kill" the process by using window messages.
>
> Why is it you need to "Kill" the window? Maybe you need to rethink your design as to
what you are wanting to accomplish and it's methods?
>
> Cheers,
> Charlie
>
> ----- Original Message -----
> From: Rob Kennedy
> To: delphi-en%40yahoogroups.com">delphi-enyahoogroups.com
> Sent: Tuesday, March 11, 2008 8:33 PM
> Subject: Re: [delphi-en] Windows processes
>
>
> Francisco Javier Hernandez Segura wrote:
&gt; > I am opening a file from my delphi app. For example: a PPS File or an Adobe file.
&gt; > How can i close that file after it is launched from my delphi app ?
> >
> > How can i to kill the process assigned to such file from my delphi application?
>
> You're not opening a file. You're starting a program (maybe). What that
> program does with the file is out of your control, so you cannot close it.
>
> How are you doing whatever you're doing. The answer determines what you
> can do to stop whatever you started earlier.
>
> --
> Rob
>
>
>
>
> [Non-text portions of this message have been removed]
>

__._,_.___
.

__,_._,___
Re: Re: Windows processes
country flaguser name
United States
2008-03-12 10:25:09

You would need to lookup something WinExecute and
wait.. and then assign an Integer value that will stay
open slightly longer than the process time.. for
example if it takes 15 seconds on average for each
file then I would set the integer to something like
20000 I will send you an example. make sure that you
also add to the uses clause SHELLAPI
--- Francisco Javier Hernandez Segura
&lt; franseg13%40yahoo.com">franseg13yahoo.com> wrote:

> What i am trying to do is to open automatically some
>; files each specific time.
&gt; I am opening such files with Shellexecute
> instruction as is showed next:
&gt;
> var
> vpchar : arrar [0..800] of char;
&gt; begin
&gt; StrPcopy(vpchar, table1filename.value);
> Shellexecute(application.handle, 'open', vpchar,
> nil, nil, SW_NORMAL)
>
> .....
&gt; .....
&gt;
> end;
>;
> When a file is opened, after certain time it should
&gt; be closed and then another file should
> be opened.
>
> Thank you !
>
> --- In delphi-en%40yahoogroups.com">delphi-enyahoogroups.com, "Charlie Chambers&quot;
> <chambers120...> wrote:
&gt; >
>; > Hi Francisco,
> > What Rob says is correct and i totally agree with.
&gt; >
> > Tell us how you "open" the file. Many times you
> can get the window handle of the
> process you started and "kill" the process by using
&gt; window messages.
> >
> > Why is it you need to "Kill" the window? Maybe
&gt; you need to rethink your design as to
> what you are wanting to accomplish and it's methods?
> >
> > Cheers,
> > Charlie
> >
> > ----- Original Message -----
> > From: Rob Kennedy
> > To: delphi-en%40yahoogroups.com">delphi-enyahoogroups.com
> > Sent: Tuesday, March 11, 2008 8:33 PM
> > Subject: Re: [delphi-en] Windows processes
> >
> >
> > Francisco Javier Hernandez Segura wrote:
&gt; > > I am opening a file from my delphi app. For
> example: a PPS File or an Adobe file.
&gt; > > How can i close that file after it is launched
> from my delphi app ?
> > >
> > > How can i to kill the process assigned to such
>; file from my delphi application?
> >
> > You're not opening a file. You're starting a
> program (maybe). What that
> > program does with the file is out of your
>; control, so you cannot close it.
> >
> > How are you doing whatever you're doing. The
> answer determines what you
> > can do to stop whatever you started earlier.
> >
> > --
> > Rob
> >
> >
> >
> >
> > [Non-text portions of this message have been
>; removed]
> >
>;
>
>
>

__________________________________________________________
Looking for last minute shopping deals?
Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping

__._,_.___
.

__,_._,___
Re: Re: Windows processes
country flaguser name
United States
2008-03-12 10:28:11

function WinExecAndWait32(FileName: string;
Visibility: Integer): Longword;
var { by Pat Ritchey }
zAppName: array[0..512] of Char;
zCurDir: array[0..255] of Char;
WorkDir: string;
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
begin
StrPCopy(zAppName, FileName);
GetDir(0, WorkDir);
StrPCopy(zCurDir, WorkDir);
FillChar(StartupInfo, SizeOf(StartupInfo), #0);
StartupInfo.cb := SizeOf(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := Visibility;
if not CreateProcess(nil,
zAppName, // pointer to command line string
nil, // pointer to process security attributes
nil, // pointer to thread security attributes
False, // handle inheritance flag
CREATE_NEW_CONSOLE or // creation flags
NORMAL_PRIORITY_CLASS,
nil, //pointer to new environment block
nil, // pointer to current directory name
StartupInfo, // pointer to STARTUPINFO
ProcessInfo) // pointer to PROCESS_INF
then Result := WAIT_FAILED
else
begin
WaitForSingleObject(ProcessInfo.hProcess,
INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess, Result);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
end;
end; { WinExecAndWait32 }

procedure TForm1.Button1Click(Sender: TObject);
begin
WinExecAndWait32('notepad.exe', False, True);
end;

{*******************************}

"Anti-Freezing&quot;:

function ExecAndWait(const FileName: string; const
CmdShow: Integer): Longword;
var { by Pat Ritchey }
zAppName: array[0..512] of Char;
zCurDir: array[0..255] of Char;
WorkDir: string;
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
AppIsRunning: DWORD;
begin
StrPCopy(zAppName, FileName);
GetDir(0, WorkDir);
StrPCopy(zCurDir, WorkDir);
FillChar(StartupInfo, SizeOf(StartupInfo), #0);
StartupInfo.cb := SizeOf(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := CmdShow;
if not CreateProcess(nil,
zAppName, // pointer to command line string
nil, // pointer to process security attributes
nil, // pointer to thread security attributes
False, // handle inheritance flag
CREATE_NEW_CONSOLE or // creation flags
NORMAL_PRIORITY_CLASS,
nil, //pointer to new environment block
nil, // pointer to current directory name
StartupInfo, // pointer to STARTUPINFO
ProcessInfo) // pointer to PROCESS_INF
then Result := WAIT_FAILED
else
begin
while WaitForSingleObject(ProcessInfo.hProcess, 0)
= WAIT_TIMEOUT do
begin
Application.ProcessMessages;
Sleep(50);
end;
{
// or:
repeat
AppIsRunning :=
WaitForSingleObject(ProcessInfo.hProcess, 100);
Application.ProcessMessages;
Sleep(50);
until (AppIsRunning <> WAIT_TIMEOUT);
}

WaitForSingleObject(ProcessInfo.hProcess,
INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess, Result);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
end;
end; { WinExecAndWait32 }

procedure TForm1.Button1Click(Sender: TObject);
begin
ExecAndWait('C:ProgrammeWinZipWINZIP32.EXE',
SW_SHOW);
end;



{--WinExecAndWait32V2
------------------------------------------------}
{: Executes a program and waits for it to terminate
Param FileName contains executable + any parameters
Param Visibility is one of the ShowWindow options,
e.g. SW_SHOWNORMAL
Returns -1 in case of error, otherwise the programs
exit code
Desc In case of error SysErrorMessage( GetlastError )
will return an
error message. The routine will process paint
messages and messages
send from other threads while it waits.
}{ Created 27.10.2000 by P. Below
----------------------------------------------------------}

function WinExecAndWait32V2(FileName: string;
Visibility: Integer): DWORD;
procedure WaitFor(processHandle: THandle);
var
Msg: TMsg;
ret: DWORD;
begin
repeat
ret := MsgWaitForMultipleObjects(1, { 1 handle
to wait on }
processHandle, { the handle }
False, { wake on any event }
INFINITE, { wait without timeout }
QS_PAINT or { wake on paint messages }
QS_SENDMESSAGE { or messages from other
threads }
);
if ret = WAIT_FAILED then Exit; { can do little
here }
if ret = (WAIT_OBJECT_0 + 1) then
begin
{ Woke on a message, process paint messages
only. Calling
PeekMessage gets messages send from other
threads processed. }
while PeekMessage(Msg, 0, WM_PAINT, WM_PAINT,
PM_REMOVE) do
DispatchMessage(Msg);
end;
until ret = WAIT_OBJECT_0;
end;
var { V1 by Pat Ritchey, V2 by P.Below }
zAppName: array[0..512] of char;
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
begin { WinExecAndWait32V2 }
StrPCopy(zAppName, FileName);
FillChar(StartupInfo, SizeOf(StartupInfo), #0);
StartupInfo.cb := SizeOf(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := Visibility;
if not CreateProcess(nil,
zAppName, { pointer to command line string }
nil, { pointer to process security attributes }
nil, { pointer to thread security attributes }
False, { handle inheritance flag }
CREATE_NEW_CONSOLE or { creation flags }
NORMAL_PRIORITY_CLASS,
nil, { pointer to new environment block }
nil, { pointer to current directory name }
StartupInfo, { pointer to STARTUPINFO }
ProcessInfo) { pointer to PROCESS_INF } then
Result := DWORD(-1) { failed, GetLastError has
error code }
else
begin
Waitfor(ProcessInfo.hProcess);
GetExitCodeProcess(ProcessInfo.hProcess, Result);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
end;
end; { WinExecAndWait32V2 }

procedure TForm1.Button1Click(Sender: TObject);
begin
WinExecAndWait32V2('notepad.exe', SW_SHOWNORMAL);
end;

// With ShellExecuteEx:
//*****************************************************



uses
ShellApi;

procedure ShellExecute_AndWait(FileName: string;
Params: string);
var
exInfo: TShellExecuteInfo;
Ph: DWORD;
begin
FillChar(exInfo, SizeOf(exInfo), 0);
with exInfo do
begin
cbSize := SizeOf(exInfo);
fMask := SEE_MASK_NOCLOSEPROCESS or
SEE_MASK_FLAG_DDEWAIT;
Wnd := GetActiveWindow();
ExInfo.lpVerb := 'open';
ExInfo.lpParameters := PChar(Params);
lpFile := PChar(FileName);
nShow := SW_SHOWNORMAL;
end;
if ShellExecuteEx(exInfo) then
Ph := exInfo.HProcess
else
begin
ShowMessage(SysErrorMessage(GetLastError));
Exit;
end;
while WaitForSingleObject(ExInfo.hProcess, 50) <>
WAIT_OBJECT_0 do
Application.ProcessMessages;
CloseHandle(Ph);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ShellExecute_AndWait('FileName', 'Parameter');
end;

{*******************************}



function ShellExecute_AndWait(Operation, FileName,
Parameter, Directory: string;
Show: Word; bWait: Boolean): Longint;
var
bOK: Boolean;
Info: TShellExecuteInfo;
{
****** Parameters ******
Operation:

edit Launches an editor and opens the document for
editing.
explore Explores the folder specified by lpFile.
find Initiates a search starting from the specified
directory.
open Opens the file, folder specified by the lpFile
parameter.
print Prints the document file specified by lpFile.
properties Displays the file or folder's properties.

FileName:

Specifies the name of the file or object on which
ShellExecuteEx will perform the action specified by
the lpVerb parameter.

Parameter:

String that contains the application parameters.
The parameters must be separated by spaces.

Directory:

specifies the name of the working directory.
If this member is not specified, the current
directory is used as the working directory.

Show:

Flags that specify how an application is to be shown
when it is opened.
It can be one of the SW_ values

bWait:

If true, the function waits for the process to
terminate
}
begin
FillChar(Info, SizeOf(Info), Chr(0));
Info.cbSize := SizeOf(Info);
Info.fMask := SEE_MASK_NOCLOSEPROCESS;
Info.lpVerb := PChar(Operation);
Info.lpFile := PChar(FileName);
Info.lpParameters := PChar(Parameter);
Info.lpDirectory := PChar(Directory);
Info.nShow := Show;
bOK := Boolean(ShellExecuteEx(Info));
if bOK then
begin
if bWait then
begin
while
WaitForSingleObject(Info.hProcess, 100) =
WAIT_TIMEOUT
do Application.ProcessMessages;
bOK := GetExitCodeProcess(Info.hProcess,
DWORD(Result));
end
else
Result := 0;
end;
if not bOK then Result := -1;
end;

__________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

__._,_.___
.

__,_._,___
RE: Re: Windows processes
country flaguser name
Netherlands
2008-03-12 10:33:56

Gepending on whether the started application will stop
itself (and yours has to wait for it) or your application
should stop it after a certain of time, you should either
use ShellExecuteEx and wait for the application to finish
(checking for "STILL_ACTIVE&quot; with "GetExitCodeProcess") or
start the application using "CreateProcess&quot; and just kill
that process when the time is right.

Greetz,

Peter.

-----Oorspronkelijk bericht-----
Van: delphi-en%40yahoogroups.com">delphi-enyahoogroups.com [mailto: delphi-en%40yahoogroups.com">delphi-enyahoogroups.com]Namens
Francisco Javier Hernandez Segura
Verzonden: woensdag 12 maart 2008 16:08
Aan: delphi-en%40yahoogroups.com">delphi-enyahoogroups.com
Onderwerp: [delphi-en] Re: Windows processes

What i am trying to do is to open automatically some files each specific
time.
I am opening such files with Shellexecute instruction as is showed next:

var
vpchar : arrar [0..800] of char;
begin
StrPcopy(vpchar, table1filename.value);
Shellexecute(application.handle, 'open', vpchar, nil, nil, SW_NORMAL)

.....
.....

end;

When a file is opened, after certain time it should be closed and then
another file should
be opened.

Thank you !

__._,_.___
.

__,_._,___
[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )