Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 6th, 2004, 8:24 AM   #1
lepricaun
Hobbyist Programmer
 
lepricaun's Avatar
 
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5 lepricaun is on a distinguished road
#include <windows.h>
#include <stdio.h>
int main(void)
{
  char *run;
  printf("\nCommand:>");
  scanf("%s",run);
  WinExec(run,SW_SHOW);
  return 0;
}
can someone please tell me what i've done wrong? i've read several tut's on pointers now, but still can't get this to work
__________________
http://www.white-scorpion.nl
lepricaun is offline   Reply With Quote
Old Aug 6th, 2004, 8:35 AM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Well, what happens?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Aug 6th, 2004, 8:40 AM   #3
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Aug 6th, 2004, 10:30 AM   #4
Kylixen
Programmer
 
Kylixen's Avatar
 
Join Date: Jun 2004
Location: SC
Posts: 60
Rep Power: 5 Kylixen is on a distinguished road
Send a message via AIM to Kylixen Send a message via MSN to Kylixen
Quote:
Originally posted by Infinite Recursion@Aug 6 2004, 01:40 PM
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.
scanf("%s", &variable_that_is_not_a_pointer);

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
Kylixen is offline   Reply With Quote
Old Aug 6th, 2004, 10:47 AM   #5
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
#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:
I mean, really. Do you want to be responsible for telling someone to use CIN and COUT instead of scanf and printf??
Yes, I do.

Quote:
Dont' be scared of pointers, embrace them. They are hella powerful.
Pointers have their uses... when they are not necessary, I don't use them.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Aug 6th, 2004, 12:12 PM   #6
lepricaun
Hobbyist Programmer
 
lepricaun's Avatar
 
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5 lepricaun is on a distinguished road
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;
}
how must i alter this to take parameters? like "c:\program files\internet explorer\iexplore.exe http ://www. programmingforums.org"
__________________
http://www.white-scorpion.nl
lepricaun is offline   Reply With Quote
Old Aug 6th, 2004, 12:32 PM   #7
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Quote:
#include <windows.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
/*
char run[50];
printf("\nCommand:>");
scanf("%s",run);
*/

WinExec(argv[1],SW_SHOW);
return 0;
}

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."
Infinite Recursion is offline   Reply With Quote
Old Aug 6th, 2004, 2:59 PM   #8
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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);
If there ever was a problem in the first place, that should fix it
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Aug 6th, 2004, 3:18 PM   #9
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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:
I'm assuming it doesn't already work as it is
Works fine... and fairly flexible.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Aug 7th, 2004, 12:19 AM   #10
kurifu
Expert Programmer
 
kurifu's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia (Canada)
Posts: 784
Rep Power: 5 kurifu is on a distinguished road
Send a message via ICQ to kurifu Send a message via MSN to kurifu
Quote:
Originally posted by lepricaun@Aug 6 2004, 01:24 PM
#include <windows.h>
#include <stdio.h>
int main(void)
{
  char *run;
  printf("\nCommand:>");
  scanf("%s",run);
  WinExec(run,SW_SHOW);
  return 0;
}
In this example you have successfuly set aside memory to hold a pointer address

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 &lt;geek@cliffordroche.com&gt;
Web Hosting: http://www.crd-hosting.com
Consulting: http://www.crdev-consulting.com
kurifu is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:07 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC