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*
Forum Updated to NodeBB v4.3 + New Features

Convert QString to wchar_t*

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 6 Posters 14.4k 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.
  • 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 Offline
              JonBJ Offline
              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