Integration 3rd party application into Qt form



  • 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.


  • Qt Champions 2016

    @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.



  • @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.



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

    Regards
    Gerd


  • Qt Champions 2016

    @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.



  • @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.


  • Qt Champions 2016

    @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.



  • @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);
    

  • Qt Champions 2016

    @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.



  • 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.


  • Qt Champions 2016

    @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.