Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. 3rd Party Software
  4. Integration 3rd party application into Qt form

Integration 3rd party application into Qt form

Scheduled Pinned Locked Moved Solved 3rd Party Software
3rd party
11 Posts 3 Posters 3.1k Views
  • 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
    Guess11
    wrote on 20 Apr 2016, 13:43 last edited by
    #1

    I am trying to integrate some application that installed in my operation system to the Qt from. What I already read is this topic(https://forum.qt.io/topic/44091/embed-an-application-inside-a-qt-window-solved/7) but I am still have a problem with getting WinID of the application that I try to integrate. For example I am trying to catch Google Chrome WinID.
    What I am Allready write is

    
        QString n1="Google_Chrome";
        LPCWSTR name=n1.utf16();
        HWND hwnd_1 = ::FindWindow(name, NULL);
        LONG retVal = GetWindowLongA(hwnd_1, GWL_STYLE);
    

    But I am getting "cannot convert const unshort to LPCWSTR" error. If somebody can help me, I will be really thankful. I am using Windows 10 and Qt 5.5.1.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 20 Apr 2016, 16:12 last edited by kshegunov
      #2

      @Guess11

      Q_ASSERT(sizeof(TCHAR) == sizeof(wchar_t));
      
      int size = n1.size();
      
      LPCTSTR name = new T‌CHAR[size + 1];
      name[size] = 0;
      
      n1.toWCharArray(name);
      HWND hwnd_1 = ::FindWindow(name, NULL);
      
      delete [] name;
      

      One can't help it, but be inspired by MS's approach to designing an "usable" API.

      Read and abide by the Qt Code of Conduct

      G 1 Reply Last reply 21 Apr 2016, 07:44
      0
      • K kshegunov
        20 Apr 2016, 16:12

        @Guess11

        Q_ASSERT(sizeof(TCHAR) == sizeof(wchar_t));
        
        int size = n1.size();
        
        LPCTSTR name = new T‌CHAR[size + 1];
        name[size] = 0;
        
        n1.toWCharArray(name);
        HWND hwnd_1 = ::FindWindow(name, NULL);
        
        delete [] name;
        

        One can't help it, but be inspired by MS's approach to designing an "usable" API.

        G Offline
        G Offline
        Guess11
        wrote on 21 Apr 2016, 07:44 last edited by
        #3

        @kshegunov
        Thank you for your reply and help. Unfortunately now I have almost the same problem: "cannot convert from LPCTSTR to wchar_t". Indeed Microsoft has "usable" API. Probably it is impossible to nicely integrate 3rd party app to Qt form.

        K 1 Reply Last reply 21 Apr 2016, 08:12
        0
        • G Offline
          G Offline
          Gerd
          wrote on 21 Apr 2016, 08:08 last edited by
          #4

          LPCTSTR is a const Pointer, not usable for toWCharArray.
          Change it to LPTSTR and it should work.

          Regards
          Gerd

          G 1 Reply Last reply 21 Apr 2016, 09:04
          1
          • G Guess11
            21 Apr 2016, 07:44

            @kshegunov
            Thank you for your reply and help. Unfortunately now I have almost the same problem: "cannot convert from LPCTSTR to wchar_t". Indeed Microsoft has "usable" API. Probably it is impossible to nicely integrate 3rd party app to Qt form.

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 21 Apr 2016, 08:12 last edited by
            #5

            @Guess11
            See @Gerd's comment. There's indeed a typo in the example, so changing to LPTSTR (or simply to TCHAR * what LPTSTR stands for) should fix it.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            0
            • G Gerd
              21 Apr 2016, 08:08

              LPCTSTR is a const Pointer, not usable for toWCharArray.
              Change it to LPTSTR and it should work.

              Regards
              Gerd

              G Offline
              G Offline
              Guess11
              wrote on 21 Apr 2016, 09:04 last edited by
              #6

              @kshegunov said:

              Q_ASSERT(sizeof(TCHAR) == sizeof(wchar_t));

              int size = n1.size();

              LPCTSTR name = new T‌CHAR[size + 1];
              name[size] = 0;

              n1.toWCharArray(name);
              HWND hwnd_1 = ::FindWindow(name, NULL);

              delete [] name;
              Now I need to convert from *LPTSTR to LPCWSTR. My code look like this :

                  QString n1="Google_Chrome";
              
                  Q_ASSERT(sizeof(TCHAR) == sizeof(wchar_t));
              
                  int size = n1.size();
                  size=size+1;
                  LPTSTR * name = new LPTSTR [size];
                  name[size]=0;
                  HWND hwnd_1 = ::FindWindow(name, NULL);
                  LONG retVal = GetWindowLongA(hwnd_1, GWL_STYLE);
                  qDebug()<<"Chrome id " <<hwnd_1;
              

              if I put

               HWND hwnd_1 = ::FindWindow(*name, NULL);
              

              I get unresolved externals from linker. Other way(without pointer) it is just problem to convert from *LPCTSTR to LPCWSTR. If some one has better idea how I can catсh winId of some external program please tell me.

              K 1 Reply Last reply 21 Apr 2016, 09:11
              0
              • G Guess11
                21 Apr 2016, 09:04

                @kshegunov said:

                Q_ASSERT(sizeof(TCHAR) == sizeof(wchar_t));

                int size = n1.size();

                LPCTSTR name = new T‌CHAR[size + 1];
                name[size] = 0;

                n1.toWCharArray(name);
                HWND hwnd_1 = ::FindWindow(name, NULL);

                delete [] name;
                Now I need to convert from *LPTSTR to LPCWSTR. My code look like this :

                    QString n1="Google_Chrome";
                
                    Q_ASSERT(sizeof(TCHAR) == sizeof(wchar_t));
                
                    int size = n1.size();
                    size=size+1;
                    LPTSTR * name = new LPTSTR [size];
                    name[size]=0;
                    HWND hwnd_1 = ::FindWindow(name, NULL);
                    LONG retVal = GetWindowLongA(hwnd_1, GWL_STYLE);
                    qDebug()<<"Chrome id " <<hwnd_1;
                

                if I put

                 HWND hwnd_1 = ::FindWindow(*name, NULL);
                

                I get unresolved externals from linker. Other way(without pointer) it is just problem to convert from *LPCTSTR to LPCWSTR. If some one has better idea how I can catсh winId of some external program please tell me.

                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 21 Apr 2016, 09:11 last edited by
                #7

                @Guess11
                Your code is wrong.

                Why are you doing this:

                 size=size+1;
                

                Then the allocation looks a bit iffy:

                LPTSTR * name = new LPTSTR [size];
                

                LPTSTR is a typedef for TCHAR * so this line looks like TCHAR * name = new TCHAR * [size] which is wrong syntactically as well as semantically. You want to create an array of wide characters, not an array of pointers to wide characters. You'd do it like this:

                TCHAR * name = new TCHAR[size + 1]; //< Don't modify the size variable
                

                Then:

                name[size]=0;
                

                leads to a buffer overflow (writing beyond allocated space). And finally you didn't copy the QString object's contents into your array. This line n1.toWCharArray(name); I can't see in your code.

                Read and abide by the Qt Code of Conduct

                G 1 Reply Last reply 21 Apr 2016, 09:40
                1
                • K kshegunov
                  21 Apr 2016, 09:11

                  @Guess11
                  Your code is wrong.

                  Why are you doing this:

                   size=size+1;
                  

                  Then the allocation looks a bit iffy:

                  LPTSTR * name = new LPTSTR [size];
                  

                  LPTSTR is a typedef for TCHAR * so this line looks like TCHAR * name = new TCHAR * [size] which is wrong syntactically as well as semantically. You want to create an array of wide characters, not an array of pointers to wide characters. You'd do it like this:

                  TCHAR * name = new TCHAR[size + 1]; //< Don't modify the size variable
                  

                  Then:

                  name[size]=0;
                  

                  leads to a buffer overflow (writing beyond allocated space). And finally you didn't copy the QString object's contents into your array. This line n1.toWCharArray(name); I can't see in your code.

                  G Offline
                  G Offline
                  Guess11
                  wrote on 21 Apr 2016, 09:40 last edited by
                  #8

                  @kshegunov
                  Fix that, but still problem is the same converting wchar_t to LPTSTR. this is the only one compilation error in the line:

                  n1.toWCharArray(name);
                  
                  K 1 Reply Last reply 21 Apr 2016, 09:42
                  0
                  • G Guess11
                    21 Apr 2016, 09:40

                    @kshegunov
                    Fix that, but still problem is the same converting wchar_t to LPTSTR. this is the only one compilation error in the line:

                    n1.toWCharArray(name);
                    
                    K Offline
                    K Offline
                    kshegunov
                    Moderators
                    wrote on 21 Apr 2016, 09:42 last edited by kshegunov
                    #9

                    @Guess11

                    Just reinterpret cast the hell out of it ;)

                    n1.toWCharArray(reinterpret_cast<wchar_t *>(name));
                    

                    PS.
                    Keep the Q_ASSERT on top, so you're sure that TCHAR is the size of wchar_t.

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      Guess11
                      wrote on 21 Apr 2016, 10:08 last edited by
                      #10

                      Ok. Thank you. All responses was very helpfull. It's work. I end up with this code:

                      #include <Windows.h>
                      #include <atlbase.h>
                      
                          Q_ASSERT(sizeof(TCHAR) == sizeof(wchar_t));
                      
                          QString n1="Google_Chrome";
                          int size = n1.size();
                      
                          LPTSTR * name = new LPTSTR [size+1];
                          name[size]=0;
                          n1.toWCharArray(reinterpret_cast<wchar_t *>(name));
                          LPCWSTR newname= reinterpret_cast<LPCWSTR> (name);
                          //HWND hwnd_1 = ::FindWindow(name, NULL);
                          HWND hwnd_1 = ::FindWindow(newname, NULL);
                          LONG retVal = GetWindowLongA(hwnd_1, GWL_STYLE);
                      
                          qDebug()<<"Chrome id " <<hwnd_1;
                          qDebug()<<"Long " <<retVal;
                      
                          delete [] name;
                      

                      I am wondering why program crashes after each time I closed it, But now I need to figure out why winId 0. Again thank you.

                      K 1 Reply Last reply 21 Apr 2016, 10:11
                      0
                      • G Guess11
                        21 Apr 2016, 10:08

                        Ok. Thank you. All responses was very helpfull. It's work. I end up with this code:

                        #include <Windows.h>
                        #include <atlbase.h>
                        
                            Q_ASSERT(sizeof(TCHAR) == sizeof(wchar_t));
                        
                            QString n1="Google_Chrome";
                            int size = n1.size();
                        
                            LPTSTR * name = new LPTSTR [size+1];
                            name[size]=0;
                            n1.toWCharArray(reinterpret_cast<wchar_t *>(name));
                            LPCWSTR newname= reinterpret_cast<LPCWSTR> (name);
                            //HWND hwnd_1 = ::FindWindow(name, NULL);
                            HWND hwnd_1 = ::FindWindow(newname, NULL);
                            LONG retVal = GetWindowLongA(hwnd_1, GWL_STYLE);
                        
                            qDebug()<<"Chrome id " <<hwnd_1;
                            qDebug()<<"Long " <<retVal;
                        
                            delete [] name;
                        

                        I am wondering why program crashes after each time I closed it, But now I need to figure out why winId 0. Again thank you.

                        K Offline
                        K Offline
                        kshegunov
                        Moderators
                        wrote on 21 Apr 2016, 10:11 last edited by kshegunov
                        #11

                        @Guess11
                        This code is hanging by a thread, it works more as a coincidence than anything. As I said LPTSTR is a pointer!

                        LPTSTR * name = new LPTSTR [size+1];
                        

                        should simply be:

                        TCHAR * name = new TCHAR[size + 1];
                        

                        And this:

                        LPCWSTR newname= reinterpret_cast<LPCWSTR> (name);
                        

                        is not needed.

                        Read and abide by the Qt Code of Conduct

                        1 Reply Last reply
                        1

                        1/11

                        20 Apr 2016, 13:43

                        • Login

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