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. Error converting string to LPCTSTR with Qt
QtWS25 Last Chance

Error converting string to LPCTSTR with Qt

Scheduled Pinned Locked Moved Solved 3rd Party Software
winapi
17 Posts 7 Posters 7.4k 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.
  • RIVOPICOR Offline
    RIVOPICOR Offline
    RIVOPICO
    wrote on last edited by A Former User
    #1

    Hello i was converting my string to LPCTSTR like this code:

    string subclave="Hello";
    string valor="TheValue";
    LPCTSTR _subclave = TEXT(subclave.c_str());
    LPCTSTR _valor = TEXT(valor.c_str());
    

    But qt show me these errors why?
    alt text

    I can't use c_str or why these errors?

    1 Reply Last reply
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by
      #2

      Hi @RIVOPICO,

      The problem is that TEXT is not a function, but a macro. I don't have Windows handy, but if I remember correctly, it simply adds an L prefix a string literal (when Unicode is enabled). So, for example, TEXT("foo") becomes L"foo", which is something the compiler understands, but TEXT(valor.c_str()) becomes Lvalor.c_str(), which is why the compiler is looking for a (non-existent) Lvalor variable.

      The solution here, is to not use the TEXT macro. Instead, do something like:

      #ifdef UNICODE
      LPTSTR _valor = (LPTSTR)valor.utf16();
      #else
      LPTSTR _valor = (LPTSTR)valor.toLocal8Bit().constData();
      #endif
      

      There's probably a better way to convert to LPTSTR (someone else might suggest something better?), but you get the idea.

      Cheers.

      ? kshegunovK 2 Replies Last reply
      3
      • Paul ColbyP Paul Colby

        Hi @RIVOPICO,

        The problem is that TEXT is not a function, but a macro. I don't have Windows handy, but if I remember correctly, it simply adds an L prefix a string literal (when Unicode is enabled). So, for example, TEXT("foo") becomes L"foo", which is something the compiler understands, but TEXT(valor.c_str()) becomes Lvalor.c_str(), which is why the compiler is looking for a (non-existent) Lvalor variable.

        The solution here, is to not use the TEXT macro. Instead, do something like:

        #ifdef UNICODE
        LPTSTR _valor = (LPTSTR)valor.utf16();
        #else
        LPTSTR _valor = (LPTSTR)valor.toLocal8Bit().constData();
        #endif
        

        There's probably a better way to convert to LPTSTR (someone else might suggest something better?), but you get the idea.

        Cheers.

        ? Offline
        ? Offline
        A Former User
        wrote on last edited by A Former User
        #3

        @Paul-Colby The beauty and blessing of the Windows API :)

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by Chris Kawa
          #4

          Since you're not using unicode anyway you can also undefine UNICODE and your original code will work.
          Put in your .pro file DEFINES -= UNICODE

          But to be honest your usage of the TEXT macro is wrong anyway (for reasons @Paul-Colby mentioned) so you might just as well stop pretending and use the ANSI types explicitly and don't care for the UNICODE definition:
          LPCSTR _valor = valor.c_str();

          In fact, if you're passing that to some WinAPI function, you don't need to do that at all. You can just directly use the variable, e.g.

          SetWindowTextA(hwnd, valor.c_str());
          //or with UNICODE undefined:
          SetWindowText(hwnd, valor.c_str());
          
          RIVOPICOR 1 Reply Last reply
          2
          • Paul ColbyP Paul Colby

            Hi @RIVOPICO,

            The problem is that TEXT is not a function, but a macro. I don't have Windows handy, but if I remember correctly, it simply adds an L prefix a string literal (when Unicode is enabled). So, for example, TEXT("foo") becomes L"foo", which is something the compiler understands, but TEXT(valor.c_str()) becomes Lvalor.c_str(), which is why the compiler is looking for a (non-existent) Lvalor variable.

            The solution here, is to not use the TEXT macro. Instead, do something like:

            #ifdef UNICODE
            LPTSTR _valor = (LPTSTR)valor.utf16();
            #else
            LPTSTR _valor = (LPTSTR)valor.toLocal8Bit().constData();
            #endif
            

            There's probably a better way to convert to LPTSTR (someone else might suggest something better?), but you get the idea.

            Cheers.

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #5

            @Paul-Colby said in Error converting string to LPCTSTR with Qt:

            LPTSTR _valor = (LPTSTR)valor.toLocal8Bit().constData();
            

            That's returning a pointer to a temporary, though.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            3
            • B Offline
              B Offline
              bnogal
              wrote on last edited by
              #6

              If i remember well. You need to call a windows function to allocate a custom struct. Assing string pointer in the correct field and pass a pointer to the function to no the first word of the struct. After using it u should deallocate it with other windows call. I dont have my computer here but i remember that

              1 Reply Last reply
              0
              • Chris KawaC Chris Kawa

                Since you're not using unicode anyway you can also undefine UNICODE and your original code will work.
                Put in your .pro file DEFINES -= UNICODE

                But to be honest your usage of the TEXT macro is wrong anyway (for reasons @Paul-Colby mentioned) so you might just as well stop pretending and use the ANSI types explicitly and don't care for the UNICODE definition:
                LPCSTR _valor = valor.c_str();

                In fact, if you're passing that to some WinAPI function, you don't need to do that at all. You can just directly use the variable, e.g.

                SetWindowTextA(hwnd, valor.c_str());
                //or with UNICODE undefined:
                SetWindowText(hwnd, valor.c_str());
                
                RIVOPICOR Offline
                RIVOPICOR Offline
                RIVOPICO
                wrote on last edited by RIVOPICO
                #7

                @Chris-Kawa Sorry i dont understand very well. You are saying me that i dont need to do the conversion to get to work with my winapi function? But where i willl put this text which variable?

                SetWindowTextA(hwnd, valor.c_str());
                //or with UNICODE undefined:
                SetWindowText(hwnd, valor.c_str());
                
                Chris KawaC 1 Reply Last reply
                0
                • RIVOPICOR RIVOPICO

                  @Chris-Kawa Sorry i dont understand very well. You are saying me that i dont need to do the conversion to get to work with my winapi function? But where i willl put this text which variable?

                  SetWindowTextA(hwnd, valor.c_str());
                  //or with UNICODE undefined:
                  SetWindowText(hwnd, valor.c_str());
                  
                  Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @RIVOPICO said in Error converting string to LPCTSTR with Qt:

                  But where i willl put this text which variable?

                  Now I'm not sure I understand. You put it wherever you want. For example:

                  LPCSTR foo = "Hello!";
                  SetWindowTextA(hwnd, foo);
                  

                  or

                  LPCWSTR foo = L"Hello!";
                  SetWindowTextW(hwnd, foo);
                  

                  or

                  std::string foo = "Hello!";
                  SetWindowTextA(hwnd, foo.c_str());
                  

                  or

                  QString foo = "Hello";
                  SetWindowTextW(hwnd, (LPCWSTR)foo.utf16());
                  
                  RIVOPICOR 1 Reply Last reply
                  3
                  • Chris KawaC Chris Kawa

                    @RIVOPICO said in Error converting string to LPCTSTR with Qt:

                    But where i willl put this text which variable?

                    Now I'm not sure I understand. You put it wherever you want. For example:

                    LPCSTR foo = "Hello!";
                    SetWindowTextA(hwnd, foo);
                    

                    or

                    LPCWSTR foo = L"Hello!";
                    SetWindowTextW(hwnd, foo);
                    

                    or

                    std::string foo = "Hello!";
                    SetWindowTextA(hwnd, foo.c_str());
                    

                    or

                    QString foo = "Hello";
                    SetWindowTextW(hwnd, (LPCWSTR)foo.utf16());
                    
                    RIVOPICOR Offline
                    RIVOPICOR Offline
                    RIVOPICO
                    wrote on last edited by RIVOPICO
                    #9

                    @Chris-Kawa But for example if i have this function:

                    HRSRC res=FindResource(NULL,L"->SetWindowsTextA?",RT_RCDATA);
                    

                    How i can use SetWindowText? I must to save the result in my variable?. In other words, how i can apply setwindowstext to save in a var and put in my second argument?

                    1 Reply Last reply
                    0
                    • Chris KawaC Offline
                      Chris KawaC Offline
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10
                      HRSRC res=FindResource(NULL,L"->SetWindowsTextA?",RT_RCDATA);
                      

                      That doesn't make any sense whatsoever. SetWindowsText was just an example of WinAPI function since you never said what function you're interested in. Replace it with whatever function you need. If it's FindResource then you can do any of these:

                      HRSRC res = FindResourceA(NULL, "SomeResourceName", RT_RCDATA);
                      

                      or

                      HRSRC res = FindResourceW(NULL, L"SomeResourceName", RT_RCDATA);
                      

                      or

                      LPCSTR foo = "SomeResourceName";
                      HRSRC res = FindResourceA(NULL, foo, RT_RCDATA);
                      

                      or

                      LPCWSTR foo = L"SomeResourceName";
                      HRSRC res = FindResourceW(NULL, foo, RT_RCDATA);
                      

                      or

                      QString foo = "SomeResourceName";
                      HRSRC res = FindResourceW(NULL, (LPCWSTR)foo.utf16(), RT_RCDATA);
                      

                      or any of the dozen other possibilities.

                      RIVOPICOR 1 Reply Last reply
                      2
                      • Chris KawaC Chris Kawa
                        HRSRC res=FindResource(NULL,L"->SetWindowsTextA?",RT_RCDATA);
                        

                        That doesn't make any sense whatsoever. SetWindowsText was just an example of WinAPI function since you never said what function you're interested in. Replace it with whatever function you need. If it's FindResource then you can do any of these:

                        HRSRC res = FindResourceA(NULL, "SomeResourceName", RT_RCDATA);
                        

                        or

                        HRSRC res = FindResourceW(NULL, L"SomeResourceName", RT_RCDATA);
                        

                        or

                        LPCSTR foo = "SomeResourceName";
                        HRSRC res = FindResourceA(NULL, foo, RT_RCDATA);
                        

                        or

                        LPCWSTR foo = L"SomeResourceName";
                        HRSRC res = FindResourceW(NULL, foo, RT_RCDATA);
                        

                        or

                        QString foo = "SomeResourceName";
                        HRSRC res = FindResourceW(NULL, (LPCWSTR)foo.utf16(), RT_RCDATA);
                        

                        or any of the dozen other possibilities.

                        RIVOPICOR Offline
                        RIVOPICOR Offline
                        RIVOPICO
                        wrote on last edited by RIVOPICO
                        #11

                        @Chris-Kawa said in Error converting string to LPCTSTR with Qt:

                        LPCSTR foo = "SomeResourceName";
                        HRSRC res = FindResourceA(NULL, foo, RT_RCDATA);

                        When i use in qt findresourceA all time take me error i tried the two ways.
                        code:

                        HRSRC res = FindResourceA(NULL, "SomeResourceName", RT_RCDATA);
                        

                        error:

                        error: C2664: 'HRSRC FindResourceA(HMODULE,LPCSTR,LPCSTR)' : el argumento 3 no puede convertirse de 'LPWSTR' a 'LPCSTR'
                        the types are not related; the conversion require reinterpret_cast, conversi¢n of style of C or conversi¢n of style of function
                        

                        second way:

                        LPCSTR foo = "SomeResourceName";
                        HRSRC res = FindResourceA(NULL, foo, RT_RCDATA);
                        

                        error:

                        error: C2664: 'HRSRC FindResourceA(HMODULE,LPCSTR,LPCSTR)' : el argumento 3 no puede convertirse de 'LPWSTR' a 'LPCSTR'
                        Los tipos se¤alados no est n relacionados; la conversi¢n requiere reinterpret_cast, conversi¢n de estilo de C o conversi¢n de estilo de funci¢n
                        

                        i can't use the Ansi version with Qt and i dont know why,

                        i tried the ex version but not seems to work too:

                        LPCSTR foo = "SomeResourceName";
                        WORD myVariable;
                        HRSRC res = FindResourceExA(NULL, foo, RT_RCDATA,myVariable);
                        

                        error:

                        error: C2664: 'HRSRC FindResourceExA(HMODULE,LPCSTR,LPCSTR,WORD)' : el argumento 3 no puede convertirse de 'LPWSTR' a 'LPCSTR'
                        the types are not related; the conversion require reinterpret_cast, conversi¢n of style of C or conversi¢n of style of function
                        
                        1 Reply Last reply
                        0
                        • mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          Hi
                          Dont it complain about param 3 ?

                          error: C2664: 'HRSRC FindResourceA(HMODULE,LPCSTR,LPCSTR)' : el argumento 3 no puede convertirse de 'LPWSTR' a 'LPCSTR'

                          so it seems its RT_RCDATA that it barf at ?

                          1 Reply Last reply
                          1
                          • RIVOPICOR Offline
                            RIVOPICOR Offline
                            RIVOPICO
                            wrote on last edited by RIVOPICO
                            #13

                            lol the strange thing this compile

                            LPCWSTR foo = L"ELEXE";
                            HRSRC res = FindResourceW(NULL, foo, RT_RCDATA);
                            

                            But obviously return error 1812.
                            i tried ansi versión but i can't use it because return me error with the third argument?

                            mrjjM 1 Reply Last reply
                            0
                            • RIVOPICOR RIVOPICO

                              lol the strange thing this compile

                              LPCWSTR foo = L"ELEXE";
                              HRSRC res = FindResourceW(NULL, foo, RT_RCDATA);
                              

                              But obviously return error 1812.
                              i tried ansi versión but i can't use it because return me error with the third argument?

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @RIVOPICO

                              Here u ask it to use UNICODE version.
                              so now it likes RT_RCDATA

                              Please go to definition of RT_RCDATA and see what it is.
                              Most likely a unicode str.

                              1 Reply Last reply
                              1
                              • RIVOPICOR Offline
                                RIVOPICOR Offline
                                RIVOPICO
                                wrote on last edited by RIVOPICO
                                #15

                                yeah but i tried unicode version and return me error:

                                The specified resource name cannot be found in the image file.
                                

                                So i think with ansi version will be solved. so i will check in the page thanks for your recommendation.

                                1 Reply Last reply
                                1
                                • Chris KawaC Offline
                                  Chris KawaC Offline
                                  Chris Kawa
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  RT_RCDATA is a macro that expands to MAKEINTRESOURCE(10), which expands to either MAKEINTRESOURCEA(10) or MAKEINTRESOURCEW(10) when UNICODE is defined or not.
                                  So just replace RT_RCDATA in my exmples with either MAKEINTRESOURCEA(10) or MAKEINTRESOURCEW(10).

                                  RIVOPICOR 1 Reply Last reply
                                  1
                                  • Chris KawaC Chris Kawa

                                    RT_RCDATA is a macro that expands to MAKEINTRESOURCE(10), which expands to either MAKEINTRESOURCEA(10) or MAKEINTRESOURCEW(10) when UNICODE is defined or not.
                                    So just replace RT_RCDATA in my exmples with either MAKEINTRESOURCEA(10) or MAKEINTRESOURCEW(10).

                                    RIVOPICOR Offline
                                    RIVOPICOR Offline
                                    RIVOPICO
                                    wrote on last edited by RIVOPICO
                                    #17

                                    @Chris-Kawa Thanks a lot your answer solve my dude thanks a lot. The system of resources of Qt is awesome and more easy to use so it's better. Anyways i was trying to understand more about windows api and with these explications i understand now thanks.

                                    1 Reply Last reply
                                    0

                                    • Login

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