NO

Author Topic: Hiding a console window  (Read 4615 times)

Offline Vortex

  • Member
  • *
  • Posts: 864
    • http://www.vortex.masmcode.com
Hiding a console window
« on: August 09, 2024, 07:51:41 PM »
Inspired by MrBcx's Hide Console Window thread and Kliu's hideexec , here is an application to run hidden console applications. C code is the output of BCX Basic :

Code: [Select]
#include <windows.h>

int __cdecl __getmainargs(int*, char***, char***, int, int*);

int WINAPI WinMain (HINSTANCE hInst,HINSTANCE hPrev,LPSTR CmdLine,int CmdShow)
{
  PROCESS_INFORMATION  procinfo={0};
  STARTUPINFO  startinfo={0};
  BOOL ProcResult= {0};
  DWORD ExitCode= {0};
  int argc,i;
  char** argv;
  char** env;
  int doWildCard = 0;

  __getmainargs(&argc,&argv,&env,0,&doWildCard);

  ZeroMemory(&startinfo,sizeof(startinfo));

  startinfo.cb=sizeof(startinfo);;
  startinfo.dwFlags=STARTF_USESHOWWINDOW;
  startinfo.wShowWindow=SW_HIDE;

  ProcResult=CreateProcess(NULL,argv[1],NULL,NULL,FALSE,CREATE_NO_WINDOW|NORMAL_PRIORITY_CLASS,NULL,NULL, &startinfo, &procinfo);
  ExitCode=0x10000;
  ProcResult=(ProcResult!=0);

  for(i=0; i<ProcResult; i++){
      WaitForSingleObject(procinfo.hProcess,INFINITE);
      GetExitCodeProcess(procinfo.hProcess, &ExitCode);;
      CloseHandle(procinfo.hProcess);
      CloseHandle(procinfo.hThread);
    }

  return ExitCode;
}
Code it... That's all...

Offline John Z

  • Member
  • *
  • Posts: 860
Re: Hiding a console window
« Reply #1 on: August 09, 2024, 11:30:02 PM »
Very cool  8)

Thanks!

John Z

Offline WiiLF23

  • Member
  • *
  • Posts: 89
Re: Hiding a console window
« Reply #2 on: August 27, 2024, 06:48:13 PM »
I do this for a upnp cli binary, but I use ShellExecute with flags that completely hide it. Is ShellExecute not sufficient for this?

Offline Vortex

  • Member
  • *
  • Posts: 864
    • http://www.vortex.masmcode.com
Re: Hiding a console window
« Reply #3 on: August 27, 2024, 09:11:25 PM »
Hi WiiLF23,

ShellExecute is practical and easier to use. The CreateProcess API has more control on the process creation stage.
Code it... That's all...