Hide an EXE file and use Registry

Posted on 2000-04-25
Last Modified: 2010-04-04
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?
Question by:yaron_avr
  • 2
  • 2
  • 2
  • +5

Expert Comment

ID: 2746750
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

Expert Comment

ID: 2746762
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.


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:

  MyRegs: TRegistry;
  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,
  MyRegs.WriteInteger('Left', Self.Left);
  MyRegs.WriteInteger('Top', Self.Top);
  MyRegs.WriteInteger('Width', Self.Width);
  MyRegs.WriteInteger('Height', Self.Height);


- At form's OnCreate event write:

  MyRegs: TRegistry;
  L, T, W, H: integer;
  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

    (*** 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;


- 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.


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

- Create registry object:

  MyRegs:= TRegistry.Create;

- Selecting root key (directory)


- 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:


See also:

Creating components at run-time


From my EBook, Delphi programming Guide:


Expert Comment

ID: 2746990
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.

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.


Expert Comment

ID: 2747273
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..

Expert Comment

ID: 2751833
Also consider using
ShellExecute() instead of WinExec.

Expert Comment

ID: 2751836
With ShellExecute you cn specify a flag SW_HIDE to prevent the called exe from showing up.


Expert Comment

ID: 2753465
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

Accepted Solution

StrayWolfe earned 25 total points
ID: 2753646
The ShellAPI is better suited for other tasks.  The correct approach for absolute control over a spawned process is to use CreateProcess.  Refer to the WinAPI help files on how to call this.  Using CreateProcess will allow you to start the app as hidden as well as redirect input/output if required, wait until the app finishes executing and just about anything you can imagine.

Also note that WinExec is only still around for backward compatability and merely calls CreateProcess for you without giving you any of the control.  The WinExec call shouldn't be used in a 32bit application (as stated by MicroSoft).

Expert Comment

ID: 2762979
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

Expert Comment

ID: 2896827
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

Expert Comment

ID: 2896883
Here is hiding it from Ctrl+Alt+Del:


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

procedure TForm1.FormActivate(Sender: TObject);

procedure TForm1.FormDestroy(Sender: TObject);


Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question