Jump to content
View in the app

A better way to browse. Learn more.

Tuts 4 You

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

using system function to run a dos command?

Featured Replies

Posted

hi, 


 


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


 


fsutil usn deletejournal /D C:


 


without loading a seperate window etc.


 


C++


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

  • Author

Thanks mate, i will have a look at this.


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


Create an account or sign in to comment

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.