Is there a simple way to run another program, say IE, FF or just a simple .bat file sith c++? ive searched around a bit but all ive found is rocket science and didn't seem to be what im looking for. i dont wish to run it "trough" my program, just as if i had found the destination and doubleclick'ed it.

in windows can you use ShellExecute, which is an API call that will allow you to spawn an application.

Thanks for the quick answer:)
Is shellexecute a astandard function? like shellexecute(); or?

EDIT: perhaps i misunderstood, does ShellExecute have anything to do with the c++ language?

well, it's included in the windows.h, so try this:

#include <iostream>
#include <windows.h>
int main(int argc, char **argv)
{
     ShellExecute(NULL, "open", "C:\\windows\\system32\\calc.exe","", "C:\\", SW_NORMAL);
     return 0;
}

EDIT: Negative. ShellExecute is NOT C++.... ShellExecute is a windows function that can be called from any language with the ability to use windows API's (C++ is such a language). This is windows specific, and won't work in *nix or Mac.... and is not a standard.

Ahh thanks :)
Now i tryed to look up the parameters (not that i couldn't find them, i just didn't understand ;-) )
Could you please explain the parameters you are using? just briefly, i hope its not too much trouble

The first parameter, hWnd (passed NULL) is the window to associate the new process with (NULL means you aren't using this feature).

The second, lpOperation, is what you want to do with the file specified in the next parameter. Here we are "open"ing it.

Speaking of which, the third is lpFile: the file you want to perform the operation on.

The fourth, lpParameters is a string of the arguments you want to pass into the new process. These are not used in this situation.

Next is lpDirectory. This is the path you want the new process to think it was started from.

Finally, we have nShowCommand: this just specifies the starting state of the window for the new process. SW_NORMAL specifies that, what do ya know, it should start in normal mode!

You can look at specifics including other possible arguments at msdn's documentation for this function.

commented: Precisely! +10

Or use.. execlp(...)

Or use.. execlp(...)

Which I believe is *nix only.....

Thank you death_oclock, that was really helpfull:)

Cikara21, could you wxplain that? :D

Okay, Comatose: i tried it out, made a simple .bat called test.bat located on C:\, containing

msg * Hi %username% u rule

(rediculus i know)
and then a c++ "program":

#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
    ShellExecute(NULL, "open", "C:\\test.bat","", "C:\\", SW_NORMAL);
    return 0;
}

Why does that not work? :/

Cikara21, could you wxplain that? :D

Which I believe is *nix only.....

^^ Okay. beaten by seconds by Comatose :D

So, does anyone know what i am doing wrong here?

Why does that not work? :/

It works for me. Did you put test.bat in C's root dir? Here's a rewrite that starts it from the desktop.

#include <windows.h>

int main() {
    char path[MAX_PATH];
    GetEnvironmentVariable ("HOMEPATH", path, MAX_PATH);
    strcat (path, "\\Desktop");
    ShellExecute (0, "open", "test.bat", 0, path, SW_NORMAL);
    return 0;
}

[Why cant i edit my post?]

Well, it seems the problem is that i am using vista. i read something about setting ipverb to "runas". but honestly, i have no idea how to doo that? i tried googling it but.. anyone?

Link for the "runas" thingy here

@Nucleon: yeah i put it at the root: -) so i dont think your script would make a difference? (correct me if wrong ; -) )

Since you are just using ShellExecute (not ShellExecuteEx) you could just change "open" to "runas". I think.

Changing "open" to "runas" doesn't help:/ and if i copy-paste comatose's examle with calc.exe it just works? both with "runas" AND "open"? i don't get it?

you wouldn't change open to runas..... open is the "command" basically, that you are doing. What do you want to do with this file? Open it. Just like if I double clicked on it. calc.exe is a program in windows (all versions passed like 2.0). Runas is a program in vista. So replacing open with runas isn't going to work.... it's the wrong parameter. You need to replace calc.exe with runas, and somehow pass like calc.exe (and all the switches) to the parameters argument of shellexecute. It may be easier to use shellexecuteex as in the provided link above... so I guess something like this (not tested):

ShellExecute (NULL, "open", "runas", "test.bat", "c:\\", SW_NORMAL);

But based on the fact that you are in vista, and require elevated privs, I suggest using the shellexecuteex.

Okay. So, what is this ShellExecuteex? again, google didn't help me much (no related results really).
I wont do to just write ShellExecuteex and the the same params, i know. but..

it's actually on the link that you provided. If you look carefully, they are using ShellExecuteEx there. So, do that, and adjust it to your .bat file.

Ahh, i see.
Hmm, when i try to compile it my IDE gives me alot of errors, first one is:
stdafx.h : no such file or directory.

Is that because i am missing a library or?

What really makes me wonder, is that the first sollution

#include <iostream>
#include <windows.h>
#include <fstream>

using namespace std;

int main()
{
    ShellExecute(NULL, "open", "C:\\ny test.bat","", "", SW_NORMAL);
    cin.get();
    return 0;
}

Works PERFECTLY with .exe files, but not with .bat files?
So ShellExecute(NULL, "open", "C:\\ny test.exe","", "", SW_NORMAL); Works, presuming i knew how to solve my problem with an .exe, wich i dont.
BUT ShellExecute(NULL, "open", "C:\\ny test.bat","", "", SW_NORMAL); Does not work?!?

Above code works fine in windows XP, so perhaps some sort of vista thing?

The more important question is: Why would you want to execute a bat file? What's in it that you can't do with c++?

^^ i think you are right it's a vista thing. however its still a problem ;-)

Well for one thing, when something is not working, and there is no appearent reason why. i think one should try to either solve the problem, or at least find out whats causing the problem.

But you are absolutely right, its nothing one couldn't do with C++, it's just that I dont know how ;-)

I would like to know how to do this but to solve my actual problem:
What i need is actually just a way to determin the user's username, and ,if possible, eventually the password. but then i came across this problem and then.. i just wanted to know how to solve it nomatter what i can use it for, but I'm open to suggestions about another way of doing this. Hope no one died from trying to read this looong post, if so R.I.P

EDIT FOR THE POST ABOVE: I just read my post through, and it sounds like I'm making a keylogger of a sort. Thats not the case, this is for "display" purposes, and for the purpose of learning, solely

maybe you should consider trying it with cmd.exe. Use cmd.exe as the program and the bat file as a the parameter. I'm pretty sure UAC is the reason for it not working....

Ill try that, how would i add the params?

EX. ShellExecute(NULL, "open", "C:\\windows\\system32\\cmd.exe msg * hej","", "", SW_NORMAL); Want do it

ShellExecute (NULL, "open", "cmd.exe", "msg.bat * hej", "c:\\", SW_NORMAL);

Hmm when i run it^^
It opens cmd all right, but thats all that happens?

guess it's not happy about passing it a parameter.... I guess you could try:

ShellExecute (NULL, "open", "cmd.exe msg.bat * hej", "", "c:\\", SW_NORMAL)

but I have a really strong feeling that isn't going to fly...

You are right.. exactly same result, it just opened cmd, i tryed this param buissnes earlier with system(), i'd would'nt take msg as a param either, however, no problems with giving ipconfig as parameter, ass long as i didn't add anything (as /displaydns or the like)
But neither works this way :/ now I'm not a skilled programmer, but I'm having difficulties seeing MS's logic here

Me Too.... does it work with ShellExecuteEx?

I haven't tried, as I get the "stdafx.h : no such file or directory." Error when i tried ShellExecuteex the last time :/

Don't #include "stdafx.h" . Those are pre-compiled headers only used with VS, just just leave the line out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.