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 isQString 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.
-
Q_ASSERT(sizeof(TCHAR) == sizeof(wchar_t)); int size = n1.size(); LPCTSTR name = new TCHAR[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. -
@kshegunov said:
Q_ASSERT(sizeof(TCHAR) == sizeof(wchar_t));
int size = n1.size();
LPCTSTR name = new TCHAR[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.
-
@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 forTCHAR *
so this line looks likeTCHAR * 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. -
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.
-
@Guess11
This code is hanging by a thread, it works more as a coincidence than anything. As I saidLPTSTR
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.