123iamking
123iamking

Reputation: 2703

Create C++ background Win32 application from scratch

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;
}

Upvotes: 1

Views: 649

Answers (1)

xMRi
xMRi

Reputation: 15365

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.

Upvotes: 2

Related Questions