MSFN Forum: [Tool] Hide console (command line) windows - MSFN Forum

Jump to content



  • 4 Pages +
  • 1
  • 2
  • 3
  • Last »
  • You cannot start a new topic
  • You cannot reply to this topic

[Tool] Hide console (command line) windows run you batch files completely hidden Rate Topic: -----

#1 User is offline   Vadikan 

  • MSFT MVP Windows Expert: Consumers
  • PipPipPip
  • Group: Members
  • Posts: 349
  • Joined: 22-February 04
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 23 June 2005 - 05:56 PM

Hi, All!

I searched the forum for hid* in titles, but couldn't find anything similar except for cmdow.

Let me introduce a small utility that completely hides console windows. All credit goes to a member of Russian board OSZone.net hasherfrog who wrote the utility exclusively for OSZone.net members and welcomed its sharing with MSFN community. [LOL, that sounds like a huge gift :D] In fact, the author didn't have unattended installations in mind, he just wrote an utility in responce to one of the threads. That was me who thought about unattended installations.

The utility is attached to the post.

====Changelog======
06/28/05 The utility size reduced to 1 kb, switch /W made case insensitive, usage window is displayed when run w/o parameters.
06/27/05 Added /W switch
=================

Usage

Quote

RunHiddenConsole.exe [/W]<path>\yourfile

yourfile can be either a batch file or any command line (console) utility.
/W - (case-insensitive) waits for a batch file or console utility to end

Example of usage:
Runs cleanup.cmd hidden
 
#include <process.h> 
#include <windows.h> 
 
// Отключаем станд. библиотеки
#pragma comment(linker,"/NODEFAULTLIB")
// Объединяем секции кода и данных
#pragma comment(linker,"/MERGE:.rdata=.text")
// Разрешаем запись в секцию кода
#pragma comment(linker,"/SECTION:.text,EWRX")
// Новая точка входа
#pragma comment(linker,"/ENTRY:NewWinMain")

//int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR lpszCmd,int nCmd) 
void NewWinMain(void)
{ 
	STARTUPINFO si; 
	PROCESS_INFORMATION pi; 
	int bWait = 0;
	DWORD exitcode = 0;
	char stopchar = ' ';
	char* lpszCmd = GetCommandLine();

	// Определяем символ, который будет символизировать конец имени исполняемого файла
	if (lpszCmd[0] == '\"') stopchar = '\"'; 
	// Прокручиваем строку до начала параметров запуска
	// "lpszCmd[0] != 0" - более-менее надёжная страховка от выхода за пределы буфера
	do { lpszCmd++; } while ((lpszCmd[0] != stopchar) && (lpszCmd[0] != 0));
	// Если остались пробелы или табы, пробегаем и мимо них
	if (lpszCmd[0] != 0)
	{
  do { lpszCmd++; }
  while ((lpszCmd[0] != 0) && ((lpszCmd[0] == ' ') || (lpszCmd[0] == '\t')));
	};
	// Выводим сообщение об ошибке, если параметров запуска не оказалось
	if (lpszCmd[0] == 0) 
	{
  MessageBox(0, "About:\n\nhidec hides console window of started program & waits (opt.) for its termination\n\nUsage:\n\n\thidec [/w] <filename>\n\nWhere:\n\n/w\twait for program termination\nfilename\texecutable file name", "Error: Incorrect usage", 0);
  ExitProcess(0);
	};

	/* "парсер" :-) */
	if ((lpszCmd[0] == '/')&&(((lpszCmd[1])|0x20) == 'w')&&(lpszCmd[2] == ' '))
	{ 
  bWait = 1; 
  lpszCmd += 3; 
	};
	// встаем на первый символ в имени файла для запуска
	while ((lpszCmd[0] != 0) && ((lpszCmd[0] == ' ') || (lpszCmd[0] == '\t'))) lpszCmd++;

	/* create process with new console */ 
	//нехитрая замена memset(&si,0,sizeof(si)); - кстати, а зачем?
	unsigned char *ps = (unsigned char*)&si;
	for (unsigned int i = 0; i < sizeof(si); i++) ps[ i ] = 0x00;
 
	si.cb = sizeof(si); 
	si.dwFlags = STARTF_USESHOWWINDOW; 
	si.wShowWindow = SW_HIDE; 
	if( CreateProcess( NULL, lpszCmd, 
        NULL, NULL, FALSE, CREATE_NEW_CONSOLE, 
        NULL, NULL, & si, & pi ) ) 
	{ 
  if (bWait) WaitForSingleObject(pi.hProcess, INFINITE);
  CloseHandle( pi.hProcess ); 
  CloseHandle( pi.hThread ); 
	}
	else
  exitcode = GetLastError();
 
	/* terminate this */ 
	ExitProcess(exitcode);
} 


