December 1, 2011

Adi Adi
Lab Rat
120 posts

QString to LPCTSTR conversion


HI All

I am using win32 dll in my QT Qt Application.
win32 dll has one function which takes parameter of type LPCTSTR.
When i try to call this function from my Qt Application passing QString as parameter function is not recieveng the correct value .
How to resolve this?

4 replies

December 1, 2011

koahnig koahnig
Robot Herder
2954 posts

Which compiler are you using?
How did you perform the conversion?

December 1, 2011

broadpeak broadpeak
Lab Rat
309 posts

LPCTSTR = Long Pointer to a Const TCHAR STRing
(const char*)

Try the simple casting.

December 1, 2011

Volker Volker
Ant Farmer
5331 posts

Try QString::toWCharArray() [].

December 1, 2011

Gerolf Gerolf
Hobby Entomologist
3251 posts

If it is a pure in parameter, you can try:

  1. void fooSub(LPSTSTR X); // this is our function :-)
  3. foo()
  4. {
  5.     QString text;
  6.     if(sizeof(TCHAR) == 1)
  7.         fooSub((LPCSTR)text.toLocal8Bit().constData()); // here you have to check, how to convert, you could also use utf8(), ...
  8.     else
  9.         fooSub((LPCWSTR)text.utf16());
  10. }


Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

  ‹‹ Bad ptr when passing an argument      QSystemTrayIcon[SOLVED] ››

You must log in to post a reply. Not a member yet? Register here!