Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get WinMain function's params in Qt's main function?
Forum Updated to NodeBB v4.3 + New Features

How to get WinMain function's params in Qt's main function?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 648 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gaojinhsu
    wrote on last edited by
    #1

    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;
    }
    
    JonBJ 1 Reply Last reply
    0
    • G gaojinhsu

      @JonB you mean by modifying Qt's soruce code and compile it again?

      Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #4

      @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 to GetModuleHandle(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.

      G 1 Reply Last reply
      4
      • G gaojinhsu

        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;
        }
        
        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #2

        @gaojinhsu
        So far as I know, to access these individual top-level parameters to WinMain() you would need to provide your own definition code for this function, e.g. as per above.

        G 1 Reply Last reply
        0
        • JonBJ JonB

          @gaojinhsu
          So far as I know, to access these individual top-level parameters to WinMain() you would need to provide your own definition code for this function, e.g. as per above.

          G Offline
          G Offline
          gaojinhsu
          wrote on last edited by
          #3

          @JonB you mean by modifying Qt's soruce code and compile it again?

          Chris KawaC 1 Reply Last reply
          0
          • G gaojinhsu

            @JonB you mean by modifying Qt's soruce code and compile it again?

            Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by Chris Kawa
            #4

            @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 to GetModuleHandle(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.

            G 1 Reply Last reply
            4
            • Chris KawaC Chris Kawa

              @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 to GetModuleHandle(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.

              G Offline
              G Offline
              gaojinhsu
              wrote on last edited by
              #5

              @Chris-Kawa That' s exactly what I want, thank you.

              1 Reply Last reply
              0
              • G gaojinhsu has marked this topic as solved on

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved