Convert QString to wchar_t*
-
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());
@VRonin
c_str
returns aconst
right? It's not a const that it asks for, so I'm getting an error saying that can't convert fromconst wchar_t*
towchar_t*
.... I just put a
const_cast<wchar_t *>
and it compiled but the string isn't right, it's like this: -
@VRonin
c_str
returns aconst
right? It's not a const that it asks for, so I'm getting an error saying that can't convert fromconst wchar_t*
towchar_t*
.... I just put a
const_cast<wchar_t *>
and it compiled but the string isn't right, it's like this: -
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());
Hi @Devopia53,
Your code assumes that
wchar_t
is 16 bit wide. That is not always the case, see the already mentioned docs. -
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? -
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?@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. -
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
-
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? -
@VRonin It's not working. After calling
RAROpenArchiveEx
it writes toRAROpenArchiveDataEx.OpenResult
and the result should beERAR_SUCCESS
but it's returningERAR_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';
-
@VRonin It's not working. After calling
RAROpenArchiveEx
it writes toRAROpenArchiveDataEx.OpenResult
and the result should beERAR_SUCCESS
but it's returningERAR_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';
@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? -
@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? -
@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.
@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 andrar->ArcNameW
will contain garbage -
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; }
-
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; }
@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());