Re:Executing a function in my program from outside
You can find if your program is already running using FindWindow. To 'order' him to open the file, you can use the WM_COPYDATA message:
Put this in your project source:
var Prev:HWND;
pCopyData: TCopyDataStruct;
FileToLoad: String;
Prev := FindWindow('TForm1', nil); //your main form class
if ((Prev <> 0) then
begin
for i:=1 to ParamCount do
begin
if FileExists(ParamStr(i)) then
begin
FileToLoad := AnsiLowerCaseFileName(ParamStr(i));
with pCopyData do
begin
dwData := 0;
cbData := Length(FileToLoad) + 1;
lpData := PChar(FileToLoad);
end;
SendMessage(Prev, WM_COPYDATA, Application.Handle, Longint(@pCopyData));
end;
end;
SetForegroundWindow(Prev);
Halt(1);
//
Application.Initialize;
//.....
end;
in your main form unit:
type Form1=class(TForm)
...
private;
procedure OpenFromAnother(var Message: TMessage);message WM_COPYDATA;
end;
procedure TForm1.OpenFromAnother(var Message: TMessage);
var TheFileName: String;
pCopyData: PCopyDataStruct;
begin
pCopyData := Pointer(Message.LParam);
TheFileName := AnsiLowerCaseFileName(String(PChar(pCopyData^.lpData)));
OpenFile(TheFileName);
end;
NOTE! if you run your program within Delphi IDE, it will never execute since there is already a TForm1 window (the designer). Compile it and execute it outside the IDE.
HTH, Alex.
Mrlon M. Brum <naoresponda@por_email.com> wrote in article <01be80f2$07272740$9b2aa...@SERVIDOR.NESS.NET>...
Quote
> I was going to ask how can I now if the program is already been executed,
> but Ive already seen the anwser. My problem now is that my program needs
> to check if the Program is running, and if it is, make this existing
> instance of the program to open the file that was assigned to the first.
> How can I give an order to the program from outside it?