Re: Running Command line programs from VC++



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.
.



Relevant Pages

  • Re: working set and console applications
    ... I created a windows application and then I created a child console process ... that I started from the parent process using CreateProcess. ... I minimize the child console window it also causes the working set of the ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Console applications window size
    ... AllocConsole() or AttachConsole. ... You already have a console, ... SMALL_RECT srWindowRect; // hold the new window size ... >exe from a cmd prompt, the same cmd prompt gets bigger properly. ...
    (microsoft.public.vc.language)
  • Re: How to minimize command mode window in console program?
    ... to set some property to minimize the console window. ...     ByVal hWnd As IntPtr, ...     End Sub ...
    (microsoft.public.dotnet.languages.vb)
  • Re: vb6 run from and output to command window
    ... VB doesn't really create true console applications (because the linker ... allocate the console window, and read/write from/to it. ... Private Declare Function AllocConsole Lib "kernel32" As Long ... Dim lResult As Long ...
    (microsoft.public.vb.general.discussion)
  • Re: Prevent Domain Users From Browsing Around in Active Directory?
    ... View, Customize, uncheck everything except the top one that says Console ... But they will have the rt-click feature. ... Rt-click on that OU, new window from here. ...
    (microsoft.public.windows.server.active_directory)