![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5
![]() |
#include <windows.h>
#include <stdio.h>
int main(void)
{
char *run;
printf("\nCommand:>");
scanf("%s",run);
WinExec(run,SW_SHOW);
return 0;
}![]()
__________________
http://www.white-scorpion.nl |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Well, what happens?
|
|
|
|
|
|
#3 |
|
Programming Guru
![]() ![]() ![]() |
I hate pointers, I avoid using pointers like so... (btw, if it wasn't obvious this is C++)
#include <windows.h> #include <iostream> #include <string> int main(void) { string run; cout << "\nCommand:>"; cin >> run; WinExec(run.c_str(), SW_NORMAL); return 0; } --- Typing 'control.exe' as your command, will bring up the Control Panel.
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#4 | |
|
Programmer
|
Quote:
I mean, really. Do you want to be responsible for telling someone to use CIN and COUT instead of scanf and printf?? Dont' be scared of pointers, embrace them. They are hella powerful.
__________________
ALLOW IMAGES IN SIGNATURES NOW |
|
|
|
|
|
|
#5 | ||
|
Programming Guru
![]() ![]() ![]() |
#include <windows.h>
#include <stdio.h>
int main(void)
{
char run[50];
printf("\nCommand:>");
scanf("%s",run);
WinExec(run,SW_SHOW);
return 0;
}Ok... so here is the C code to run a command less than 50 characters. Quote:
![]() Quote:
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
||
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5
![]() |
what a response
![]() thanks for the help, now i have only one question left: #include <windows.h>
#include <stdio.h>
int main(void)
{
*char run[50];
*printf("\nCommand:>");
*scanf("%s",run);
*WinExec(run,SW_SHOW);
*return 0;
}
__________________
http://www.white-scorpion.nl |
|
|
|
|
|
#7 | |
|
Programming Guru
![]() ![]() ![]() |
Quote:
Notice the line: int main(int argc, char *argv[]) This is how you get command line parameters into your program. 'argc' is the parameter count, 'argv' is the array of parameters. argv[] starts at index 0. So when you compile this code into an exe... you run like so: C:\myRun control.exe Control panel will open up for you. Note: argv[0] is the program's name (in this case the value us 'myRun') argv[1] is the first exe you want to run...
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
|
#8 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Erm... I think he means in the WinExec function. This is what you need to do. I'm assuming it doesn't already work as it is, as I can't be bothered to test it (remember, if there are spaces in the path, you need to use double-quotes).
If there are quote marks encompassing the path, separate the path from the arguments by using two different variables ("path" and "args" are probably your best choices). If not, first check the entire string isn't a file by opening it for reading and checking the return value (look at fopen() ). If it is, execute it. Otherwise, assume the path is the entire string up to the first space. Separate the path and the arguments and add quotes round the path. Finally, do this: run = strcat(0x20, path); run = strcat(args, run); WinExec(run, SW_SHOW); ![]() |
|
|
|
|
|
#9 | |
|
Programming Guru
![]() ![]() ![]() |
The code I provided earlier works fine... to accomplish parameters. Compile it into an exe and run it as follows (be sure to include the "):
Assuming you named it 'myRun': myRun "c:\program files\internet explorer\iexplore.exe http://www.programmingforums.org" --- Assuming you want to run multiple exes: You could modify the code to do this: WinExec(argv[1],SW_SHOW); WinExec(argv[2],SW_SHOW); WinExec(argv[3],SW_SHOW); To allow for this: myRun prog1 prog2 prog3 Quote:
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
|
#10 | |
|
Expert Programmer
|
Quote:
char* run; however when that is initialized it will point to 0xcdcdcdcd, NULL, or some random memory address depending on the compiler and debugging options built into the project. The step you are missing is actuuallt setting aside memory to point to. run = new char; This will set aside 1 char of memory and assign the memory address the pointer need into run, so that it may be used. If you only intend on using 1 byte, or you intend on using a fixed array of bytes, try using something more like this char run; scanf( ..., &run ); The ampersand takes the memory address of run instead of the value of run. Watch out for buffer overflows though, if you read values directly into an array that is not large enough to hold the input you will likely corrupt your heap/stack.
__________________
Clifford Matthew Roche <geek@cliffordroche.com> Web Hosting: http://www.crd-hosting.com Consulting: http://www.crdev-consulting.com |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|