[fpc-pascal]No wait execution and redirection of stdin and stdout
Full_Name
memsom at post.interalpha.co.uk
Wed Aug 28 13:52:20 CEST 2002
Quoting "Peter H.M. Brooks" <peter at new.co.za>:
> - Firstly I'd like to launch a program with no wait and then check for
> its completion later.
Calling:
winexec('c:\Listener.exe', sw_normal);
will execute the listener app, and leave it running independently. WinExec is
depreciated in the windows api, but is way easier to use than createprocess.
However, I think you would need to use createprocess to do your stdin/stdout
redirection. A 1 minute search on google groups found the following (untested):
procedure RunAProgram(Directory: PChar; File2Run: PChar;
CmmdRun: PChar; How2Show: Word);
var
pi : TProcessInformation;
si : TStartupInfo;
begin
with si do begin
cb := SizeOf(si);
lpReserved := nil;
lpDesktop := nil;
lpTitle := nil;
dwFlags := 0;
wShowWindow := How2Show;
cbReserved2 := 0;
lpReserved2 := nil;
end;
Chdir(Directory);
CreateProcess(File2Run, cmmdRun, nil, nil, FALSE, 0, nil, nil, si, pi);
end;
Here's another part of the puzzle (it's delphi though, but afain it's pretty
generic, bar the @windproc line that you have to cast to wndproc iirc in FPC,
as noted.)
/////////////////////////////////////////////////////////////////////////
program Listener;
uses
windows,
sysutils,
messages;
var
HookMessage: longword = 0;
Handle: HWND;
wC : TWndClass;
msg: tmsg;
const
AppName: array[0..50] of char = 'application/x-vnd.MyApp'#0;
function WindowProc(Window: HWnd; Message, WParam: Word;
LParam: Longint): Longint; stdcall;
begin
if Message = HookMessage then begin
//Do your action...
writeln('I got a message...');
Result := 1;
Exit;
end;
case message of
WM_CLOSE,
WM_QUIT: PostQuitMessage(0);
end;
Result := DefWindowProc(Window, Message, WParam, LParam);
end;
var
w: hwnd;
begin
HookMessage := RegisterWindowMessage('Application/x-vnd.CustomHookMessage');
with wC do begin
style := CS_VREDRAW or CS_HREDRAW;
lpfnWndProc := @WindowProc; //in fpc this should be 'wndproc(@WindowProc)'
cbClsExtra := 0;
cbWndExtra := 0;
wC.hInstance := HInstance;
hIcon := LoadIcon(HInstance,IDI_APPLICATION);
hCursor := LoadCursor(HInstance,IDC_ARROW);
hbrBackground := GetStockObject(LTGRAY_BRUSH);
lpszMenuName := nil;
lpszClassName:= @AppName;
end;
RegisterClass(WC);
//create off screen window....
handle := CreateWindow(wc.lpszClassName,
'Nothing',
WS_OVERLAPPEDWINDOW or WS_TABSTOP,
-100,
-100,
1,
1,
0,
0,
HInstance,
nil);
ShowWindow(Handle, SW_HIDE); //completely hide the dummy window
UpdateWindow(Handle);
while Windows.GetMessage(msg, 0, 0, 0) do
begin
TranslateMessage(msg);
DispatchMessage(msg);
end;
end.
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
program Messenger;
uses
SysUtils, windows, messages;
var
HookMessage: longword = 0;
begin
HookMessage := RegisterWindowMessage('Application/x-vnd.CustomHookMessage');
postmessage(HWND_BROADCAST, HookMessage, 0, 0);
end.
/////////////////////////////////////////////////////////////////////////////
Hope that helps,
Matt
--
"Computer games don't affect kids; I mean if Pac-Man affected us as kids,
we'd all be running around in darkened rooms, munching magic pills and
listening to repetitive electronic music."
Kristian Wilson,
Nintendo, Inc, 1989
-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCS d? s+++:+ a- C++ UL+ P L++ E---- W- N+ o+ K- w
O- M V PS PE-- Y PGP- t- 5-- X- R- tv+ b+ DI++ D+
G e++ h--- r+++ y+++
------END GEEK CODE BLOCK------
More information about the fpc-pascal
mailing list