Link to home
Start Free TrialLog in
Avatar of yaron_avr
yaron_avr

asked on

Hide an EXE file and use Registry

Hi;
I wrote a program that needs to use another EXE file.
when my program is running it opens the EXE file as expected.
The thing is that I want the window of the EXE to be HIDE;
I used the command WINEXEC(Path of the EXE file, SW_HIDE).
The program is running the EXE file but always provides  an Error.
 I also need to know in my program where the user put the EXE file in computer, so my program will run it when it needs it, Some people told me that it concern with Registry, How do I do this?
Avatar of TheNeil
TheNeil

In the past I've just used WinExec like this

  iResult := winexec(PCHAR(sComString), sw_normal);

and it works fine. Try running the EXE using sw_normal rather than sw_hide and see if that works. Also check the path and the filename that you're attempting to run and whether it's your app or the app that you're trying to run that is causing the error.

As for finding WHERE the EXE is then you're assuming that the EXE is registered in the registry. What you might have to do is search through the user's hard drive for the EXE and then put the result of the search in yourself. When you need to run the EXE next time, just extract the path from the registry. It's not ideal but it does cover most eventualities. If you want more help with this then just ask

The Neil
If you want to read and write in Registry read this topic:
-------------------


Windows registry is a big hierarchical database which contains information about hardware and software including windows itself. Configuration information about any application can be stored and retreived from Windows registry. This configuration information can be application form coordinates, width, height, color, and many others.

In early Windows versions befor using registry, applications save it's configuration in a seperate file called initialization file, or configuration file (.ini, or .cfg) which always exists withen application directory or in Windows directory. Initialization and configuration files are easy to be deleted and damaged, moreover any normal user can access it and modify it's contents, but Windows registry is more safe than normal files. Windows grantees that registry database will be exists in safe area and it makes a backup for it periodically. In addition to that it is hard be accessed and modified by normal users.

Accessing Registry in Delphi

Windows Registry can be accessed by declaring TRegistry objects which exists in Registry unit. TRegistry class encapsulate functions and properties which used to access and manipulate Windows registry such as adding keys (Key is like a directory in which we can store data or create sub keys), reading and writing different kinds of data, deleting key, and so on.

Example

In this example we want to store form's coordinates in registry and retrieving it when opening the application again:

- Add Registry to unit's clause.
- At form's OnClose event write:

var
  MyRegs: TRegistry;
