Solved

Creating a console process without an MS-DOS window

Posted on 1999-01-26
8
276 Views
Last Modified: 2013-12-03
I want to use the CreateProcess API to create a Windows NT/95/98 process that does not have any UI, and does not pop up an MS-DOS window.  How do I do this?

I used the VC++ AppWizard for console process to create my process, but it is not doing any I/O so I do not want the window visible.

0
Comment
Question by:tophermiller
  • 3
  • 3
  • 2
8 Comments
 
LVL 15

Expert Comment

by:NickRepin
ID: 1419308
Use DETACHED_PROCESS as creation flag in CreateProcess call.
0
 

Author Comment

by:tophermiller
ID: 1419309
I tried using DETACHED_PROCESS and it doesn't have the desired effect.  I still get an MS-DOS window popping up.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1419310
Try the following code.

STARTUPINFO StartupInfo =
{
    sizeof(STARTUPINFO),
    NULL,
    NULL,
    NULL,
    0, 0,
    0, 0,
    0, 0,
    0,
    STARTF_USESHOWWINDOW,
    SW_HIDE,
    0,
    NULL,
    NULL,
    NULL,
    NULL
};

PROCESS_INFORMATION ProcessInformation;

::CreateProcess(NULL,
                _T("app.exe"),
                NULL,
                NULL,
                TRUE,
                0,
                NULL,
                NULL,
                &StartupInfo,
                &ProcessInformation);
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1419311
chensu, your solution w/o DETACHED_PROCESS will display console.
STARTUPINFO doesn't matter.

It seems that tophermiller just doesn't want to pay his points.

Here is working sample

//---------------------
// b.exe parent process (window)
//---------------------
#include <windows.h>

int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int)
{
   STARTUPINFO si;
   PROCESS_INFORMATION pi;
   ZeroMemory(&si,sizeof(si));
   si.cb = sizeof(si);

   CreateProcess(NULL,"a.exe",NULL,NULL,FALSE,
      DETACHED_PROCESS,NULL,NULL,&si,&pi);
   CloseHandle( pi.hProcess );
   CloseHandle( pi.hThread );
   return 0;
}#include <windows.h>
//------------------
//  a.exe child process (console)
//------------------
void main()
{
   for(;;) {
   MessageBeep(-1);
   Sleep(2000);
   }
}
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:tophermiller
ID: 1419312
Thanks NickRepin.

I have no problem paying my points.  I just wanted to be sure to get to the bottom of it.  It turned out to be my bad, in that I was putting DETACHED_PROCESS into the STARTUP_INFORMATION flags instead of the creation flags in the CreateProcess call.  Your sample code alerted me to that.

Anyway, I'm rejecting chensu's answer, in the hopes that it will allow you to get the points instead.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1419313
Have you tried my code? The following is a complete version. ipconfig.exe is a Win32 console application in the Windows directory.

    STARTUPINFO StartupInfo =
    {
        sizeof(STARTUPINFO),
        NULL,
        NULL,
        NULL,
        0, 0,
        0, 0,
        0, 0,
        0,
        STARTF_USESHOWWINDOW,
        SW_HIDE,
        0,
        NULL,
        NULL,
        NULL,
        NULL
    };

    PROCESS_INFORMATION ProcessInformation;

    if (::CreateProcess(NULL,
                        _T("ipconfig.exe"),
                        NULL,
                        NULL,
                        TRUE,
                        0,
                        NULL,
                        NULL,
                        &StartupInfo,
                        &ProcessInformation))
    {
        ::CloseHandle(ProcessInformation.hProcess);
        ::CloseHandle(ProcessInformation.hThread);
    }
    else
    {
        LPVOID lpMsgBuf;
        ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
                        FORMAT_MESSAGE_FROM_SYSTEM,
                        NULL,
                        ::GetLastError(),
                        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                        (LPTSTR)&lpMsgBuf,
                        0,
                        NULL);
       
        // Display the string.
        ::AfxMessageBox((LPCTSTR)lpMsgBuf);
       
        // Free the buffer.
        ::LocalFree(lpMsgBuf);
    }
0
 
LVL 23

Expert Comment

by:chensu
ID: 1419314
Try it. It does work.
0
 
LVL 15

Accepted Solution

by:
NickRepin earned 70 total points
ID: 1419315
Thanks.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Event ID 10010 3 36
DB Mail Export to Excel some row wrap to new row 30 102
Is COM supported from Apache 1 37
Need more details 5 83
This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now