Re: system calls

From: Joseph M. Newcomer (newcomer_at_flounder.com)
Date: 11/03/04


Date: Wed, 03 Nov 2004 14:31:23 -0500

Using system() presumes that COMSPEC is set to cmd.exe, not a safe assumption.

ShFileOperation will copy files. Not a major issue.

Writing a simple recursive file-deleter is fairly straightforward; I used to use a
recursive directory treewalk as a lab exercise, expecting students to finish it completely
in about 45 minutes with little previous experience. Of course, a simple search of
someplace like www.codeproject.com will reveal lots of useful information. It took me less
than 5 minutes to locate this article:

http://www.codeproject.com/file/alexfileoperations.asp

which also shows how to do all of the operations you want WITHOUT using ShFileOperation!

CreateProcess is fairly trivial

        STARTUPINFO startup = { sizeof(startup) };
        PROCESS_INFORMATION info;

        CreateProcess(NULL,
                                                   program_and_args,
                        NULL,
                        NULL,
                        FALSE,
                        CREATE_NO_WINDOW,
                        NULL,
                        NULL,
                        &startup,
                        &info);

does not look terribly complicated, and I fail to see what could possibly be
"unsatisfactory". If you really want to run a command line
        cmd.exe /c "xcopy whatever"
works just fine. Of course, if ANYTHING goes wrong, you haven't a clue as to whether the
opertion succeeded or failed, or why, which is why I would never use the Unix foolishness
of launching a shell (I detested this technique in Unix, where I spent about 15 years, and
I still detest it, because it is impossible to determine what went wrong, report it in a
meaningful way, or recover from the error in a meaningful way).

It is amazing how many times I have to rip out system() calls from programs because there
are better ways to do the operation from within the API, and be able to report to the user
MEANINGFUL error messages when the operation fails. I have yet to find a program that
cannot be improved by the removal of system() calls.
                                        joe

On Wed, 3 Nov 2004 17:28:14 +0000 (UTC), "Kevin" <nospam@krw66.freeserve.co.uk> wrote:

>I agree that using API calls is much superior to using system. In some
>instances I have little choice as I am using console apps ported from Linux.
>In other instances I would like to use API calls but can find nothing
>appropriate. For example I can find no API call to delete a directory, its
>sub-directories and all their contents but this can be achieved easily with
>system("rd /s /q"). Similiar I can find no API call to copy a directory and
>it's sub-directories and contents but this can be done with system("xcopy
>...");
>
>I have looked at CreateProcess but found it complicated and unsatisfactory.
>Another poster supplied some code which used CreateProcess but it still
>caused command windows to appear.
>
>K
>
>"Joseph M. Newcomer" <newcomer@flounder.com> wrote in message
>news:46tgo0pom92fnng1lsfs3dmgnk1r45k7t3@4ax.com...
>> You mean how to keep the rather quaint and antiquated "system()" call from
>popping up a
>> DOS box? The answer is simple. Stop using something that obsolete and use
>CreateProcess to
>> create a process without a visible console.
>>
>> Also, I have discovered that about 90% of the time someone uses system(),
>it is because
>> they don't know that there is a better way to accomplish what they want to
>do. For
>> example, the most common use I've seen is to do a "dir *.*", capture the
>output lines, and
>> use them to iterate over the files in the directory. This is absurd.
>Another common use is
>> to call the Copy command or Move command, when CopyFile and MoveFile APIs
>exist and do a
>> better job, particularly when there are issues of decent error detection
>and recovery
>> involved.
>>
>> Most important question: what are you doing with this call? Knowing what
>you are doing
>> helps us suggest alternative mechanisms.
>> joe
>>
>>
>> On Tue, 2 Nov 2004 17:28:08 +0000 (UTC), "Kevin"
><nospam@krw66.freeserve.co.uk> wrote:
>>
>> >Is it possible to make system calls without a DOS box/command windows
>> >popping up? I'm making calls with the system function which always seems
>to
>> >cause this. I'm using various console applications. Anyway to avoid the
>> >windows popping up? Or at least have them behind the main window? Or even
>> >just in one place so the don't dance all over the screen?
>> >
>> >Cheers,
>> >K
>> >
>>
>> Joseph M. Newcomer [MVP]
>> email: newcomer@flounder.com
>> Web: http://www.flounder.com
>> MVP Tips: http://www.flounder.com/mvp_tips.htm
>

Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm



Relevant Pages

  • CreateProcess for 16 bit applications failes for Application Verifier
    ... I have used "CreateProcess" API. ... Passing a NULL lpApplicationName to CreateProcess ... with some command line atttributes then it works fine. ... So now how to pass my application through Application Verifier which ...
    (microsoft.public.win32.programmer.kernel)
  • CreateProcess not working as doc specifies.
    ... I am having some difficulty using CreateProcess() to run a batch file and get ... you must start the command interpreter; ... the test.bat (should return exit code 1 and does when run from the command ...
    (microsoft.public.win32.programmer.kernel)
  • Re: CreateProcess() Not Passing Command Line
    ... LPSECURITY_ATTRIBUTES lpProcessAttributes, ... LPSTARTUPINFO lpStartupInfo, ... Use QueryAssocString to get open command line for a document. ... > My program will then use CreateProcess() to open the file. ...
    (microsoft.public.win32.programmer.kernel)
  • RE: call rsh from .NET - source code?
    ... With an API? ... calling the command line from a command prompt. ... Yes, it is not an extremely sexy methodology, ...
    (microsoft.public.dotnet.framework)
  • Re: Starting an MFC application with CreateProcess Api
    ... You can form the command line using sprintf, ... I am trying to start a dialog based MFC application using CreateProcess ... CString cmd; ... i start my application using CreateProcess Api, ...
    (microsoft.public.vc.mfc)

Quantcast