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: 146
Rep Power: 4 L7Sqr is on a distinguished road
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: 599
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: 146
Rep Power: 4 L7Sqr is on a distinguished road
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: 893
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 offline   Reply With Quote
Old Jun 16th, 2005, 10:18 PM   #5
L7Sqr
Hobbyist Programmer
 
Join Date: Jun 2005
Location: here
Posts: 146
Rep Power: 4 L7Sqr is on a distinguished road
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: 893
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 offline   Reply With Quote
Old Jun 18th, 2005, 4:36 PM   #7
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
I have seen what you mean, so I guess it's possible, but it might be luck. I don't know how that would be done though.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Jun 18th, 2005, 7:43 PM   #8
drunkenCoder
Newbie
 
drunkenCoder's Avatar
 
Join Date: Nov 2004
Posts: 8
Rep Power: 0 drunkenCoder is on a distinguished road
Post

sup y'all
I came across this the other day at http://www.onecomputerguy.com/windowsxp_tips.htm

To prevent applications from stealing the focus from the window you are working:

Start Regedit
Go to HKEY_CURRENT_USER \ Control Panel \ Desktop
Edit the key ForegroundLockTimeout
Give it a value of 00030d40

I have never had the need to use it, but I hope it works.
drunkenCoder is offline   Reply With Quote
Old Jun 18th, 2005, 9:15 PM   #9
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
Here is a program you can call from your batch file to send the console window to the back of the Z order. I am not sure whether you will be able to compile it under Builder 4 as it uses GetConsoleWindow which is only available in Windows XP and 2000.
// SendToBack
// Send the current console window to the back
#define _WIN32_WINNT 0x0500
#include <windows.h>

int main(int argc, char *argv[])
{
  HWND hWnd = GetConsoleWindow();
  if (hWnd != NULL)
    SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);

  return 0;
}

If you can't get this going, you could try using the SetConsoleWindowInfo function, which is available on Windows95 and above, however this function only allows you to set the size and position of the window, so you could just move them off to the side somewhere.
The Dark 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 2:13 PM.

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