Link to home
Start Free TrialLog in
Avatar of jamoville
jamovilleFlag for Afghanistan

asked on

getting an app. to start another app.

I have writtin an application that I would like to start when a particular application is started.  In other words I want to be able to start, lets say word.exe, and have my application execute at the same time.  Is this possible?
Avatar of Tommy Hui
Tommy Hui

In your program you can use either WinExec() or CreateProcess() to start another application.
Avatar of jamoville

ASKER

This solution would mean the user would have to start my application first and then the other application would start.  I want the user to be able to start my application by starting a different application.  In other words, I need my application to automatically be started.
Avatar of jkr
Well, this is possible by setting a shell hook using 'SetWindowsHookEx()'. When your hook proc receives 'HSHELL_WINDOWCREATED', you can check if it is e.g. 'WinWord' by using 'GetWindowText( ( HWND) wParam), ...)' (see help on 'ShellProc()' why i use 'wParam', it's a bit too much to elaborate here ;-). If so, the shell hook could start your application.
Using the setwindowshookex would mean setting a global hook and unfortunatally I am unable to do that at this time.  Any other suggestions?
On Win95/Win NT 4 and over, try modifying registry key "HKEY_CLASSES_ROOT\exefile\shell\open\command" that specify what command is executed when an .EXE file is launched from the shell.

The default value is '"%1" %*' meaning the launched command is the exefile itself with all its parameters.

You can set the key value to something like 'MYLAUNCH "%1" %*'
where MYLAUNCH is a program of yours that will be executed every time a exe file is launched. MYLAUNCH will receive the exe file path as first parameter.
Then, in MYLAUNCH, you'll have the possibility to launch the exe file using WinExec or CreateProcess API.

Be careful when modifying this registry key, because it control every executable program that you will have to launch from the shell. To prevent problems, test your MYLAUNCH program first with 'batfile' key for example, that controls .BAT file launching.

Hope this helps.

jamoville - i'm just curious, why can't you set a shell hook??
Where is this registry key located.  I've not heard of this before, could you give me a little more help?

jkr- the requirements of the project I'm working on are out of my control and that's why I'm not able to create the global hook.  I'm sure that it would have worked but I think my superiors believe it would effect system resources too much.
The HKEY_CLASSES_ROOT registry key contains associations between file extensions and commands (specifies what command is executed when a file with associated extension is lauched)

Don't you know how to use REGEDIT tool ?
Execute REGEDIT (Registry editor) from WIN95/NT shell and open successively the following keys on the left part of the Registry editor window :
HKEY_CLASSES_ROOT, exefile, shell, open, command
(HKEY_CLASSES_ROOT contains a great amount of ".xxx" keys. "exefile" is located after those ones in ASCII order)
Then, on the right part of the Registry editor window, you can modify "(Default)" key by double-clicking it.

On what system are you working :
Win 3.x ? Win 95 ? NT 3.51 ? NT 4 ?

Once again, be careful when modifying registry keys. As I last wrote, you should first test your program by modifying "batfile" key instead of "exefile" and try lauching a .BAT file.

Here is a small code piece that can be used for a MYLAUNCH program, as I described earlier :

#include  <windows.h>
int  WINAPI  WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
      MessageBox(NULL,lpCmdLine,"MYLAUNCH parameters",MB_OK) ;
      WinExec(lpCmdLine,SW_SHOWDEFAULT) ;
      return 0 ;
}

- Compile your program with name MYLAUNCH.EXE in, for example, C:\BIN\ directory
- Modify "batfile\(Default)" key value to set it to 'C:\BIN\MYLAUNCH "%1" %*'
- Run a .BAT file (it must exist)

--> MYLAUNCH is first executed, displays the .BAT file name you typed in in a message box, and the .BAT file is launched.


This answer is great, but I've been told that I can't change the registry for the application.  Unfortunetly I don't know why.  Is there a different solution.  If not, I will give you the point anyway.
ASKER CERTIFIED SOLUTION
Avatar of stsanz
stsanz

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
I think your answers a great and I know the registry solution would have work.  You right I'm not allowed to do much.  O well, ya gotta listen to the boss I guess.  Thanks for your input and I'll try you other solution.  It sound logical.