CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Hybrid View

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Running a system command

    I'm building a complex C++ project using MSVC. As a pre-build step it runs a small Perl script (called "fixup.pl") whose job is to configure certain files which then get used later in the chain. Suppose (from within fixup.pl) I wanted to run a system process (such as an exe program or a DOS batch file). Is it possible to do that from a Perl script?
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Running a system command

    I discovered that Perl offers a system command which is quite simple to use - e.g.

    Code:
    	@my_command = ( "the_exe_name", "arg1", "arg2", "etc"  );
    
    	system(@my_command);
    In fact, if the exe returns an error status you can even test it like so:-

    Code:
    	system(@my_command);
    	if ($? != 0) {
    		print "An error occurred while executing \"the_exe_name\"\n";
    	}
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Running a system command

    Bumping this question as a new problem has come to light....

    The perl system command seems to work - AND - if the launched program outputs any text, I can see the text in my command prompt (assuming I originally ran my perl script from a command prompt) - BUT - if I attempt to re-direct that output to a file - e.g.

    fixup.pl > output.txt
    it seems to work in Windows 7 - but not in Windows 8 or XP. It doesn't work - even if I attempt to do the redirection from within my perl script - e.g.

    Code:
    	@my_command = ( "the_exe_name", "arg1", "arg2", "etc", ">", "output.txt"  );
    
    	system(@my_command);
    Can anyone suggest a workaround to make redirection work as expected? I'm by no means a perl expert so I'm probably just doing something wrong
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Running a system command

    redirection in that way isn't part of the normal application parameters (in fact the application doesn't even know about the redirection per se).

    I'm not sure why this would have worked in W7 and not in W8/XP, maybe you had some special execution processor installed that took care of this.

    Typically speaking, if you want redirection, you either have to do it "the hard way", by creating the file objects handles, then making the execute process adopt those handles as standard input and standard output.
    ALternatively, you can take an easy way out and simply run everyting through the command processor. So the command would then end up being something like:

    %comspec% /C the_exe_name arg1 arg2 arg3 > output.txt

    you may need to get the "%comspec%" variable from the environment yourself and substitute it directly.

  5. #5
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Running a system command

    Quote Originally Posted by OReubens View Post
    ALternatively, you can take an easy way out and simply run everyting through the command processor.
    Hi OReubens. I'm not very familiar with Perl but I assumed that's what its system command did?

    I found this new set of perl statements. These ones work in Windows 7 and Windows XP - but not for Windows 8

    Code:
            use IPC::Run3;
            my $output;
            run3(["the_exe_name", "arg1", "arg2", "etc"], \undef, "output.txt");
    It just seems to be impossible to find one set of code that'll work everywhere! Do you happen to know if it's possible to get system information using a Perl script? In particular, is there a way to get the value of WINVER?
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  6. #6
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Running a system command

    As the rest of the project seems to be in c++, why not rewrite the perl script in c++?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Running a system command

    Quote Originally Posted by 2kaud View Post
    As the rest of the project seems to be in c++, why not rewrite the perl script in c++?
    Yeah, I must admit I'm seriously contemplating that. If I rewrite it in C++ it'll most likely work first time. And even if it doesn't, at least I know I'll be able to fix it !

    I don't understand why programmers keep struggling with all this scripting stuff. It almost never works reliably in my experience - and even if you can make it work on one machine it rarely works the same on somebody else's!
    "A problem well stated is a problem half solved.” - Charles F. Kettering

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured