![]() |
| | #1 |
| Registered User Join Date: Jun 2007
Posts: 19
| system calls Code:
char IP[15];
scanf("%s", &IP);
system("ping IP");
Thanks. -KiaiViper |
| kiai_viper is offline | |
| | #2 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,710
| Use sprintf() / strcat() etc to construct the command line in a char buffer, then pass the result to system. See the FAQ on better ways of running programs than using system(). |
| Salem is offline | |
| | #3 |
| Registered User Join Date: Jun 2007
Posts: 19
| Thanks for the info. I reviewed the FAQ and am successfully able to execute external commands. My question now is: how do I input the location of a program such as: Code: C:\Program Files\program.exe Code: spawnl( P_WAIT, "\"c:\\Program Files\\program.exe\"", " parameter1", NULL ); Thanks for your help. |
| kiai_viper is offline | |
| | #4 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,768
| I don't think you have to put the program name in double quotes. So remove those two instances of \" at the beginning and end of the second parameter. And the space in front of " parameter1" isn't necessary and might bork things up. |
| brewbuck is offline | |
| | #5 |
| Registered User Join Date: Jun 2007
Posts: 19
| Thanks for the response. However, the problem remains. I have removed the double quotes and double slashes. I am trying to execute Nmap from my C program. Nmap is installed in the "Program Files" directory. When I run my program, it errors out the program files directory because of the two words. |
| kiai_viper is offline | |
| | #6 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,710
| Which compiler are you using? Post your latest code. spawn is an archaic DOS interface, it probably doesn't understand long filenames (it doesn't here). Code: #include <iostream>
#include <fstream>
#include <process.h>
#include <windows.h>
using namespace std;
int main()
{
// with long filename, nada, zip, zilch
// spawnl( P_WAIT, "\"c:\\Program Files\\PFE\\pfe32.exe\"", NULL );
// with substituted 8.3 equivalents, success
spawnl( P_WAIT, "c:\\Progra~1\\PFE\\pfe32.exe", NULL );
// or if you're writing a win32 program, use a win32 API and it's all good
char szPath[] = "C:\\Program Files\\WinZip\\winzip32.exe";
PROCESS_INFORMATION pif; //Gives info on the thread and..
//..process for the new process
STARTUPINFO si; //Defines how to start the program
ZeroMemory(&si,sizeof(si)); //Zero the STARTUPINFO struct
si.cb = sizeof(si); //Must set size of structure
BOOL bRet = CreateProcess(
szPath, //Path to executable file
NULL, //Command string - not needed here
NULL, //Process handle not inherited
NULL, //Thread handle not inherited
FALSE, //No inheritance of handles
0, //No special flags
NULL, //Same environment block as this prog
NULL, //Current directory - no separate path
&si, //Pointer to STARTUPINFO
&pif); //Pointer to PROCESS_INFORMATION
}
|
| Salem is offline | |
| | #7 |
| Registered User Join Date: Jun 2007
Posts: 19
| Thanks for the info. Here is my current code: Code: #include <stdio.h>
#include <process.h>
int main(void)
{
spawnl( P_WAIT, "c:\\Program Files\\Nmap\\Nmap.exe", " PARAMETER", NULL );
system("PAUSE");
return 0;
}
|
| kiai_viper is offline | |
| | #8 |
| Registered User Join Date: Jun 2007
Posts: 19
| Okay guys, I think I have solved the problem (of course with your help ) Here is my new code:Dev-C++ (using gcc) Code: #include <windows.h>
#include <stdio.h>
int Exec(char szPath[])
{
PROCESS_INFORMATION pif;
STARTUPINFO si;
ZeroMemory(&si,sizeof(si));
si.cb = sizeof(si);
BOOL bRet = CreateProcess(szPath," 192.168.1.10",NULL,NULL,FALSE,0,NULL,NULL,&si,&pif);
if (bRet == FALSE)
{
MessageBox(HWND_DESKTOP,"Unable to start program","",MB_OK);
return 1;
}
CloseHandle(pif.hProcess);
CloseHandle(pif.hThread);
return 0;
}
int main()
{
Exec( "C:\\Program Files\\Nmap\\Nmap.exe " );
}
Last edited by kiai_viper; 06-13-2007 at 12:21 PM. |
| kiai_viper is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Basic system calls help. | AmbliKai | C Programming | 5 | 03-21-2008 07:18 AM |
| Opinions on custom system build | lightatdawn | Tech Board | 2 | 10-18-2005 04:15 AM |
| Problem With My Box | HaVoX | Tech Board | 9 | 10-15-2005 07:38 AM |
| School Mini-Project on C/C++ (Need Your Help).. | EazTerence | C++ Programming | 4 | 09-08-2005 01:08 AM |
| System Calls && Variables | Okiesmokie | C++ Programming | 6 | 03-06-2002 09:10 PM |