C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 06-12-2007, 12:05 PM   #1
Registered User
 
Join Date: Jun 2007
Posts: 19
system calls

I am trying to port a DOS batch script to C. Whenever I try to use a system call with a variable, I get an error. For example, here is what I am doing:

Code:
char IP[15];

scanf("%s", &IP);

system("ping IP");
I usually get an error stating that IP is not defined or known. Can anyone shed any light?


Thanks.


-KiaiViper
kiai_viper is offline   Reply With Quote
Old 06-12-2007, 12:23 PM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
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().
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 06-12-2007, 01:12 PM   #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
I have tried to use double backslashes "\\" and double quotes " "" ", but the external application does not execute. Here is what I am currently doing:

Code:
spawnl( P_WAIT, "\"c:\\Program Files\\program.exe\"", " parameter1", NULL );

Thanks for your help.
kiai_viper is offline   Reply With Quote
Old 06-12-2007, 01:37 PM   #4
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,768
Quote:
Originally Posted by kiai_viper View Post
Code:
spawnl( P_WAIT, "\"c:\\Program Files\\program.exe\"", " parameter1", NULL );
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   Reply With Quote
Old 06-13-2007, 09:09 AM   #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   Reply With Quote
Old 06-13-2007, 09:46 AM   #6
and the hat of Jobseeking
 
Salem's Avatar
 
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
}
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 06-13-2007, 10:05 AM   #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   Reply With Quote
Old 06-13-2007, 11:34 AM   #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 " );
   
}
Thank you very much for your help!

Last edited by kiai_viper; 06-13-2007 at 12:21 PM.
kiai_viper is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:55 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22