Jump to content
Tuts 4 You

how to hide console window?


alaphate

Recommended Posts

I want to convert a batch file (clean.bat) to clean.exe, but I dislike the console window.

Any buddy knows how to hide the cmd window? Thanks a lot.

WinExec and ShellExecute seem can not fit my requirement.

del /f /s /q "%userprofile%\Local Settings\Temp\*.*"	   
del /f /s /q "%userprofile%\recent\*.*"
reg delete "HKCU\Software\Microsoft\Internet Explorer\TypedURLs" /va /f
reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU" /va /f
reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\RecentDocs" /f
//clean.exe#include <windows.h>
#include <process.h>int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrev, LPSTR pCmdLine, int iCmdShow) {
system("del /f /s /q \"%userprofile%\Local Settings\Temp\*.*\"");
return 0;
}
Link to comment

Surely not what you meant, but at least you can run it minimized like this:

@echo off
if not "%1"=="" goto %1start /MIN cmd.exe /C "%~nx0 begin"
goto:eof:begin
del /f /s /q "%userprofile%\Local Settings\Temp\*.*"
del /f /s /q "%userprofile%\recent\*.*"
reg delete "HKCU\Software\Microsoft\Internet Explorer\TypedURLs" /va /f
reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU" /va /f
reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\RecentDocs" /f
goto:eof
Link to comment

Ufo-Pu55y, your batch file is awesome.

However,

I'm wondering how to hide the console window when I call function system("dir > a.txt").

Link to comment
@echo off
if not "%1"=="" goto %1start /MIN cmd.exe /C "%~nx0 begin"
goto:eof:begin
del /f /s /q "%userprofile%\Local Settings\Temp\*.*"
del /f /s /q "%userprofile%\recent\*.*"
reg delete "HKCU\Software\Microsoft\Internet Explorer\TypedURLs" /va /f
reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU" /va /f
reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\RecentDocs" /f
goto:eof:dir
%1 %2 %3 %4 %5 %6 %7 %8
goto:eof

There are tons of batch tutorials out there btw..

Link to comment

what a problem to change in pe header from console to windowed.

also on net there are many tools that hide windows. even windowed ones.

most av detect them, due people us it to hide tools.

imagine runing command on remote pc and suddenly user see your proggy window:P

Link to comment

Yep, pe header modification from console -> gui would do the trick, or alternatively you could use pipes and createprocess so the console window never 'shows'...

Link to comment
Yep, pe header modification from console -> gui would do the trick, or alternatively you could use pipes and createprocess so the console window never 'shows'...

I used this trick in my "PunchIt" application available on Arteam releases page.

Here's the main function for hiding a console window. I launch PECompact's

console version of their packer / encrypter below:

// 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(&hStdOutputReadPipe, &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(b) );
/* 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. {
sprintf(b, "CreateProcess: %s", bbuffer);
MessageBoxError(b);
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, b);
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;
}

cheers

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