How to get WinMain function's params in Qt's main function?
-
wrote on 4 Jul 2023, 09:35 last edited by
this is how Qt's main function is called. how can I get hInstance, hPrevInstance, lpCmdLine and nShowCmd?
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd)
extern "C" int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR /*cmdParamarg*/, int /* cmdShow */) { int argc = 0; wchar_t **argvW = CommandLineToArgvW(GetCommandLineW(), &argc); if (argvW == nullptr) return -1; char **argv = new char *[argc + 1]; for (int i = 0; i != argc; ++i) argv[i] = wideToMulti(CP_ACP, argvW[i]); argv[argc] = nullptr; LocalFree(argvW); const int exitCode = main(argc, argv); for (int i = 0; (i != argc) && (argv[i] != nullptr); ++i) delete [] argv[i]; delete [] argv; return exitCode; }
-
Lifetime Qt Championwrote on 4 Jul 2023, 10:00 last edited by Chris Kawa 7 Apr 2023, 10:13
@gaojinhsu These parameters get abstracted away by Qt into the cross-platform form.
To get the
lpCmdLine
which is basically the command line parameters you can use QCoreApplication::arguments(). This gets you the arguments as a list of QStrings.
If you want the command line in native form you can use the WinAPI GetCommandLineA() function.To get
hInstance
you can use a native call toGetModuleHandle(NULL)
. This will get you the handle to the current executable (which is usually what you mean unless you somehow modified Qt app to run entirely as a DLL, then you need the DLL name passed as the parameter).As for
hPrevInstance
this is a legacy parameter that on any 32 or 64 bit Windows is always null. It's a leftover from a 16 bit OS days, so you can safely ignore it or assume null if you need to. -
this is how Qt's main function is called. how can I get hInstance, hPrevInstance, lpCmdLine and nShowCmd?
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd)
extern "C" int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR /*cmdParamarg*/, int /* cmdShow */) { int argc = 0; wchar_t **argvW = CommandLineToArgvW(GetCommandLineW(), &argc); if (argvW == nullptr) return -1; char **argv = new char *[argc + 1]; for (int i = 0; i != argc; ++i) argv[i] = wideToMulti(CP_ACP, argvW[i]); argv[argc] = nullptr; LocalFree(argvW); const int exitCode = main(argc, argv); for (int i = 0; (i != argc) && (argv[i] != nullptr); ++i) delete [] argv[i]; delete [] argv; return exitCode; }
wrote on 4 Jul 2023, 09:42 last edited by@gaojinhsu
So far as I know, to access these individual top-level parameters toWinMain()
you would need to provide your own definition code for this function, e.g. as per above. -
@gaojinhsu
So far as I know, to access these individual top-level parameters toWinMain()
you would need to provide your own definition code for this function, e.g. as per above. -
Lifetime Qt Championwrote on 4 Jul 2023, 10:00 last edited by Chris Kawa 7 Apr 2023, 10:13
@gaojinhsu These parameters get abstracted away by Qt into the cross-platform form.
To get the
lpCmdLine
which is basically the command line parameters you can use QCoreApplication::arguments(). This gets you the arguments as a list of QStrings.
If you want the command line in native form you can use the WinAPI GetCommandLineA() function.To get
hInstance
you can use a native call toGetModuleHandle(NULL)
. This will get you the handle to the current executable (which is usually what you mean unless you somehow modified Qt app to run entirely as a DLL, then you need the DLL name passed as the parameter).As for
hPrevInstance
this is a legacy parameter that on any 32 or 64 bit Windows is always null. It's a leftover from a 16 bit OS days, so you can safely ignore it or assume null if you need to. -
@gaojinhsu These parameters get abstracted away by Qt into the cross-platform form.
To get the
lpCmdLine
which is basically the command line parameters you can use QCoreApplication::arguments(). This gets you the arguments as a list of QStrings.
If you want the command line in native form you can use the WinAPI GetCommandLineA() function.To get
hInstance
you can use a native call toGetModuleHandle(NULL)
. This will get you the handle to the current executable (which is usually what you mean unless you somehow modified Qt app to run entirely as a DLL, then you need the DLL name passed as the parameter).As for
hPrevInstance
this is a legacy parameter that on any 32 or 64 bit Windows is always null. It's a leftover from a 16 bit OS days, so you can safely ignore it or assume null if you need to.wrote on 6 Jul 2023, 02:45 last edited by@Chris-Kawa That' s exactly what I want, thank you.
-
1/5