1

I want to create a very simple background application. I start with the empty c++ project, and I create a source.cpp file with the code below:

#include "Windows.h"

int WINAPI _tWinMain(HINSTANCE  hInstance,
    HINSTANCE   hPrevInstance,
    LPTSTR      lpCmdLine,
    int     nCmdShow)
{
    MSG Msg;

    while (GetMessage(&Msg, NULL, 0, 0))
    {

    }

    return 0;
}

then I set the settings of the project as follow:

Properties -> Configuration Properties -> Linker -> System: Set SubSystem is: Windows (/SUBSYSTEM:WINDOWS)

Properties -> Configuration Properties -> Linker -> Advanced ->Set Entry Point is: _tWinMain

Am I doing it right? Also I want to add the MFC library into this project so I can use the MFC's function, how can I do it?

Thanks for reading :)

Edit: Weird, I just need to include "tchar.h" and the error [ LNK1561: entry point must be defined ] disappear. I don't need to configure the project settings anymore. All I need is the code below:

#include "Windows.h"
#include "tchar.h"

int WINAPI _tWinMain(HINSTANCE  hInstance,
    HINSTANCE   hPrevInstance,
    LPTSTR      lpCmdLine,
    int     nCmdShow)
{
    //MessageBox(0, _T("test"), _T("Test"), 0);

    return 0;
}
6
  • I want to make it from scratch so I can totally understand how a windows process run. Commented Dec 31, 2016 at 3:14
  • Don't fiddle with the entry point. Leave it at the default. If you set it to _tWinMain, you are skipping required CRT startup code. This includes initializers for objects with static storage duration among others. If you want to understand how Windows processes work, read a book (like Windows Internals by Russinovich). Whatever you do, don't add MFC. It'll make it a lot harder to understand, what's going on. All the more, if you aren't proficient in C++, the Windows API or MFC. Commented Dec 31, 2016 at 21:03
  • If I don't config entry point, the project won't build, it show the error: Entry point must be defined, so I do as the following: stackoverflow.com/questions/17070367/… Commented Jan 3, 2017 at 2:00
  • 1
    I'm afraid, you applied the wrong solution. I commented (and voted) on the highest-rated answer you linked to. Commented Jan 3, 2017 at 10:06
  • @IInspectable: Thank you very much, I'll dig more into this topic. Commented Jan 3, 2017 at 12:59

1 Answer 1

2

Create a SDI application without using Documents and Views. Check only the options you need. In most cases you need a window even for a background application to get window messages.

Leave the main window that is created and don't show it, set m_nCmdShow to SW_HIDE. Destroying this main window will end the message loop and stop the program.

Later you may remove other unused things like the toolbar from the mainframe.

Another way, is to create a dialog based application with the wizard. Without creating any dialog and using your own message pump. with something like this:

while (OuterCondition()) 
{
  while( ::PeekMessage( &message, NULL , WM_MIN, WM_MAX, PM_NOREMOVE ) )
  {
    ::AfxPumpMessage();
  }
}

Also MsgWaitForMultipleObjects may be useful.

You usually need a CWinApp object to init MFC correctly.

Sign up to request clarification or add additional context in comments.

1 Comment

The OP presumably mis-tagged the question. This is not related to MFC, but rather a straight Windows API question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.