pascal_a
asked on
finding a program
i wanbt to know how can i know when my application is runnung if another application exists on my computer
i think that is something like findexecutable but i am having problem with the syntax ;
i think that is something like findexecutable but i am having problem with the syntax ;
Are you talking about preventing mutiple program instances ??
ASKER
no i am searching for another program while my application is running if it is not exists i need to remove something from my program
If you know the caption title of the program, you can use this :
WindowName := FindWindow(nil,'Gangsters' );
If WindowName = 0 then
begin
//can't find the program
MessageDlg('Run the game first',mtWarning,[mbOK],0) ;
Application.Terminate;
end
else
begin
//if you find the program
end;
WindowName := FindWindow(nil,'Gangsters'
If WindowName = 0 then
begin
//can't find the program
MessageDlg('Run the game first',mtWarning,[mbOK],0)
Application.Terminate;
end
else
begin
//if you find the program
end;
ASKER
i know the name of the program (something.exe) but this program is not running i want to know if i can make it to show something if the program exists from my program
i mean that if this program exists in my computer and is not an active window;
i mean that if this program exists in my computer and is not an active window;
Is Raymonds comment not what you need ?
U can use a couple of functions to find any file on your fixed drives, something like this:
function SearchFile(strfile:string) : boolean ;
var c,c1: array [0..100] of char; blnok:boolean; i:integer;
pstrdrives : LPSTR;
nsize : DWORD;
nrezult : WORD;
strdrive:string;
begin
SearchFile := false;
nsize := 2;
pstrdrives := LPSTR(HeapAlloc(GetProcess Heap, 0, nsize));
nrezult := GetLogicalDriveStrings(nsi ze,pstrdri ves);
if nsize < nrezult then
begin
nsize := nrezult;
pstrdrives := LPSTR(HeapReAlloc(GetProce ssHeap, 0, pstrdrives, nsize));
nrezult := GetLogicalDriveStrings(nsi ze,pstrdri ves);
end;
if nrezult <> 0 then
begin
strdrive := '';
for i:= 0 to nsize-1 do
if pstrdrives[i] = #0 then
begin
if (GetDriveType(LPSTR(strdri ve)) = DRIVE_FIXED) and SearchInDrive(strdrive,str file) then
begin
SearchFile := true;
break
end;
strdrive := '';
if (i<nsize - 1) and (pstrdrives[i+1] = #0) then break;
end
else strdrive := strdrive + pstrdrives[i];
end;
if(pstrdrives <> nil) then HeapFree(GetProcessHeap, 0, pstrdrives);
end;
function SearchInDrive(strpath,strf ile:string ) : boolean;
var searchrec : TSearchRec;
nfind : integer;
var blnok: boolean;
begin
SearchInDrive := false;
nfind := Findfirst(strpath + strfile, faanyfile, searchrec);
if nfind = 0 then
begin
SearchInDrive := true;
FindClose(SearchRec);
exit
end
else
begin
nfind := Findfirst(strpath + '*.*', faanyfile, searchrec);
while nfind = 0 do
begin
if ((SearchRec.Attr and faDirectory) > 0) and (searchrec.name<>'.') and (searchrec.name<>'..') then
begin
blnok := SearchinDrive(strpath + searchrec.name + '\',strfile);
if blnok then
begin
SearchInDrive := blnok;
FindClose(SearchRec);
exit
end
else nfind := Findnext(searchrec);
end
else nfind := Findnext(searchrec);
end;
FindClose(SearchRec);
end;
end;
Cheers,
Bogdan
function SearchFile(strfile:string)
var c,c1: array [0..100] of char; blnok:boolean; i:integer;
pstrdrives : LPSTR;
nsize : DWORD;
nrezult : WORD;
strdrive:string;
begin
SearchFile := false;
nsize := 2;
pstrdrives := LPSTR(HeapAlloc(GetProcess
nrezult := GetLogicalDriveStrings(nsi
if nsize < nrezult then
begin
nsize := nrezult;
pstrdrives := LPSTR(HeapReAlloc(GetProce
nrezult := GetLogicalDriveStrings(nsi
end;
if nrezult <> 0 then
begin
strdrive := '';
for i:= 0 to nsize-1 do
if pstrdrives[i] = #0 then
begin
if (GetDriveType(LPSTR(strdri
begin
SearchFile := true;
break
end;
strdrive := '';
if (i<nsize - 1) and (pstrdrives[i+1] = #0) then break;
end
else strdrive := strdrive + pstrdrives[i];
end;
if(pstrdrives <> nil) then HeapFree(GetProcessHeap, 0, pstrdrives);
end;
function SearchInDrive(strpath,strf
var searchrec : TSearchRec;
nfind : integer;
var blnok: boolean;
begin
SearchInDrive := false;
nfind := Findfirst(strpath + strfile, faanyfile, searchrec);
if nfind = 0 then
begin
SearchInDrive := true;
FindClose(SearchRec);
exit
end
else
begin
nfind := Findfirst(strpath + '*.*', faanyfile, searchrec);
while nfind = 0 do
begin
if ((SearchRec.Attr and faDirectory) > 0) and (searchrec.name<>'.') and (searchrec.name<>'..') then
begin
blnok := SearchinDrive(strpath + searchrec.name + '\',strfile);
if blnok then
begin
SearchInDrive := blnok;
FindClose(SearchRec);
exit
end
else nfind := Findnext(searchrec);
end
else nfind := Findnext(searchrec);
end;
FindClose(SearchRec);
end;
end;
Cheers,
Bogdan
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
function GetProgramDir : string;
var
Reg: TRegistry;
TargetDir: string;
begin
Result := '';
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_LOCAL_MACHINE; // Should be this by default anyway
// Look initially for the project directory
if Reg.OpenKeyReadOnly('\Soft
begin
TargetDir := Reg.ReadString('');
Reg.CloseKey;
Result := ExtractFilePath(TargetDir)
end;
finally
Reg.Free;
end;
end;
Cheers,
Raymond.