Out of scope pointer - how to fix?
-
Yeah, it works, but I would still recommend something like:
@
QStringList logicalDrives()
{
auto length = GetLogicalDriveStrings(0, nullptr);
auto array = std::make_unique<wchar_t[]>(length + 1);
GetLogicalDriveStrings(length + 1, array.get());
return QString::fromWCharArray(array.get(), length)
.split(QChar('\0'), QString::SkipEmptyParts);
}
@No "new"s, don't have to worry about freeing stuff, no memcpy (yuck) etc.
-
And how do I cast QString to wchar_t?
@BOOL WINAPI GetDiskFreeSpaceEx(
In_opt LPCTSTR lpDirectoryName,
Out_opt PULARGE_INTEGER lpFreeBytesAvailable,
Out_opt PULARGE_INTEGER lpTotalNumberOfBytes,
Out_opt PULARGE_INTEGER lpTotalNumberOfFreeBytes
);@ -
Eg.
@
QString s = "whatever";
WhateverWinAPICall(s.toStdWString().c_str());
@ -
Thanks, I will try this.
Slowly I get into this. -
You like to copy lpBuffer (the pointer) or the content it points to?
At the moment you are copying the pointer only and keep.
This like making a scratch in the paint of a boat where someone was falling off. In harbor you can tell the rescue guys exactly where the guy felt off. Unfortunately, it will not rescue the guy from drowning ;- )In the constructor you would need to allocate memory and copy from one pointer to the other.
@
lpDirectoryName = new wchar_t [MAX_PATH];
memcpy (lpDirectoryName, mountPoint, sizeof (wchar_t) * MAX_PATH);
@
Please check with size. This is not tested.Alternatively, you can copy byte by byte in a for loop.
Personally I would copy the information to string. std::string or QString. The followup handling is easier.
[edit, since interrupted, I had missed out the other exchange, koahnig]
-
[quote author="Chris Kawa" date="1393862155"]Yeah, it works, but I would still recommend something like:
@
QStringList logicalDrives()
{
auto length = GetLogicalDriveStrings(0, nullptr);
auto array = std::make_unique<wchar_t[]>(length + 1);
GetLogicalDriveStrings(length + 1, array.get());
return QString::fromWCharArray(array.get(), length)
.split(QChar('\0'), QString::SkipEmptyParts);
}
@No "new"s, don't have to worry about freeing stuff, no memcpy (yuck) etc.[/quote]
How can I do the same without C++11? Auto and nullptr are undefined and even std::make_unique throws "is not a member" when C++11 is enabled.
Thanks.
-
Yeah, sorry, make_unique is actually part of C++14 but newer compilers already support it for some time so I'm used to it (I can't recommend enough to upgrade. It's another world :) ).
In C++03 just replace the nice stuff with the ugly one :)
@
QStringList logicalDrives()
{
DWORD length = GetLogicalDriveStrings(0, NULL);
QScopedPointer<wchar_t, QScopedPointerArrayDeleter<wchar_t> > array(new wchar_t[length+1]);
GetLogicalDriveStrings(length + 1, array.data());
return QString::fromWCharArray(array.data(), length)
.split(QChar('\0'), QString::SkipEmptyParts);
}
@ -
Do I have any new dependencies for my application? I am using Qt 5.2.1, most likly I upgrade to any new release. If there are no new dependencies, I would upgrade, too.
-
Well it depends on what you're using now :)
For MSVC make_unique is available from VS2013 (or VS2012 November CTP to be precise) and if you're using MinGW 4.8 from the online installer it's already there, you just need to add QMAKE_CXXFLAGS += -std=c++1y in the .pro file.As for app dependencies - only the usual: Qt libs, plugins, and system/compiler runtime just like in any version.
-
-I still get this warning:-
@C:\folder\file.cpp:53: Warnung: 'auto' changes meaning in C++11; please remove it [-Wc++0x-compat]@-as well as:-
@C:\folder\file.cpp:53: Fehler: 'length' does not name a type
auto length = GetLogicalDriveStrings(0, nullptr);
^@-I tried the online MinGW install, but there are components missing / headers are not compatible anymore in several of my classes.-
Works now, just had to comment out my WINVER define.
Now I am not able to compile some spcial Vista+ APIs but I can solve this later.