Solved

Hide an EXE file and use Registry

Posted on 2000-04-25
11
380 Views
Last Modified: 2010-04-04
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?
0
Comment
Question by:yaron_avr
  • 2
  • 2
  • 2
  • +5
11 Comments
 
LVL 5

Expert Comment

by:TheNeil
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
0
 
LVL 7

Expert Comment

by:Motaz
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.

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
0
 

Expert Comment

by:johnstoned
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.

Dave.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 1

Expert Comment

by:tomer_engel
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..
0
 

Expert Comment

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

Expert Comment

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

0
 
LVL 1

Expert Comment

by:tomer_engel
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
0
 

Accepted Solution

by:
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).
0
 

Expert Comment

by:liuxiao
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
0
 

Expert Comment

by:we11er
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
0
 

Expert Comment

by:we11er
ID: 2896883
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)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Run video youtube webbrowse 10 65
oracle global variables 4 76
LAN or WAN ? 11 95
Delphi Firmonkey, Android:  blocking dialogs not implemented on this platform 5 24
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

856 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