Q. How do I compile?
A. The author uses Visual C++ Toolkit and this is how he compiles:
cl hidec.cpp /GA /O1 /link /subsystem:windows kernel32.lib advapi32.lib user32.lib

Q. I have more questions!
A. This is what the thread is for. I'll try to answer them, but the author, probably, will not.

Thanks for your attention. I hope at least one person will find the utility useful; otherwise I wasted my [working] time typing the post LOL

Attached File(s)


This post has been edited by VAD: 10 July 2005 - 11:01 PM



#2 User is offline   Alanoll 

  • CODE tags people, CODE tags!
  • Group: Patrons
  • Posts: 5,496
  • Joined: 25-September 03

Posted 23 June 2005 - 10:42 PM

Good to see one of them MSFN offshoot sites doing creative things :P I know you know what I mean VAD (reminds me...the Unattended site should be getting an overhaul shortly).

Oh yeah, this reminds me of this topic: http://www.msfn.org/...showtopic=49103

Sounds similar, though I haven't read your entire post nor theirs :P

#3 User is offline   Vadikan 

  • MSFT MVP Windows Expert: Consumers
  • PipPipPip
  • Group: Members
  • Posts: 349
  • Joined: 22-February 04
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 24 June 2005 - 05:02 PM

Ah, I see, I should've searched better :) However, mods search differently, 'cause they read all topics and often remember the titles or even some posts ;)

You can merge this thread with the one you gave the link to, if you wish.

#4 User is offline   SiMoNsAyS 

  • C'mon you apes! You wanna live forever!?
  • Group: Patrons
  • Posts: 3,455
  • Joined: 17-May 04

Posted 24 June 2005 - 05:25 PM

i think you can use cmdow in a similar way. i.e: "cmdow /hid /run path\file.cmd"...

...not sure... :P

cool adition anyway :)

edit: after testing, yes, with cmdow you always see a "blinking" command window

This post has been edited by SiMoNsAyS: 24 June 2005 - 05:30 PM


#5 User is offline   Vadikan 

  • MSFT MVP Windows Expert: Consumers
  • PipPipPip
  • Group: Members
  • Posts: 349
  • Joined: 22-February 04
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 24 June 2005 - 05:30 PM

SiMoNsAyS
IMHO, there's a minor difference. I covered it in the first hypothetical question in the first post :)

#6 User is offline   SiMoNsAyS 

  • C'mon you apes! You wanna live forever!?
  • Group: Patrons
  • Posts: 3,455
  • Joined: 17-May 04

Posted 24 June 2005 - 06:25 PM

oh my mistake, i missed that part! :P
i'll definetly include this tool on my uA :)

#7 User is offline   n7Epsilon 

  • Currently Learning: C#, JavaScript, PHP
  • PipPip
  • Group: Members
  • Posts: 156
  • Joined: 11-February 05

Posted 25 June 2005 - 07:04 AM

Hi,
Just a minor problem, RunHiddenConsole.exe doesn't have a switch to wait until completion of batch file execution, so I can't use it in RunOnceEx...

-> Can you implement this, (eg: RunHiddenConsole.exe /WAIT)

#8 User is offline   Vadikan 

  • MSFT MVP Windows Expert: Consumers
  • PipPipPip
  • Group: Members
  • Posts: 349
  • Joined: 22-February 04
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 25 June 2005 - 02:21 PM

ChipCraze23
Well, the utility does what it does ;) No switches available. I can ask the author, but nothing's guaranteed. Consider using the startx utility.

To be honest, I have just one cleanup.cmd in my RunOnceEx routine. Apparently, this is the last file. The first command is shutdown with 60 sec delay, so I have plenty of time to execute the rest of the commands.

#9 User is offline   smashly 

  • Member
  • PipPip
  • Group: Members
  • Posts: 107
  • Joined: 09-April 05

