system calls

This is a discussion on system calls within the Networking/Device Communication forums, part of the General Programming Boards category; I am trying to port a DOS batch script to C. Whenever I try to use a system call with ...

  1. #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

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    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.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #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.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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.

  5. #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.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    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.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #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;
    }

  8. #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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic system calls help.
    By AmbliKai in forum C Programming
    Replies: 5
    Last Post: 03-21-2008, 07:18 AM
  2. Opinions on custom system build
    By lightatdawn in forum Tech Board
    Replies: 2
    Last Post: 10-18-2005, 04:15 AM
  3. Problem With My Box
    By HaVoX in forum Tech Board
    Replies: 9
    Last Post: 10-15-2005, 07:38 AM
  4. School Mini-Project on C/C++ (Need Your Help)..
    By EazTerence in forum C++ Programming
    Replies: 4
    Last Post: 09-08-2005, 01:08 AM
  5. System Calls && Variables
    By Okiesmokie in forum C++ Programming
    Replies: 6
    Last Post: 03-06-2002, 08:10 PM

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