Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Jun 15th, 2005, 8:36 PM   #1
L7Sqr
Hobbyist Programmer
 
Join Date: Jun 2005
Location: here
Posts: 144
Rep Power: 0 L7Sqr is an unknown quantity at this point
system () command in Borland C++ Bldr. 4

Here is what I am trying to do:
From a TMemo object I am pulling a line out (full path name) and chopping the string to the file name (a batch file). No problem.
From there I want to execute the batch file by means of a system () command. Each time I try this, I get a dos window open/shut and the system command returns an error (non-zero).
errno is supposed to be set accordingly, but when I check it, I get an invalid value. Valid values include: ENOENT, ENOEXEC, ENOMEM, none of which I get. When I check, the errno is set to "Error 0", which to me means, no error....
However, system returns non-zero when passed a NULL pointer (indicating that a command processor IS available) and returns 1 (which is not indicated as a possible return value) when I pass a valid command.
Anyone have a sugestion on where I might be going wrong?
I have tried copying the file to the cwd to avoid PATH issues, but to no avail.

So you can avoid the obvious:
- yes, it is a valid batch file (it runs when invoked seperately)
- yes, I parsed the string properly - its the first thing I checked.
- I have tried both the full path and just the file name - same results.

My windows experience is limited, so please bear with me.
Thanks in advance for the help.

the code snippet, in case it helps you...
while (frmMain->memList->Lines->Count != 0) {
   batch = frmMain->memList->Lines->Strings[0];
   pos = batch.LastDelimiter("\\") + 1;
   len = batch.Length () - pos;
   sub = batch.SubString (pos, len + 1);
   error = system (batch.c_str ());
   if (error) {
      if (errno == ENOENT) {
         msg = "Path not found";
      } else if (errno == ENOEXEC) {
         msg = "Exec format error";
      } else if (errno == ENOMEM) {
         msg = "No memory";
      } else {
         msg = "Unknown error: " + IntToStr(error);
      }
      MessageBoxA (frmMain->Handle, msg.c_str(), "Error msg", 0);
      frmMain->btnAddFile->Enabled = false;
      return;
   }
   /*
    * ...
    */
}
L7Sqr is offline   Reply With Quote
Old Jun 15th, 2005, 10:07 PM   #2
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 598
Rep Power: 4 Ancient Dragon is on a distinguished road
with VC++ 6.0, I get 0 when the batch file is executed, and 1 if the batch file cannot be found (errno = 0, as you indicated). I think errno is 0 because the problem is not any of the indicated error numbers.

ShellExecute() may give you better errors.

Last edited by Ancient Dragon; Jun 15th, 2005 at 10:31 PM.
Ancient Dragon is offline   Reply With Quote
Old Jun 15th, 2005, 10:35 PM   #3
L7Sqr
Hobbyist Programmer
 
Join Date: Jun 2005
Location: here
Posts: 144
Rep Power: 0 L7Sqr is an unknown quantity at this point
Yeah, I though of that as well, but the file (path included) is inserted into the memo object by way of an OpenFileDialog.....
I actually have the ReadOnly flag of the memo object set to true so a user wouldnt be able to change the text.
Any other reason one can imagine I would get a 1 returned from the system command? Searching Borland's help files only mentions negative return on error and zero for success. (However, Borland simply calls Microsoft's built-in command, so I dont expect to find an extensive explanation).
I'm retiring for the evening, but will search fresh in the morn...

[edit]
Oooh...Just caught the ShellExecute... I'll work with that tomorrow. Thanks!
[/edit]
L7Sqr is offline   Reply With Quote
Old Jun 16th, 2005, 9:36 PM   #4
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 879
Rep Power: 4 The Dark is on a distinguished road
Does the batch file name have a space in it? system() would then treat it as a command and a parameter.
The Dark is online now   Reply With Quote
Old Jun 16th, 2005, 10:18 PM   #5
L7Sqr
Hobbyist Programmer
 
Join Date: Jun 2005
Location: here
Posts: 144
Rep Power: 0 L7Sqr is an unknown quantity at this point
I tried that for the copy...
   pos = batch.LastDelimiter("\\") + 1;
   len = batch.Length () - pos;
   sub = batch.SubString (pos, len + 1);
   cmd = "copy /Y \"" + batch + "\" " + sub;
   /*
    * double check the syntax
    */
   if (debug) 
      frmMain->memList->Lines->Add (cmd);
   error = system (cmd.c_str());

   /*
    *
    */

With the same result...
[edit]
where cmd is the file ping_test.bat
[/edit]

Last edited by L7Sqr; Jun 16th, 2005 at 10:22 PM.
L7Sqr is offline   Reply With Quote
Old Jun 17th, 2005, 12:17 AM   #6
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 879
Rep Power: 4 The Dark is on a distinguished road
Can you zip up your project and attach it here? I have CBuilder 5, so I could try it on that.
The Dark is online now   Reply With Quote
Old Jun 17th, 2005, 12:04 PM   #7
L7Sqr
Hobbyist Programmer
 
Join Date: Jun 2005
Location: here
Posts: 144
Rep Power: 0 L7Sqr is an unknown quantity at this point
I am at work now, but by the weekend I will be able to have it up.
L7Sqr is offline   Reply With Quote
Old Jun 17th, 2005, 7:20 PM   #8
L7Sqr
Hobbyist Programmer
 
Join Date: Jun 2005
Location: here
Posts: 144
Rep Power: 0 L7Sqr is an unknown quantity at this point
Here is the zipped file.
I included the file I am trying to run in the bat directory. (ping_yahoo.bat)
It should queue up the files as they are being processed, but
I cant get past the copy part.
Attached Files
File Type: zip ProjBatchBuilder.zip (18.7 KB, 32 views)
L7Sqr is offline   Reply With Quote
Old Jun 17th, 2005, 8:19 PM   #9
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 879
Rep Power: 4 The Dark is on a distinguished road
The copy part didn't work because the copy command was trying to copy the batch file onto itself. I suspect the open file dialog has changed the current directory.

Changing the cmd to
         cmd = "\"" + batch + "\"";
Made it work on my PC. It pinged yahoo and then returned normally - note that it then just looped and ran the batch file again, until I pressed Control-C on the batch file, which then returned an error to system.

The error returned from system() is whatever the program being run returns.

To get more info on what is happening, stick "cmd /k " in from of any command you want to run - this will leave the command interpreter up when it is done running the copy/batch file. You then just type exit to return to your program.

I also had some problem because I was running through remote desktop back to work (where builder is). It reminded me how much I hate builder (at least version 5) - it seems to lock up every now and then for no good reason.
The Dark is online now   Reply With Quote
Old Jun 17th, 2005, 10:42 PM   #10
L7Sqr
Hobbyist Programmer
 
Join Date: Jun 2005
Location: here
Posts: 144
Rep Power: 0 L7Sqr is an unknown quantity at this point
First, thanks.

Yeah, I put the copy in to try and avoid PATH issued that were mentioned in the help files (of Borland), obviously not a problem here.
I didnt finish the function body because I couldnt get the system command working (hence the infinite loop).
Slick how the dialog changes the directory, glad you helped with that one, I'd still be searching. (man, do I miss gdb here).
On the /K switch, its a good piece of info, unfortunately not applicable here, this is something that might run several programs overnight, so having to close the window to continue to the next file defeats part of the purpose.

I appreciate the help. Tomorrow I'll get a good run at it with the full path ala \" + ... + \"
L7Sqr 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 6:55 PM.

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