Posted 26 June 2005 - 01:52 AM

Nice util , but I still need to work out a few things to use it.

If I run this tool from cmdlines.txt it appears to sorta work and sorta not.

[COMMANDS]
"REGEDIT /S tweaks.reg" 
"rhc.exe daemon.cmd"     <-----Fails this
"rhc.exe RunOnceEx.cmd"  <----- Does this fine , all good
My daemon.cmd works fine without the rhc.exe. It just seems to skip the daemon.cmd alltogether when I use rhc.exe.
Thought it maybe was cause of running daemon.msi from %CDROM%\$OEM$\ not from %SystemDrive%\Install\.

#10 User is offline   n7Epsilon 

  • Currently Learning: C#, JavaScript, PHP
  • PipPip
  • Group: Members
  • Posts: 156
  • Joined: 11-February 05

Posted 26 June 2005 - 02:00 AM

I have found a difference between STARTX and RunHiddenConsole,

When you start a batch file hidden using RunHiddenConsole, if the batch file contains CMDOW @ /VIS, the batch file can appear, which can be useful (eg: for user input) after being started completely hidden...

But,
when you start the same batch file using StartX, even if the batch file contains CMDOW @ /VIS, the batch file does NOT appear, so I lose this functionality...

--> That's why I'd like to see RunHiddenConsole.exe /WAIT ...

#11 User is offline   Vadikan 

  • MSFT MVP Windows Expert: Consumers
  • PipPipPip
  • Group: Members
  • Posts: 349
  • Joined: 22-February 04
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 26 June 2005 - 01:14 PM

@smashly

Quote

Thought it maybe was cause of running daemon.msi from %CDROM%\$OEM$\
That may be the reason, 'cause your batch file has to set the %CDROM% variable first. Put
SET CDROM=%~d0
in the beginning of the batch file, if you launch it from cmdlines.txt.

@ChipCraze23
I'm confused. Why do you want to use RHC and display the batch file window? If you want to see the window, don't use any of the hiding utils. Could you please elaborate? Meanwhile, I forwarded your feature request to the author.

#12 User is offline   n7Epsilon 

  • Currently Learning: C#, JavaScript, PHP
  • PipPip
  • Group: Members
  • Posts: 156
  • Joined: 11-February 05

Posted 26 June 2005 - 02:18 PM

I want to use this utility to run a long batch file...

A part of this batch file contains processes I want hidden to prevent cancellation..., then when the program requires user input, the batch window appears directly at the user input part of the batch program...

I've already implemented this using CMDOW @ /HID at the beginning of the batch file and CMDOW @ /VIS at the part where I want the program to be displayed but am annoyed at the brief flashing DOS windows...

#13 User is offline   Vadikan 

  • MSFT MVP Windows Expert: Consumers
  • PipPipPip
  • Group: Members
  • Posts: 349
  • Joined: 22-February 04
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 26 June 2005 - 02:35 PM

I see now. Well, although offtopic, but the obvious solution is to split your batch file in two :) Run one hidden, run the other visible.

#14 User is offline   Vadikan 

  • MSFT MVP Windows Expert: Consumers
  • PipPipPip
  • Group: Members
  • Posts: 349
  • Joined: 22-February 04
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 27 June 2005 - 01:28 PM

In response to the popular demand: the /W switch has been added. This is the equivavlent of the /WAIT switch for the START console command in Windows. The switch allows to run a batch file and wait until it ends.

The first post has been updated accordingly. Please, re-download the utility.

#15 User is offline   eirogge 

  • www.eirogge.de
  • PipPip
  • Group: Members
  • Posts: 218
  • Joined: 07-February 04

Posted 27 June 2005 - 02:05 PM

ty. testing it now.

#16 User is offline   n7Epsilon 

  • Currently Learning: C#, JavaScript, PHP
  • PipPip
  • Group: Members
  • Posts: 156
  • Joined: 11-February 05

Posted 28 June 2005 - 02:14 AM

Hello VAD,
Thanks for the PM and for the update, here's an issue:

1. RunHiddenConsole.exe /W only works when invoked from a batch file.

eg: TEST.CMD
@Echo Off
cls
Echo Testings...
Echo.
cls
Cmdow @ /VIS
Echo.
Echo Test completed.
Echo.
ping localhost > NUL
goto end
:end


