CreateProcess with CREATE_NO_WINDOW

Posted on 2001-09-10
Last Modified: 2012-05-04
Hi all,

Does anybody know how to use the CREATEPROCESS API with a CREATE_NO_WINDOW so that a window, of course, is not created when the process is created/executed?

Basically, a CREATEPROCESS call that works the same way as a SHELL statement with a vbHide qualifier is used?

Any assistance would greatly be appreciated.

Question by:davism
  • 3
  • 2
  • 2
  • +1

Expert Comment

ID: 6472357
I will place a comment only first on this one.  You can not EVER make a process in windows that needs a window not have one.  This would break the windows message system.  BUT what you can do and what I would suggest is just make it hidden...

Sorry if I missed what you are asking but seems to be what you may need by the text above.

Author Comment

ID: 6472386
How can I make that hidden with a CREATEPROCESS?  Again, I know I can do that with the SHELL with the vbHide qualifer but how can I do that (i.e. hidden) with a CREATEPROCESS?  

Accepted Solution

trkcorp earned 100 total points
ID: 6472395
This will start a "windowed" process and hide it from you.  WARNING: You will have to go into task manager and manually terminate NOTEPAD.EXE in order to end this program normally since notepad will not end itself and you will be unable to see it...

'Create a module and add this code:

Option Explicit
'Global Constants
Global Const SW_HIDE = &H0&
Global Const INFINITE = -1&

'CreateProcess parm structures
    cb As Long
    lpReserved As String
    lpDesktop As String
    lpTitle As String
    dwX As Long
    dwY As Long
    dwXSize As Long
    dwYSize As Long
    dwXCountChars As Long
    dwYCountChars As Long
    dwFillAttribute As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Long
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
End Type

  hProcess As Long
  hThread As Long
  dwProcessID As Long
  dwThreadID As Long
End Type

Declare Function CreateProcessA Lib "kernel32" _
   (ByVal lpApplicationName As Long, ByVal lpCommandLine As _
   String, ByVal lpProcessAttributes As Long, ByVal _
   lpThreadAttributes As Long, ByVal bInheritHandles As _
   Long, ByVal dwCreationFlags As Long, ByVal _
   lpEnvironment As Long, ByVal _
   lpCurrentDirectory As Long, lpStartupInfo As STARTUPINFO, _
   lpProcessInformation As PROCESS_INFORMATION) As Long
Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, _
  ByVal dwMilliseconds As Long) As Long
Declare Function CloseHandle Lib "kernel32" (hObject As Long) _
    As Boolean

'Using a form with a button to execute the following:

Private Sub Command1_Click()
Dim AppToRun As String
Dim rc As Long

NameStart.cb = Len(NameStart)
NameStart.wShowWindow = SW_HIDE

' Launch notepad and wait for its completion
AppToRun = "notepad.exe"
rc = CreateProcessA(0&, AppToRun, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, NameStart, NameofProc)
rc = WaitForSingleObject(NameofProc.hProcess, INFINITE)
rc = CloseHandle(NameofProc.hProcess)
End Sub

Hope this helps!!!
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


Expert Comment

ID: 6472916

Not veru sure if this is what you need, but here it is:

Description: Make a "formless" program with a system tray icon (3K)

You could simply kick out the code for putting the icon in the sys tray.  FYI, I've come across code which doesn't use the Form gived in VB, but simply a *.bas file.  Is that what you're looking for anyway? - Can't promise I'll be able to find it though, but I'll try if that's what you need.

That's it!

glass cookie : )

Expert Comment

ID: 6472917

Not veru sure if this is what you need, but here it is:

Description: Make a "formless" program with a system tray icon (3K)

That's it!

glass cookie : )

Expert Comment

ID: 6473032
As glass cookie alluded to, and you may already know, you may have an executable created from just a .bas module and no forms at all.  You set the start up object on the project properties window general tab to Sub Main and create a .bas module with a Sub Main() routine...

Also, in my example above, by commenting out the code:
rc = WaitForSingleObject(NameofProc.hProcess, INFINITE)
rc = CloseHandle(NameofProc.hProcess)
you can launch a process and not wait or monitor it but I wouldn't advise doing it unless it will "self-terminate".  You can even exit the launcing program and leave the launched process active.

If you change the constant SW_HIDE thusly:
Global Const SW_HIDE = &H1&
You will be saying in effect, do not hide the process. You can then see it and wait on it as if it were a modal form, or not wait by commenting out as in the prior paragraph.  Doing the latter you can, as I said, exit the launching program leaving the launched one running...

The above example was originally written to run BCP.exe (a DOS SQL Bulk copy command) in background. (no dos window) It will, however, run any process but as you will see if you have a windowed process the API as stated will not allow you to see the window, but it will launch the process and run any code that runs without intervention.  If your real purpose is to launch a windowed program but not have it take focus, then just use SHELL and do a ME.ZORDER to place your launching program back in foreground.

Author Comment

ID: 6474316
Worked out great.  Thanks much.  In response to your later comment as well - I did work with certain aspect as in removing the waitforsingleobject, etc because I didn't have to wait for it.

One thing I did notice however, is this is using the CREATEPROCESSA where as I had a previous API of CREATEPROCESS which had a alias of CREATEPROCESSA but the fundamental parameter datatypes where different.  For instance, the very first parameter of the lpApplication name in the CREATEPROCESS API was a datatype of string and in the CREATEPROCESSA API it is a long.

Any idea on the reasons for the differences?  

But very much appreciated on the answer.  It worked out great.  :-)

Expert Comment

ID: 6474614
Nope, I don't have my trusty API book in my hand... But Glad that I could help.  You are welcome.

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question