begin
  MyRegs:= TRegistry.Create;
  MyRegs.RootKey:= HKEY_LOCAL_MACHINE;  // Root key (directory)

  (*** Open \Software\My Company\ key, create it if it is not exist ***)
  MyRegs.OpenKey('\SOFTWARE\My Company\', True);

  (*** Write data to registry,
   in (HKEY_LOCAL_MACHINE\SOFTWARE\My Company ***)
  MyRegs.WriteInteger('Left', Self.Left);
  MyRegs.WriteInteger('Top', Self.Top);
  MyRegs.WriteInteger('Width', Self.Width);
  MyRegs.WriteInteger('Height', Self.Height);
  MyRegs.CloseKey;
  MyRegs.Free;

end;


- At form's OnCreate event write:

var
  MyRegs: TRegistry;
  L, T, W, H: integer;
begin
  MyRegs:= TRegistry.Create;
  MyRegs.RootKey:= HKEY_LOCAL_MACHINE;  // Root key (directory)

  (*** Open \Software\My Company\ key if there is a data ***)
  if MyRegs.OpenKey('\SOFTWARE\My Company\', False) then
  begin

    (*** Read data from registry,
     at (HKEY_LOCAL_MACHINE\SOFTWARE\My Company ***)
    L:= MyRegs.ReadInteger('Left');
    T:= MyRegs.ReadInteger('Top');
    W:= MyRegs.ReadInteger('Width');
    H:= MyRegs.ReadInteger('Height');

    (*** Apply form intialization ***)
    Self.Left:= L;
    Self.Top:= T;
    Self.Width:= W;
    Self.Height:= H;
    MyRegs.CloseKey;
  end;
  MyRegs.Free;

end;

- Run the application then try to resize and move the form, after that close it and run the application again. You will notice that the form will maintain it's last postion and size.


Sammary:

This is the main methods and properties to access Windows registry:

- Create registry object:

  MyRegs:= TRegistry.Create;

- Selecting root key (directory)

  MyRegs.RootKey:= HKEY_LOCAL_MACHINE;

- Opening a key: There are two methods of opening keys:

1.  MyRegs.OpenKey('\Software\AKey', False);

This method try to open an existing key it it is not exits, it will return false. If it is successed on opening key it will return True. This kind of opening key always used when reading from registry.

2.  MyRegs.OpenKey('\Software\AKey', True);

This method try to open an existing key, if key not found it will attempt to create new one. OpenKey will return True if it is successed in opening or creating key, otherwise it will return False. This kind of opening keys always used when writing to registry.

- Writing to registry: you can write to the registry different data types such as:

  MyRegs.WriteString('Name', 'Mohammed');
  MyRegs.WriteInteger('Age', 24);
  MyRegs.WriteBoolean('Single', True);

- Reading from registry: you can read from registry existed data such as:

  Name:= MyRegs.ReadString('Name');
  Age:= MyRegs.ReadInteger('Age');
  Single:= MyRegs.ReadBoolean('Single');

- Closing key:

  MyRegs.CloseKey;


See also:

Creating components at run-time

---------

From my EBook, Delphi programming Guide:
www.geocities.com/motaz1

Motaz
if the EXE has been installed on the PC with install shield, then look in

HKEY_LOCAL_MACHINE\software\microsoft\windows\currentversion\app paths

for a list of installed applications.  One of these should be the program you're looking for.  If you open this key, the default entry shows the path that the exe is living in.  Have a look in regedit.

Dave.
as i understood the end of your question , you just want to see if the exe is one the computer, not installed or something,so use the

FileExists('path+file name') ,function very easy and friendly function..
Also consider using
ShellExecute() instead of WinExec.
With ShellExecute you cn specify a flag SW_HIDE to prevent the called exe from showing up.

ok ,let me clear up things a little bit for you:

a)to use/execute another exe file : use as said earlier :shellexcute(...) procedure, (add the ShellApi to your uses clause)

b)to FIND an exe file I can orginize your confusion(if there is any):

  1) if it's an oridnary file(an application) use the FileExists procedure.
  2)if the exe file is a component(a COM component)then
     a) you would know about it(don't ask your self :is this a COM component, 'cause if it were and you had to use it you'd know)
      b) if it IS a COM component: then ask again and i'll answer you were to find the registry key for it BUT


i think(from reading your question carfully) that it's an ordinary application so use the FileExists, and the ShellExecute procedures. so think again is you exe file is a contanere to a COM component or an oridinary application --if  an application --you don't have to use the registry...

so feedback me,,and we'll see how to continue if the exe file IS a COM component, if it doesnt' i think you have all you need
ASKER CERTIFIED SOLUTION
Avatar of StrayWolfe
StrayWolfe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Assign the EXE running state as min(this occur at the EXE file, don't do any thing in your program)
this is not a good solution, every time you run this exe file from any where it will be min. But it is very simple
This exe you want to hide:  Add this to the project source:

SetWindowLong(Application.Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW);

, and the program will be hide itself (from the taskbar).  Using ShowWindow(...sw_hide) often makes the program flicker for a moment on the taskbar, so SetWindowLong is the best method to use

You can hide it from the Ctrl+Alt+Del menu if you want, and to do that, you need to access the RegisterServiceProcess function in the Kernel32.dll file.  But you'll have to look at another questions for that because I can't remember how to do it.  There are plenty of examples out there
Here is hiding it from Ctrl+Alt+Del:

const  RSP_SIMPLE_SERVICE = 1;
       RSP_UNREGISTER_SERVICE = 0;

function  RegisterServiceProcess(dwProcessID,dwType : DWORD) : DWORD; stdcall; external 'KERNEL32.DLL';

procedure TForm1.FormActivate(Sender: TObject);
begin
RegisterServiceProcess(GetCurrentProcessID,RSP_SIMPLE_SERVICE);
end;


procedure TForm1.FormDestroy(Sender: TObject);
begin
RegisterServiceProcess(GetCurrentProcessID,RSP_UNREGISTER_SERVICE);
end;


#:o)