Pelles C forum

C language => User contributions => Topic started by: Vortex on August 09, 2024, 07:51:41 PM

Title: Hiding a console window
Post by: Vortex 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 :

#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;
}
Title: Re: Hiding a console window
Post by: John Z on August 09, 2024, 11:30:02 PM
Very cool  8)

Thanks!

John Z
Title: Re: Hiding a console window
Post by: WiiLF23 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?
Title: Re: Hiding a console window
Post by: Vortex 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.