Why winmain parameters doesn't match to each other?

1

Why QApp constructor fails with WinMain parameters?

 int CALLBACK WinMain(HINSTANCE, HINSTANCE, LPTSTR _lpCmdLine, int _nShowCmd) {
    QApplication app(_nShowCmd, & _lpCmdLine);

And here it fail with exception:

Exception at adress 0x0F3621DC (Qt5Guid.dll) in updater_app.exe: 0xC0000005

Whats wrong? how to fix it?

UPD:

And it works in such way:

int CALLBACK WinMain(HINSTANCE, HINSTANCE, LPTSTR _lpCmdLine, int _nShowCmd) {

int nShowCmd(0);
QApplication app(nShowCmd, & _lpCmdLine);

_lpCmdLine is 10 and _nShowCmd is empty string - so it doesn't match. Why?

visual-studio
qt
winmain
qapplication
asked on Stack Overflow Jan 14, 2016 by Mira • edited Jan 14, 2016 by Mira

2 Answers

1

The Qt application QApplication main object supposed to be created in the standard main function:

int main(int argc, char *argv[])
{
   QApplication app(argc, argv);
   // more app objects initialization
   return app.exec();
}

and you do:

int CALLBACK WinMain(HINSTANCE, HINSTANCE, LPTSTR _lpCmdLine, int _nShowCmd)
{
   QApplication app(nShowCmd, & _lpCmdLine);
   return app.exec();
}

Which implies that &_lpCdLine being equivalent to argv but:

LPTSTR _lpCmdLine which is an equivalent to char* and you take an address of it so &_lpCmdLine matches char** when _lpCmdLine is pointing to a contiguous buffer of characters and not an array of character strings as argv.

It makes sense to consider how main() function is implemented on Windows. There is a good discussion: WINMAIN and main() in C++ (Extended) Mind that the C runtime must be initialized before main() function runs and that may depend on compiler/linker. And also find an example of Qt application main function.

I guess that the case when you make your code not to crash by introducingn nShowCmd == 0 makes QApplication object not to read the command line which prevents an incorrect access via random content interpreted as addresses in _lpCmdLine. But that is still wrong and an incomplete intitialization of QApplication object.

The authors fails to see the window and sees the console instead and that relates to incomplete code of main function not launching any window. Also, QML application main.cpp might help.

answered on Stack Overflow Jan 14, 2016 by Alexander V • edited May 23, 2017 by Community
0

Since this problem becomes relevant again with Qt6, here is a very simple solution for VisualStudio with using WinMain as entry point:

QApplication app(__argc, __argv);

__argc and __argv are populated by the microsoft compiler (found this suggestion here: https://codingmisadventures.wordpress.com/2009/03/10/retrieving-command-line-parameters-from-winmain-in-win32/)

Short explanation of the problem: using main() with VC requires to use Subsystem:Console, but this always opens a console window, which you wanna avoid usually. You cannot get rid of this console, except with some very dirty hacks. So you need to switch to Subsystem:Windows to have a "silent" application launch, but this requires to use WinMain() as application entry point. Up to Qt5 there was an own implementation of WinMain() in qtmain.lib, which you simply linked in, and which forwards to the main()-function, but this has been kicked out in Qt6. The problem exists afaik only for MSVC users, because with mingw you can disable the console window and continue using main(), as shown in the Qt-examples.

answered on Stack Overflow May 9, 2021 by chamile

User contributions licensed under CC BY-SA 3.0