![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 144
Rep Power: 0
![]() |
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;
}
/*
* ...
*/
} |
|
|
|
|
|
#2 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 598
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 144
Rep Power: 0
![]() |
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] |
|
|
|
|
|
#4 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 879
Rep Power: 4
![]() |
Does the batch file name have a space in it? system() would then treat it as a command and a parameter.
|
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 144
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#6 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 879
Rep Power: 4
![]() |
Can you zip up your project and attach it here? I have CBuilder 5, so I could try it on that.
|
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 144
Rep Power: 0
![]() |
I am at work now, but by the weekend I will be able to have it up.
|
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 144
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#9 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 879
Rep Power: 4
![]() |
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 + "\""; 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. |
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 144
Rep Power: 0
![]() |
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 \" + ... + \" |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|