Convert QString to wchar_t*
-
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());