Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Convert QString to wchar_t*

Convert QString to wchar_t*

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 6 Posters 16.0k Views 2 Watching
  • 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.
  • G Offline
    G Offline
    gkosi
    wrote on last edited by
    #2
    This post is deleted!
    aha_1980A 1 Reply Last reply
    1
    • G gkosi

      This post is deleted!

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #3

      hi @gkosi

      two comments to your code:

      1. the array needs to be at least the size of the sting contents
      2. the array is not null terminated, so you should make it one element larger and set this one to zero

      Regards

      Qt has to stay free or it will die.

      1 Reply Last reply
      3
      • M Offline
        M Offline
        Mr Gisa
        wrote on last edited by Mr Gisa
        #4

        I did it like this:

            // MAX_FILENAME_SIZE = 2048
            wchar_t filenameW[MAX_FILENAME_SIZE];
            int length = filename.left(MAX_FILENAME_SIZE - 1).toWCharArray(filenameW);
            filenameW[length] = '\0';
        

        Any tips for improvement?

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #5

          Since you have to use a container anyway, instead of a raw array I'd just use std::wstring:

          QString testStr("Test");
          std::wstring  testWstring  = testStr.toStdWString();
          functionThatTakesWcharPointer(testWstring.c_str());
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          M 1 Reply Last reply
          2
          • VRoninV VRonin

            Since you have to use a container anyway, instead of a raw array I'd just use std::wstring:

            QString testStr("Test");
            std::wstring  testWstring  = testStr.toStdWString();
            functionThatTakesWcharPointer(testWstring.c_str());
            
            M Offline
            M Offline
            Mr Gisa
            wrote on last edited by Mr Gisa
            #6

            @VRonin c_str returns a const right? It's not a const that it asks for, so I'm getting an error saying that can't convert from const wchar_t* to wchar_t*.

            ... I just put a const_cast<wchar_t *> and it compiled but the string isn't right, it's like this:

            0_1531804584324_5d3e913f-d6ed-4153-8e0b-efaa5aeb84e9-image.png

            D 1 Reply Last reply
            0
            • M Mr Gisa

              @VRonin c_str returns a const right? It's not a const that it asks for, so I'm getting an error saying that can't convert from const wchar_t* to wchar_t*.

              ... I just put a const_cast<wchar_t *> and it compiled but the string isn't right, it's like this:

              0_1531804584324_5d3e913f-d6ed-4153-8e0b-efaa5aeb84e9-image.png

              D Offline
              D Offline
              Devopia53
              wrote on last edited by
              #7

              @Mr-Gisa

              Just use like this:

              QString qString("Test");
              wchar_t *wc = reinterpret_cast<wchar_t *>(qString.data())
              const wchar_t *cwc = reinterpret_cast<const wchar_t *>(qString.utf16());
              
              aha_1980A 1 Reply Last reply
              0
              • D Devopia53

                @Mr-Gisa

                Just use like this:

                QString qString("Test");
                wchar_t *wc = reinterpret_cast<wchar_t *>(qString.data())
                const wchar_t *cwc = reinterpret_cast<const wchar_t *>(qString.utf16());
                
                aha_1980A Offline
                aha_1980A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on last edited by aha_1980
                #8

                Hi @Devopia53,

                Your code assumes that wchar_t is 16 bit wide. That is not always the case, see the already mentioned docs.

                Qt has to stay free or it will die.

                1 Reply Last reply
                2
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #9

                  A function that takes non-const wchar_t* is very dodgy! it should have no way of knowing the maximum allocated memory to that pointer/array so it is either allocating it internally (hence our discussion has been pointless so far) or that function is terribly designed and it's just a ticking time bomb.
                  Can I ask what that function does?

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  M VRoninV 2 Replies Last reply
                  2
                  • VRoninV VRonin

                    A function that takes non-const wchar_t* is very dodgy! it should have no way of knowing the maximum allocated memory to that pointer/array so it is either allocating it internally (hence our discussion has been pointless so far) or that function is terribly designed and it's just a ticking time bomb.
                    Can I ask what that function does?

                    M Offline
                    M Offline
                    Mr Gisa
                    wrote on last edited by
                    #10

                    @VRonin It's not a function actually, sorry for the bad information, it's a struct.

                    struct RAROpenArchiveDataEx
                    {
                      char         *ArcName;
                      wchar_t      *ArcNameW;
                      unsigned int  OpenMode;
                      unsigned int  OpenResult;
                      char         *CmtBuf;
                      unsigned int  CmtBufSize;
                      unsigned int  CmtSize;
                      unsigned int  CmtState;
                      unsigned int  Flags;
                      UNRARCALLBACK Callback;
                      LPARAM        UserData;
                      unsigned int  Reserved[28];
                    };
                    

                    ArcNameW
                    Input parameter which should point to zero terminated Unicode string containing the archive name or NULL if Unicode name is not specified.

                    1 Reply Last reply
                    0
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #11

                      Who has the responsibility of managing memory allocation of those struct members?

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      1
                      • M Offline
                        M Offline
                        Mr Gisa
                        wrote on last edited by
                        #12

                        0_1531813356323_2919dca9-6d7f-4076-94ce-b52c80d4630a-image.png
                        I couldn't find the docs online.

                        RAROpenArchiveEx pretty much.

                        You can download the dlls, examples and the docs on the official website: https://www.rarlab.com/rar/UnRARDLL.exe

                        1 Reply Last reply
                        0
                        • VRoninV VRonin

                          A function that takes non-const wchar_t* is very dodgy! it should have no way of knowing the maximum allocated memory to that pointer/array so it is either allocating it internally (hence our discussion has been pointless so far) or that function is terribly designed and it's just a ticking time bomb.
                          Can I ask what that function does?

                          VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by
                          #13

                          Ok, got it now, looks like it is used-as-const so the const_cast<wchar_t *>(testWstring.c_str()) should work. The string you see in the debugger might just be because you are trying to read the variable past its lifetime, I wouldn't worry about it

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            Mr Gisa
                            wrote on last edited by Mr Gisa
                            #14

                            @VRonin It's not working. After calling RAROpenArchiveEx it writes to RAROpenArchiveDataEx.OpenResult and the result should be ERAR_SUCCESS but it's returning ERAR_EOPEN and isn't right. For some reason it's only working the way I did before:

                                // MAX_FILENAME_SIZE = 2048
                                wchar_t filenameW[MAX_FILENAME_SIZE];
                                int length = filename.left(MAX_FILENAME_SIZE - 1).toWCharArray(filenameW);
                                filenameW[length] = '\0';
                            
                            JonBJ 1 Reply Last reply
                            0
                            • M Mr Gisa

                              @VRonin It's not working. After calling RAROpenArchiveEx it writes to RAROpenArchiveDataEx.OpenResult and the result should be ERAR_SUCCESS but it's returning ERAR_EOPEN and isn't right. For some reason it's only working the way I did before:

                                  // MAX_FILENAME_SIZE = 2048
                                  wchar_t filenameW[MAX_FILENAME_SIZE];
                                  int length = filename.left(MAX_FILENAME_SIZE - 1).toWCharArray(filenameW);
                                  filenameW[length] = '\0';
                              
                              JonBJ Online
                              JonBJ Online
                              JonB
                              wrote on last edited by
                              #15

                              @Mr-Gisa said in Convert QString to wchar_t*:

                              ERAR_EOPEN

                              I don't know what state your code is in. But ERAR_EOPEN is some kind of "file is already open" error on a RAR archive, and it hardly seems likely that could be generated as a direct result of something about converting strings, does it to you?

                              M 1 Reply Last reply
                              1
                              • JonBJ JonB

                                @Mr-Gisa said in Convert QString to wchar_t*:

                                ERAR_EOPEN

                                I don't know what state your code is in. But ERAR_EOPEN is some kind of "file is already open" error on a RAR archive, and it hardly seems likely that could be generated as a direct result of something about converting strings, does it to you?

                                M Offline
                                M Offline
                                Mr Gisa
                                wrote on last edited by
                                #16

                                @JonB The code I posted worked because it's converting the path for the rar file correctly, altough when I try the alternatives here I get a weird string and not a correct path/rar file.

                                VRoninV 1 Reply Last reply
                                0
                                • M Mr Gisa

                                  @JonB The code I posted worked because it's converting the path for the rar file correctly, altough when I try the alternatives here I get a weird string and not a correct path/rar file.

                                  VRoninV Offline
                                  VRoninV Offline
                                  VRonin
                                  wrote on last edited by
                                  #17

                                  @Mr-Gisa said in Convert QString to wchar_t*:

                                  I get a weird string

                                  I suspect you are holding a pointer to a temporary variable. If you are doing something like this, you are doing it wrong:

                                  void fillName(RAROpenArchiveDataEx* rar, const QString& fileName){
                                  rar->ArcNameW = const_cast<wchar_t *>(fileName.toStdWString().c_str());
                                  }
                                  

                                  As the temporary std::wstring will be destroyed and rar->ArcNameW will contain garbage

                                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                  ~Napoleon Bonaparte

                                  On a crusade to banish setIndexWidget() from the holy land of Qt

                                  1 Reply Last reply
                                  0
                                  • M Offline
                                    M Offline
                                    Mr Gisa
                                    wrote on last edited by
                                    #18
                                    bool RAR::open(RAR::OpenMode mode)
                                    {
                                        RAROpenArchiveDataEx openArchiveData;
                                        memset(&openArchiveData, 0, sizeof(openArchiveData));
                                    
                                        openArchiveData.ArcName = 0;
                                    
                                        auto archive = const_cast<wchar_t *>(mArchive.toStdWString().c_str());
                                    
                                        openArchiveData.ArcNameW = archive;
                                        openArchiveData.OpenMode = mode;
                                        openArchiveData.CmtBuf = 0;
                                        openArchiveData.CmtBufSize = 0;
                                        openArchiveData.Callback = 0;
                                        openArchiveData.UserData = 0;
                                    
                                        mHandle = RAROpenArchiveEx(&openArchiveData);
                                        mResult = openArchiveData.OpenResult;
                                    
                                        if (mResult != ERAR_SUCCESS) {
                                            return false;
                                        }
                                    
                                        scan();
                                    
                                        return true;
                                    }
                                    
                                    VRoninV 1 Reply Last reply
                                    0
                                    • M Mr Gisa
                                      bool RAR::open(RAR::OpenMode mode)
                                      {
                                          RAROpenArchiveDataEx openArchiveData;
                                          memset(&openArchiveData, 0, sizeof(openArchiveData));
                                      
                                          openArchiveData.ArcName = 0;
                                      
                                          auto archive = const_cast<wchar_t *>(mArchive.toStdWString().c_str());
                                      
                                          openArchiveData.ArcNameW = archive;
                                          openArchiveData.OpenMode = mode;
                                          openArchiveData.CmtBuf = 0;
                                          openArchiveData.CmtBufSize = 0;
                                          openArchiveData.Callback = 0;
                                          openArchiveData.UserData = 0;
                                      
                                          mHandle = RAROpenArchiveEx(&openArchiveData);
                                          mResult = openArchiveData.OpenResult;
                                      
                                          if (mResult != ERAR_SUCCESS) {
                                              return false;
                                          }
                                      
                                          scan();
                                      
                                          return true;
                                      }
                                      
                                      VRoninV Offline
                                      VRoninV Offline
                                      VRonin
                                      wrote on last edited by VRonin
                                      #19

                                      @Mr-Gisa said in Convert QString to wchar_t*:

                                      auto archive = const_cast<wchar_t *>(mArchive.toStdWString().c_str());

                                      Boom! exactly as I supected!

                                      auto archiveWString = mArchive.toStdWString();
                                      auto archive = const_cast<wchar_t *>(archiveWString.c_str());
                                      

                                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                      ~Napoleon Bonaparte

                                      On a crusade to banish setIndexWidget() from the holy land of Qt

                                      1 Reply Last reply
                                      3
                                      • M Offline
                                        M Offline
                                        Mr Gisa
                                        wrote on last edited by
                                        #20

                                        @VRonin said in Convert QString to wchar_t*:

                                        auto archiveWString = mArchive.toStdWString();
                                        auto archive = const_cast<wchar_t *>(archiveWString.c_str());

                                        Yay now its working. Thx.

                                        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