Hide exec process

Started by Nicola, May 24, 2024, 09:38:56 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Nicola

Hi
I was looking for a way to keep the CMD console from appearing when I start some programs with the exec command...
I found this system by inserting the constant CREATE_NO_WINDOW in the createprocess...
I wondered if this was the best method...

In sysutil I duplicated the existing function "exec" with this:
  function ExecHide(string c, int wait=0) as int
  ==========================================
  STARTUPINFO infs
  PROCESS_INFORMATION infp
  ' [color=#0000ff]CREATE_NO_WINDOW [/color]= 134217728
  CreateProcess null,c,0,0,0,134217728,0,0,@infs,@infp
  if wait
    WaitForMultipleObjects 1,@infp.hthread,1,-1
  end if
  GetExitCodeProcess(infp.hProcess,@function)
  CloseHandle infp.hProcess
  CloseHandle infp.hThread
  return 0
  end function

Cheers

Nicola

... I'm a little disappointed with the result

Charles Pegge

Hi Nicola,
Are these your own processes/programs?

Theo Gottwald

There are additional options for starting programs without displaying the CMD console in PowerBASIC.

One of the alternatives is using the ShellExecuteEx function, which provides more control and flexibility. This function is particularly useful if you need to open documents or URLs with their associated applications, or if you need to start a program with more specific parameters.

Here's how you can implement the ShellExecuteEx function to start a program without showing the CMD console:

Step-by-Step Implementation using ShellExecuteEx

Define the necessary constants and structures:

#COMPILE EXE
#DIM ALL

#INCLUDE "WIN32API.INC"

TYPE SHELLEXECUTEINFO
  cbSize          AS DWORD
  fMask           AS DWORD
  hwnd            AS DWORD
  lpVerb          AS ASCIIZ PTR
  lpFile          AS ASCIIZ PTR
  lpParameters    AS ASCIIZ PTR
  lpDirectory     AS ASCIIZ PTR
  nShow           AS LONG
  hInstApp        AS DWORD
  lpIDList        AS ANY PTR
  lpClass         AS ASCIIZ PTR
  hkeyClass       AS DWORD
  dwHotKey        AS DWORD
  hIcon           AS DWORD
  hProcess        AS DWORD
END TYPE

%SEE_MASK_NOCLOSEPROCESS = &H00000040
%SEE_MASK_NO_CONSOLE     = &H08000000
%SW_HIDE                 = 0

Create the ShellExecNoConsole function to execute the command:

FUNCTION ShellExecNoConsole (BYVAL Command AS STRING, BYVAL Params AS STRING) AS LONG
  DIM sei AS SHELLEXECUTEINFO
  DIM result AS LONG

  sei.cbSize = SIZEOF(sei)
  sei.fMask = %SEE_MASK_NOCLOSEPROCESS OR %SEE_MASK_NO_CONSOLE
  sei.hwnd = %NULL
  sei.lpVerb = VARPTR(STRPTR("open"))
  sei.lpFile = VARPTR(STRPTR(Command))
  sei.lpParameters = VARPTR(STRPTR(Params))
  sei.lpDirectory = %NULL
  sei.nShow = %SW_HIDE

  result = ShellExecuteEx(sei)

  IF result THEN
    ' Wait for the process to complete, if needed
    WaitForSingleObject(sei.hProcess, %INFINITE)
    CloseHandle(sei.hProcess)
  END IF

  FUNCTION = result
END FUNCTION

Call the ShellExecNoConsole function in your main program:


FUNCTION PBMAIN () AS LONG
  DIM command AS STRING
  DIM params AS STRING
  command = "notepad.exe"  ' Example command
  params = ""              ' Example parameters, if any

  IF ShellExecNoConsole(command, params) THEN
    MSGBOX "Process started successfully without a visible window.", %MB_OK, "Info"
  ELSE
    MSGBOX "Failed to start the process.", %MB_OK, "Error"
  END IF
END FUNCTION

Explanation

SHELLEXECUTEINFO: This structure is necessary for configuring the ShellExecuteEx function.

SEE_MASK_NOCLOSEPROCESS and SEE_MASK_NO_CONSOLE: These flags ensure the process starts without creating a console window and that you can wait for the process to complete if needed.

ShellExecuteEx: This function starts the process with the specified attributes and more control compared to CreateProcess.

WaitForSingleObject: This function waits for the process to complete if needed. You can remove it if you don't need to wait for the process to finish.

Using ShellExecuteEx provides additional options and flexibility for starting programs without a visible console, making it a robust alternative to CreateProcess. This method is particularly useful for more complex scenarios where you need finer control over the execution of the program.

Nicola

#4
Thank you Theo. I will definitely try them!!
 SEE_MASK_NO_CONSOLE has the numeric value 0x08000000 in hexadecimal or 134217728 in decimal. This value is used to set the appropriate option when creating a new process using code.
it's the constant I mentioned in the first post....

@Charles
No, I start an external third-party program.

Nicola

Hi
I tried again and now it's ok. I had left another command that opened the cmd window, the DOS one. I fixed it by doing this:

string cancellaPdf ="cmd.exe /c del "+pdf
if FileExists(pdf)=1 then exechide(cancellaPdf,1)
String riga=prodir + " -out pdf -ratio -resize 1200 -overwrite -c 5 -q "+qualita+" -o " +qu+pdf+qu+ " " +qu+f+qu
[font=Verdana, Arial, Helvetica, sans-serif]int valu=exechide(riga,1) [/font]

Exechide works.

Nicola

#6
Charles,
could you put EXECHIDE in sysutil?
and also a DOShide?

Charles Pegge

#7
I could add a default flags param:

  'process flags:
  %CREATE_NEW_CONSOLE 0x00000010
  %CREATE_NO_WINDOW 0x08000000
  'etc
  '
  function Exec(string c, int wait=0, flags=0) as int
  ===================================================
  STARTUPINFO infs
  PROCESS_INFORMATION infp
  CreateProcess null,c,0,0,0,flags,0,0,@infs,@infp
  if wait
    WaitForMultipleObjects 1,@infp.hthread,1,-1
  end if
  GetExitCodeProcess(infp.hProcess,@function)
  CloseHandle infp.hProcess
  CloseHandle infp.hThread
  return 0
  end function


  function QuExec(string c,d,f, int wait=0,flags=0) as int
  ========================================================
    return Exec(qu+c+qu+" "+d+" "+qu+f+qu, wait,flags)
  end function
 

  function DOS(string s, int wait=0,flags=0)
  ==========================================
  string c
  if s then
    c="cmd.exe /c "+s
  else
    c="cmd.exe"
  end if
  Exec c, wait,flags
  end function

Nicola

Hi Charles,
That sounds like a great idea to me. In this way we can use the various possibilities available.
Thank you.