-> When you execute RunHiddenConsole.exe /W test.cmd, it immediately returns to the prompt without waiting for the batch file...

-> But when you CALL "RunHiddenConsole.exe /W test.cmd" from another batch file, it does wait for the batch file to end...

-> But what if I want to run a batch program by invoking RunHiddenConsole.exe from within SVCPACK.INF in Windows Setup, it may not work in that case, as SVCPACK.INF is not a batch file...

--> I will test and report the results....

-> But I still wish the /W switch works from the command line....

Anyway thank you to you and the author for this utility....

#17 User is offline   Vadikan 

  • MSFT MVP Windows Expert: Consumers
  • PipPipPip
  • Group: Members
  • Posts: 349
  • Joined: 22-February 04
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 28 June 2005 - 03:19 AM

ChipCraze23
Well, I don't know about that... I just tested it in RunOnceEx routine. I made two cmd files: each one would ping localhost 5 times and write the results into a different text file for each ping. I ran both files from RunOnceEx with /W switch, and although I couldn't really see the whole execution process, I could see the 10 files and their creation time.

The screenshot is attached (first one). As you can see, they are in a perfect time order. Now look at the second screenshot. The same files ran from RunOnceEx, but without the /W switch. Apparently, the files executed at the same time.

You may have a different testing technique, though, but I can't test this utility from svcpack.inf :) Post your results.

Attached File(s)

  • Attached File  test.png (4.05K)
    Number of downloads: 21
  • Attached File  test2.png (4.15K)
    Number of downloads: 17


#18 User is offline   RogueSpear 

  • OS: SimplyMEPIS
  • Group: Supreme Sponsor
  • Posts: 1,529
  • Joined: 18-September 04

Posted 28 June 2005 - 07:43 AM

Here's a batch file I call from DetachedProgram and it works like a charm. I stick 7za.exe and RunHiddenConsole.exe in $OEM$\$$\system32.

@ECHO OFF
%SystemDrive%
cd \
RunHiddenConsole.exe /W 7za.exe x -y -aoa %SYSTEMDRIVE%\DriverPack*.7z -o"%SYSTEMDRIVE%"
RunHiddenConsole.exe %SYSTEMDRIVE%\SetDevicePath.exe %SYSTEMDRIVE%\D
RunHiddenConsole.exe /W 7za.exe x -y -aoa %SYSTEMDRIVE%\000_AllUsers.7z -o"%ALLUSERSPROFILE%"
RunHiddenConsole.exe /W 7za.exe x -y -aoa %SYSTEMDRIVE%\000_ProgFiles.7z -o"%PROGRAMFILES%"
RunHiddenConsole.exe /W 7za.exe x -y -aoa %SYSTEMDRIVE%\000_WinDir.7z -o"%SYSTEMROOT%"
Del %SYSTEMDRIVE%\*.7z /F /Q


Note that a batch from DetachedProgram is hidden by default so there is no need to use RunHiddenConsole.exe with it and the @ECHO OFF is probably unnecessary as well. Anything running from the script is what can take advantage of RunHiddenConsole.exe. Just wanted to show y'all what I use and that it's tested as working without issue.

#19 User is offline   Vadikan 

  • MSFT MVP Windows Expert: Consumers
  • PipPipPip
  • Group: Members
  • Posts: 349
  • Joined: 22-February 04
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 28 June 2005 - 12:58 PM

The utility has been updated again. I guess no further updates will be neccessary. The utility size reduced to 1 kb, switch /W made case-insensitive, usage window is displayed when run w/o parameters. The author renamed the utility to hidec, and this name is displayed in the usage window. You can choose the name you like. You can re-download the utility from the first post.

Thanks for the report, RogueSpear.

#20 User is offline   MHz 

  • SendToA3X v1.7
  • PipPipPipPipPipPipPip
  • Group: Members
  • Posts: 1,634
  • Joined: 02-August 04

Posted 28 June 2005 - 01:17 PM

Very nice VAD. Will add to my commandline tools. Just ran a test, with a batch file, calling tasklist each minute, for 3 minutes. It works well. CMDOW show the console window as when that command was reached.
Nice work, for just a kb in size.

Thanks VAD.
And thanks to the author. :thumbup

Share this topic:


  • 4 Pages +
  • 1
  • 2
  • 3
  • Last »
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2011 msfn.org
Privacy Policy