Jump to content
Tuts 4 You

using system function to run a dos command?


D3ADB33F

Recommended Posts

hi, 


 


how would one run this command from within a console application; 


 


fsutil usn deletejournal /D C:


 


without loading a seperate window etc.


 


C++


Link to comment

I imagine there are a couple of ways to do this. 


A shell command would work, but a more elegant way is


the following which I use in my "PunchIt"


application. If you look at the comments in the code you can play


around with it to do pretty much what you'd like. Before I start


the hidden console process, I create the command line for the


program. The tempfile contains the name of the program being


executed which in this case is a PECompact console program.


I build the commandline and its associated parameters:



// command string arguments passed to the PECompact2 console program
void InitPEC2(char *cbuffer)
{
/* init bbuffer */
memset(bbuffer,0,sizeof(bbuffer));
if (bBKSEL)
{
// Default is backup the output executable file prior to compressing
sprintf(bbuffer,"\"%s\" "
"\"%s\" "
"/LoaderCodecHost:\"pec2codec_aplib.dll\" "
"/CodecHost:pec2codec_lzma.dll "
"/Cr:No",
tempfil1, cbuffer);
}
else
{
// Backup the original uncompressed output executable
sprintf(bbuffer,"\"%s\" "
"\"%s\" "
"/LoaderCodecHost:\"pec2codec_aplib.dll\" "
"/CodecHost:pec2codec_lzma.dll "
"/NoBackup "
"/Cr:No",
tempfil1, cbuffer);
}
return;
}

Then I run the console program in the background.



// The following function is a shortened variant of Q190351 - HOWTO: Spawn Console Processes with Redirected Standard Handles
// Included is commented code for creating a console window and extracting the output buffer as a possible alternative
// Run via CreateProcess the PECompact2 console program and provide feedback
BOOL RunPEC2(void)
{
// If so desired, set the bInheritHandle flag so pipe handles are inherited.
ZeroMemory( &sa, sizeof(sa) );
sa.nLength = sizeof(sa);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL; /* NOTE:If using anonymous pipes include below, This is more elegant because we can hide the child process */
CreatePipe((PHANDLE)&hStdOutputReadPipe, (PHANDLE)&hStdOutputWritePipe, &sa, 0); // create a inheritable pipe
// duplicate the "write" end as inheritable stdout
DuplicateHandle(GetCurrentProcess(), hStdOutputWritePipe,
GetCurrentProcess(), &hStdOutput,
0, TRUE, DUPLICATE_SAME_ACCESS);
// duplicate stdout as inheritable stderr
DuplicateHandle(GetCurrentProcess(), hStdOutput,
GetCurrentProcess(), &hStdError,
0, TRUE, DUPLICATE_SAME_ACCESS);
// no longer need the non-inheritable "write" end of the pipe
CloseHandle(hStdOutputWritePipe); ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( π, sizeof(pi) );
ZeroMemory( &b, sizeof( );
/* initialize STARTUPINFO */
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.hStdOutput = hStdOutput;
si.hStdError = hStdError;
si.wShowWindow = SW_HIDE;
/* If using a console window include below */
/*
if (!AllocConsole())
{
MessageBoxError("AllocConsole failed!");
return FALSE;
}
else
{
isConsole=TRUE;
}
*/
// Start the child process.
if(!CreateProcess( NULL, // If no module name (use command line).
bbuffer, // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
TRUE, // Set handle inheritance to TRUE.
CREATE_NEW_CONSOLE|NORMAL_PRIORITY_CLASS, // when using anonymous pipes, otherwise 0.
NULL, // Use parent's environment block.
temppath, // Use Start in Folder for (target) process
&si, // Pointer to STARTUPINFO structure.
π )) // Pointer to PROCESS_INFORMATION structure. {
void *buf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,GetLastError(),0,
(LPTSTR) &buf,0,NULL);
sprintf(b, "CreateProcess Error: %s\n"
"%s", bbuffer,buf);
MessageBoxError(;
return FALSE;
} // Send update status to dialog's progress control
do
{
PostMessage(hwndCmp, WM_PROGRESS,0,0);
}
while(WaitForSingleObject(pi.hProcess,500)!=WAIT_OBJECT_0); // update at 500ms = 1/2 second intervals until done! dwRet = 0x00000000;
GetExitCodeProcess(pi.hProcess, &dwRet);
ReadFile(hStdOutputReadPipe, bigbuf, sizeof(bigbuf), &dwRead, NULL); // This block is for anonymous pipes
// Print the exit code
//*b = '\0';
//sprintf(b, "Exit code is: %01X\n", dwRet);
//strcat(bigbuf, ;
goto NEXT;
//
// This block of code for reading console buffer
//
/*
hConsoleOut = GetStdHandle( STD_OUTPUT_HANDLE );
GetConsoleScreenBufferInfo(hConsoleOut, &csbi);
lineWidth = csbi.dwSize.X;
count = ((csbi.dwCursorPosition.Y-lastpos.Y)*lineWidth+(csbi.dwCursorPosition.X-lastpos.X));
cbuf = (LPTSTR) LocalAlloc(LMEM_FIXED | LMEM_ZEROINIT, count*sizeof(TCHAR));
// read newly output characters starting from last cursor position
ReadConsoleOutputCharacter(hConsoleOut, cbuf, count-1, lastpos, &dwRead);
// fill screen buffer with zeroes
FillConsoleOutputCharacter(hConsoleOut, '\0', count, lastpos, &dwRead);
CloseHandle(hConsoleOut);
if (isConsole)
{
FreeConsole();
}
MessageBox(0, cbuf,"PUNCHIT",MB_OK+MB_TASKMODAL+MB_ICONINFORMATION);
LocalFree(cbuf);
*/
NEXT:
return TRUE;
}

I run this command from within my program. I specifically block out a console window from


displaying, but the processing behind the scenes is captured and then displayed in


a messagebox. Not sure if this code will help you or not, but it's pretty neat.


 


CZ


Edited by CondZero
  • Like 1
Link to comment

I looked into my bag of tricks and found the following Shell command code which


can also be used.


lpFile contains the commandline program begin executed in the example below:



SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = lpsplashitinfo->spxtemp == 1 ? tempfil1 : lpsplashitinfo->amodule;
ShExecInfo.lpParameters = L"";
ShExecInfo.lpDirectory = nbuf;
ShExecInfo.nShow = SW_SHOWNORMAL;
ShExecInfo.hInstApp = NULL;
if (!ShellExecuteEx(&ShExecInfo))
{
void *buf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,GetLastError(),0,
(LPWSTR) &buf,0,NULL);
wsprintf(b, L"ShellExecute failed! %s\n%s",lpsplashitinfo->spxtemp == 1 ? tempfil1 : lpsplashitinfo->amodule,buf);
MessageBoxError(;
iserror=TRUE;
}

Good Luck!


 


CZ


  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...