Re: Running Command line programs from VC++
- From: "Severian [MVP]" <severian@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 26 Apr 2005 11:56:22 GMT
On Tue, 26 Apr 2005 03:49:02 -0700, "MSalters"
<MSalters@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
>> ITYM system(). It's also defined in <stdlib.h> if you're already
>> including that.
>>
>> system() is appropriate if your program is a console program and you
>> want the output to be visible to the user. If your program is a
>> Windows program, a console window will pop up and disappear once the
>> command is done.
>>
>> While system() is portable and often useful, you can have more control
>> (hiding the console window, piping data to and from the program, etc.)
>> using CreateProcess().
>
>True, but system will execute a command like "RMDIR /S/Q dirname". That's
>one function stilll missing from the shell API. It would be very useful if
>ShFileOperation(FO_DELETE) did that. Not that I expect it to work soon;
>FO_COPY doesn't create directories, FOF_SILENT only removes a few message
>boxes (so a window handle is still needed), and it's error reporting is a user
>filing a bug report complaining my application failed. (XP SP1)
Here's the code I use to remove a folder and all its contents (and
subfolders):
static BOOL SevDelFolder(HWND hwnd, PTSTR pdirname)
{
SHFILEOPSTRUCT shfos = {0};
TCHAR name[MAX_PATH+2];
int err;
_tcscpy(name, pdirname);
name[_tcslen(name)+1] = 0; /* Double-null terminated */
shfos.hwnd = hwnd;
shfos.wFunc = FO_DELETE;
shfos.pFrom = name;
shfos.pTo = NULL;
shfos.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION
| FOF_NOERRORUI | FOF_SILENT;
if ( (err = SHFileOperation(&shfos)) != NO_ERROR) {
SetLastError(err);
return SysMessage(hwndMain, ERR_DELETEDIR, pdirname);
}
return TRUE;
}
SysMessage is my routine that reports my error message and translates
GetLastError() as additional information. It's usually sufficient for
the user to understand why my program was unable to delete the tree.
I've never had any complaints or bug reports that this failed to work
or failed to produce an appropriate error message upon failure (it's
running on hundreds of thousands of computers from 95 to 2k3).
--
Phillip Crews aka Severian
Microsoft MVP, Windows SDK
Posting email address is real, but please post replies on the newsgroup.
.
- References:
- Running Command line programs from VC++
- From: Henry
- Re: Running Command line programs from VC++
- From: Mark Randall
- Re: Running Command line programs from VC++
- From: Severian [MVP]
- Re: Running Command line programs from VC++
- From: MSalters
- Running Command line programs from VC++
- Prev by Date: RE: Volatile + multithreading
- Next by Date: Re: What is a union?
- Previous by thread: Re: Running Command line programs from VC++
- Next by thread: Returning BSTR to VB
- Index(es):
Relevant Pages
|