Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7728705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:47:08+00:00 2026-06-01T05:47:08+00:00

I would like to create a program that will run in background as a

  • 0

I would like to create a program that will run in background as a final product. For debug purpose I want it to display a console.

I learned that there is a ShowWindow( hWnd, SW_HIDE ); function, but if I use it in a ‘standard’ main function the console window still pops up for a moment. I was trying to work it out like this (yes I know it’s crappy):

#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <tchar.h>


#define DEBUG
//#undef DEBUG

#ifndef DEBUG
#pragma comment(linker, "/SUBSYSTEM:WINDOWS")

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
    HWND hWnd = GetConsoleWindow();
    ShowWindow( hWnd, SW_HIDE );    

    while(1);

    return 0;
}
#else
#pragma comment(linker, "/SUBSYSTEM:CONSOLE")
    int main(int argc, int **argv)
    {
        HWND hWnd = GetConsoleWindow();

        while(1);

        return 0;
    }
#endif

Here I managed to prevent the window form popping up, but I can’t pass parameters to the program.

I believe there is a much better solution for this. Can you share?

PS

I don’t want to use .NET.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T05:47:09+00:00Added an answer on June 1, 2026 at 5:47 am

    This is an answer to the first part of the question, “Here I managed to prevent the window form popping up”, i.e. how to set the Windows subsystem for an application in Visual C++.

    I will answer the second part of the question, about command line arguments, separately.

    // How to create a Windows GUI or console subsystem app with a standard `main`.
    
    #ifndef _MSC_VER
    #   error Hey, this is Visual C++ specific source code!
    #endif
    
    // Better set this in the project settings, so that it's more easily configured.
    #ifdef  NDEBUG
    #   pragma comment( linker, "/subsystem:windows /entry:mainCRTStartup" )
    #else
    #   pragma comment( linker, "/subsystem:console" )
    #endif
    
    #undef  UNICODE
    #define UNICODE
    #undef  NOMINMAX
    #define NOMINAX
    #undef  STRICT
    #define STRICT
    #include <windows.h>
    
    int main()
    {
        MessageBox( 0, L"Hi!", L"This is the app!", MB_SETFOREGROUND );
    }
    

    The NDEBUG standard C++ macro is designed for suppressing the effect of standard assert, so it’s not required to be globally meaningful. However, in practice it is globally meaningful. And then it provides a bit of portability compared to using a Visual C++ macro such as DEBUG.

    Anyway, in order to make it easier to configure the subsystem, unless you want to enforce that debug builds should be console and release builds should be GUI, then I recommend doing this in the project settings rather than via a #pragma (note also that e.g. the g++ compiler does not support linker pragmas, so using the project settings is more portable).

    If you want you can check the subsystem programmatically instead of just by inspection (i.e. instead of noting whether the above program produces a console or not):

    // How to create a Windows GUI or console subsystem app with a standard `main`.
    
    #ifndef _MSC_VER
    #   error Hey, this is Visual C++ specific source code!
    #endif
    
    // Better set this in the project settings, so that it's more easily configured.
    #ifdef  NDEBUG
    #   pragma comment( linker, "/subsystem:windows /entry:mainCRTStartup" )
    #else
    #   pragma comment( linker, "/subsystem:console" )
    #endif
    
    #undef  UNICODE
    #define UNICODE
    #undef  NOMINMAX
    #define NOMINAX
    #undef  STRICT
    #define STRICT
    #include <windows.h>
    
    #include <assert.h>         // assert
    #include <string>           // std::wstring
    #include <sstream>          // std::wostringstream
    using namespace std;
    
    template< class Type >
    wstring stringFrom( Type const& v )
    {
        wostringstream  stream;
    
        stream << v;
        return stream.str();
    }
    
    class S
    {
    private:
        wstring     s_;
    
    public:
        template< class Type >
        S& operator<<( Type const& v )
        {
            s_ += stringFrom( v );
            return *this;
        }
    
        operator wstring const& () const { return s_; }
        operator wchar_t const* () const { return s_.c_str(); }
    };
    
    IMAGE_NT_HEADERS const& imageHeaderRef()
    {
        HMODULE const                   hInstance   =
            GetModuleHandle( nullptr );
    
        IMAGE_DOS_HEADER const* const   pImageHeader    =
            reinterpret_cast< IMAGE_DOS_HEADER const* >( hInstance );
        assert( pImageHeader->e_magic == IMAGE_DOS_SIGNATURE );     // "MZ"
    
        IMAGE_NT_HEADERS const* const   pNTHeaders      = reinterpret_cast<IMAGE_NT_HEADERS const*>(
                reinterpret_cast< char const* >( pImageHeader ) + pImageHeader->e_lfanew
                );
        assert( pNTHeaders->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC );    // "PE"
    
        return *pNTHeaders;
    }
    
    int main()
    {
        IMAGE_NT_HEADERS const& imageHeader = imageHeaderRef();
        WORD const              subsystem   = imageHeader.OptionalHeader.Subsystem;
    
        MessageBox(
            0,
            S() << L"Subsystem " << subsystem << L" "
                << (0?0
                    : subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI?     L"GUI"
                    : subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI?     L"Console"
                    : L"Other"),
            L"Subsystem info:",
            MB_SETFOREGROUND );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create a console program in VB.net that would allow parameters.
I am creating a program that will multiple background tasks simultaneously. I would like
I would like to create a program in a linux/unix environment that runs from
I would like create a web service in ASP.Net 2.0 that will supports JSON.
I would like create my own collection that has all the attributes of python
Background: for my computer science class, we were asked to create a program that
I would like to create a new dynamic chart control. This chart will have
I want to create a C++ application that is to run on some Linux
I woud like to create a cross-platform drawing program. The one requirement for writing
i would like create a array of structure which have a dynamic array :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.