call/open Application (.exe) file using Win32

Started by
8 comments, last by TheUnbeliever 16 years, 8 months ago
Hi Everybody, Does anybody know how to call/open an application (.exe) file in Windows using Win32 programming? For example, opening 'Calculator' or 'Paint'. I was thinking of creating a .bat file and call it from Win32 program. But, again, I don't know how to open .bat (or any) files from Win32. And is there also a way to access the control in that particular program? maybe by finding the message that it send? What I need to do is pretty much overriding a program that I don't have access to the source code. But, don't worry, it's not for anything illegal. :) Any help would be very appreciated. Thank you in advance.
Advertisement
You have a couple of options. You can use CreateProcess, which is ridiculously overpowered and complicated unless you are writing an applications launcher, or you can use the ShellExecute or the ShellExecuteEx functions, which allow you to take advantage of cool shell functionality. For example, if you specify an html page as one of the arguments with an action of open, a browser window with the specified page will be loaded. If you specify a folder, Windows Explorer will open pointing at that folder. If you specify a word document, Microsft word will open to edit the file. Basically, if you can do it on the Windows shell, you can do it using those functions.
Mike Popoloski | Journal | SlimDX
I think you're looking for ShellExecute. Search on that one, but basically, if you only have to execute a program (and nothing else), then you should
ShellExecute(HWND_DESKTOP, "open", "C:\\Windows\\Notepad.exe", NULL, NULL, SW_SHOW);

I won't explain the parameters here (you should find that in Google or something), but I think that's it. If you get errors on "converting from char * to ...", then try
ShellExecute(HWND_DESKTOP, L"open", L"C:\\Windows\\Notepad.exe", NULL, NULL, SW_SHOW);


Good luck!
Quote:Original post by Johnny Sunshine
I won't explain the parameters here (you should find that in Google or something), but I think that's it. If you get errors on "converting from char * to ...", then try
#include <tchar.h>ShellExecute(HWND_DESKTOP, _T("open"), _T("C:\\Windows\\Notepad.exe"), NULL, NULL, SW_SHOW);


Fixed. This will work regardless of whether or not you're compiling for UNICODE, leaving no real reason to do anything different.
You guys are awesome, I got it working right away.

What about actually accessing the control/menu of the program? Does anybody have any suggestion on this?
For example, in Notepad, after opening the program then call the "New", "Open" or "Save" function.
I was thinking of using Batch file (.bat) to accomplish this; By the way, is it a good idea using .bat file to do this? is there a better alternative?

Thank you.

[Edited by - nekobasu33 on July 23, 2007 6:15:14 PM]
Programmatically using another program is almost certainly not a good idea. Why do you want to do this?
I am working on the UI side of the program. My company is working with a third party developer's software that's dealing with the hardware part.
And I just found out recently that I have to integrate my software with theirs and I don't have access to their source code.

I need to get this done soon because I need to show it to some potential customer (just to show that our system is working). Later on, when we license out the software, we will develop everything in-house (less trouble and less headache). That's the reason why I am trying my best to get this thing to work soon.

Therefore, if you guys have any suggestion on how to accomplish this, I would appreciate it so much.
Thank you in advance.
Usually in situations like these, you can take advantage of the other program's command line parameters, so that you can have your program call it with those parameters. Trying to access the actual GUI controls for that program is usually a very bad idea, but it is doable.

Before you actually decide to go that route, make sure that the other program doesn't have some kind of command line support.
Mike Popoloski | Journal | SlimDX
How can I find out if a program has a command line support?
Quote:Original post by nekobasu33
How can I find out if a program has a command line support?


The most reliable way would be to ask the supplier, or check the documentation.
[TheUnbeliever]

This topic is closed to new replies.

Advertisement