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*
Servers for Qt installer are currently down

Convert QString to wchar_t*

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 6 Posters 14.5k 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.
  • M Mr Gisa
    17 Jul 2018, 04:26

    @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 17 Jul 2018, 06:09 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());
    
    A 1 Reply Last reply 17 Jul 2018, 06:16
    0
    • D Devopia53
      17 Jul 2018, 06:09

      @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());
      
      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 17 Jul 2018, 06:16 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
      • V Offline
        V Offline
        VRonin
        wrote on 17 Jul 2018, 07:11 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 V 2 Replies Last reply 17 Jul 2018, 07:35
        2
        • V VRonin
          17 Jul 2018, 07:11

          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 17 Jul 2018, 07:35 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
          • V Offline
            V Offline
            VRonin
            wrote on 17 Jul 2018, 07:37 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 17 Jul 2018, 07:45 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
              • V VRonin
                17 Jul 2018, 07:11

                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?

                V Offline
                V Offline
                VRonin
                wrote on 17 Jul 2018, 08:06 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 17 Jul 2018, 08:11 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';
                  
                  J 1 Reply Last reply 17 Jul 2018, 08:27
                  0
                  • M Mr Gisa
                    17 Jul 2018, 08:11

                    @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';
                    
                    J Offline
                    J Offline
                    JonB
                    wrote on 17 Jul 2018, 08:27 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 17 Jul 2018, 08:41
                    1
                    • J JonB
                      17 Jul 2018, 08:27

                      @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 17 Jul 2018, 08:41 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.

                      V 1 Reply Last reply 17 Jul 2018, 08:48
                      0
                      • M Mr Gisa
                        17 Jul 2018, 08:41

                        @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.

                        V Offline
                        V Offline
                        VRonin
                        wrote on 17 Jul 2018, 08:48 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 17 Jul 2018, 08:53 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;
                          }
                          
                          V 1 Reply Last reply 17 Jul 2018, 09:04
                          0
                          • M Mr Gisa
                            17 Jul 2018, 08:53
                            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;
                            }
                            
                            V Offline
                            V Offline
                            VRonin
                            wrote on 17 Jul 2018, 09:04 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 17 Jul 2018, 09:19 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

                              16/20

                              17 Jul 2018, 08:41

                              • Login

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