What is the difference between Run and Debug in QtCreator?
-
Good old hungarian notation makes it clear that lpData is a pointer and you are passing a pointer to a temporary variable to it so it can't work.
LPCTSTR dataString = new TCHAR [cmd.size()+1]; for(int i=0i<cmd.size();++i) dataString[i]= static_cast<TCHAR>(cmd.at(i).unicode()); dataString[cmd.size()]=static_cast<TCHAR>('\0'); COPYDATASTRUCT data; data.dwData = 0x267811; data.cbData = cmd.size()+1; data.lpData = dataString; DWORD lpdwResult; LRESULT err = SendMessageTimeout(hwnd, WM_COPYDATA, 0, (LPARAM)&data, SMTO_ABORTIFHUNG, 2000, &lpdwResult); //WM_COPYDATA delete [] dataString;
-
qDebug() << data.lpData;
was what I used. It seems to print the pointer...
so i did some searching and used
char* a=(char *)data.lpData; for (int i=0; i<data.cbData; ++i) { qDebug() << *a ; ++a; }
I can see that in Run mode the characters are correct. And in Debug mode it is a bunch of garbage, so it fails. So why would this be different in debug mode?
-
Good old hungarian notation makes it clear that lpData is a pointer and you are passing a pointer to a temporary variable to it so it can't work.
LPCTSTR dataString = new TCHAR [cmd.size()+1]; for(int i=0i<cmd.size();++i) dataString[i]= static_cast<TCHAR>(cmd.at(i).unicode()); dataString[cmd.size()]=static_cast<TCHAR>('\0'); COPYDATASTRUCT data; data.dwData = 0x267811; data.cbData = cmd.size()+1; data.lpData = dataString; DWORD lpdwResult; LRESULT err = SendMessageTimeout(hwnd, WM_COPYDATA, 0, (LPARAM)&data, SMTO_ABORTIFHUNG, 2000, &lpdwResult); //WM_COPYDATA delete [] dataString;
@VRonin said in What is the difference between Run and Debug in QtCreator?:
data.lpData = dataString;
I'm getting a bunch of errors. What should I do?
191: error: assignment of read-only location '*(dataString + ((sizetype)(((unsigned int)i) * 2u)))'
dataString[i] = static_cast<TCHAR>(cmd.at(i).unicode());
^192: error: assignment of read-only location '(dataString + ((sizetype)(((unsigned int)cmd.QString::size()) * 2u)))'
dataString[cmd.size()]=static_cast<TCHAR>('\0');
^
error: invalid conversion from 'const void' to 'PVOID {aka void*}' [-fpermissive]
data.lpData = dataString;
^ -
sorry, didn't realise
LPCTSTR
(I too failed at reading hungarian notation) is defined as const, just replace it withLPTSTR
or withTCHAR*
or withauto
@VRonin said in What is the difference between Run and Debug in QtCreator?:
sorry, didn't realise
LPCTSTR
(I too failed at reading hungarian notation) is defined as const, just replace it withLPTSTR
or withTCHAR*
or withauto
Thanks for the note. So I change the code to:
TCHAR* dataString = new TCHAR [cmd.size()+1]; for(int i=0; i<cmd.size();++i) dataString[i] = static_cast<TCHAR>(cmd.at(i).unicode()); dataString[cmd.size()]=static_cast<TCHAR>('\0'); COPYDATASTRUCT data; data.dwData = MAGIC; data.cbData = cmd.size()+1; data.lpData = dataString; char* a=(char *)data.lpData; for (int i=0; i<data.cbData; ++i) { qDebug() << *a ; ++a; } DWORD lpdwResult; LRESULT err = SendMessageTimeout(hwnd, WM_COPYDATA, 0, (LPARAM)&data, SMTO_ABORTIFHUNG, 2000, &lpdwResult); //WM_COPYDATA
I can see the cmd being printed out (only half of cmd thought) but debug mode still failed. Target application now seems to get the first character of cmd, just as if I send QString* or QString.toLatin1().data().
I guess the question comes down to, what is the difference in QString::toLocal8bit().data() in Run mode and Debug mode?
-
@VRonin said in What is the difference between Run and Debug in QtCreator?:
sorry, didn't realise
LPCTSTR
(I too failed at reading hungarian notation) is defined as const, just replace it withLPTSTR
or withTCHAR*
or withauto
Thanks for the note. So I change the code to:
TCHAR* dataString = new TCHAR [cmd.size()+1]; for(int i=0; i<cmd.size();++i) dataString[i] = static_cast<TCHAR>(cmd.at(i).unicode()); dataString[cmd.size()]=static_cast<TCHAR>('\0'); COPYDATASTRUCT data; data.dwData = MAGIC; data.cbData = cmd.size()+1; data.lpData = dataString; char* a=(char *)data.lpData; for (int i=0; i<data.cbData; ++i) { qDebug() << *a ; ++a; } DWORD lpdwResult; LRESULT err = SendMessageTimeout(hwnd, WM_COPYDATA, 0, (LPARAM)&data, SMTO_ABORTIFHUNG, 2000, &lpdwResult); //WM_COPYDATA
I can see the cmd being printed out (only half of cmd thought) but debug mode still failed. Target application now seems to get the first character of cmd, just as if I send QString* or QString.toLatin1().data().
I guess the question comes down to, what is the difference in QString::toLocal8bit().data() in Run mode and Debug mode?
@Pauly said in What is the difference between Run and Debug in QtCreator?:
char* a=(char *)data.lpData;
Are you sure lpData is char and not wchar?
instead of
char* a=(char *)data.lpData; for (int i=0; i<data.cbData; ++i) { qDebug() << *a ; ++a; }
try printing it using
#if defined(UNICODE) || defined(_UNICODE) std::wcout #else std::cout #endif << static_cast<TCHAR*>(data.lpData);
what is the difference in QString::toLocal8bit().data() in Run mode and Debug mode?
QString::toLocal8bit() returns a temporary QByteArray,
.data()
returns a pointer to the internal data of that temporary variable. That pointer becomes invalid as soon as the temporary variable gets destroyed (i.e. a line below). The debug mode might just keep that variable alive for longer lo let you see inside it if you needed to -
@Pauly said in What is the difference between Run and Debug in QtCreator?:
char* a=(char *)data.lpData;
Are you sure lpData is char and not wchar?
instead of
char* a=(char *)data.lpData; for (int i=0; i<data.cbData; ++i) { qDebug() << *a ; ++a; }
try printing it using
#if defined(UNICODE) || defined(_UNICODE) std::wcout #else std::cout #endif << static_cast<TCHAR*>(data.lpData);
what is the difference in QString::toLocal8bit().data() in Run mode and Debug mode?
QString::toLocal8bit() returns a temporary QByteArray,
.data()
returns a pointer to the internal data of that temporary variable. That pointer becomes invalid as soon as the temporary variable gets destroyed (i.e. a line below). The debug mode might just keep that variable alive for longer lo let you see inside it if you needed to -
Where could I see the output as printing it your way? I'm running this at QtCreator and I can't figure out where the output goes. Thanks.
-
ok then, back to the basic:
void MyClass::SendCmd(HWND hwnd, QString cmd) { const QByteArray utf8Cmd = cmd.toUtf8(); COPYDATASTRUCT data; data.dwData = 0x267811; data.cbData = cmd.size()+1; data.lpData = utf8Cmd.data(); DWORD lpdwResult; LRESULT err = SendMessageTimeout(hwnd, WM_COPYDATA, 0, (LPARAM)&data, SMTO_ABORTIFHUNG, 2000, &lpdwResult); //WM_COPYDATA }
@Pauly said in What is the difference between Run and Debug in QtCreator?:
I think UTF8, since .toLocal8bit() is the only thing that works
toLocal8bit not necessarily == toUtf8