Solved

Hide an EXE file and use Registry

Posted on 2000-04-25
11
370 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Expert Comment

by:tomer_engel
Comment Utility
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
Comment Utility
Also consider using
ShellExecute() instead of WinExec.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Expert Comment

by:d32coder
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now