Convert DWORD to String with c++ - c++

I get Idmachine with GetVolumeInformation command. I want changed idmachine (DWORD) to string then show in TEdit(C++ builder) but not. Please help me!
GetVolumeInformation(L"C:\\",NULL,NULL,&dwVolSerial,NULL,NULL,NULL,NULL);
std::wstring wstr = std::to_wstring(idmachine);
std::string str = std::string(wstr.begin(), wstr.end());
ShowMessage(str);
Error
Vcl.Dialogs.hpp(1430): candidate function not viable: no known conversion from 'std::string' (aka 'basic_string, allocator >') to 'const System::UnicodeString' for 1st argument

Related

argument of type is incompatible with parameter of type error [duplicate]

This question already has answers here:
Cannot convert parameter from 'const char[20]' to 'LPCWSTR'
(3 answers)
'CreateDirectoryW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR' in OpenCV 2.4.5 and VS 2010
(3 answers)
cannot convert 'const char*' to 'LPCWSTR {aka const wchar_t*}'
(4 answers)
'HMODULE LoadLibraryA(LPCSTR)': cannot convert argument 1 from 'const _Elem *' to 'LPCSTR'
(2 answers)
Cannot convert const char[16] to LPCWSTR [closed]
(1 answer)
Closed 1 year ago.
uintptr_t gameModule = (uintptr_t)GetModuleHandle("client.dll");
Severity Code Description Project File Line Suppression State
Error C2664 'HMODULE GetModuleHandleW(LPCWSTR)': cannot convert argument 1 from 'const char [11]' to 'LPCWSTR'
uintptr_t gameModule = (uintptr_t)GetModuleHandle("client.dll");
HMODULE GetModuleHandleW(LPCWSTR)': cannot convert argument 1 from
'const char [11]' to 'LPCWSTR'
"client.dll" is a char string (const char [11]).
According to the Windows API TCHAR model, GetModuleHandle is a preprocessor macro which is expanded to GetModuleHandleW in Unicode builds (the default build mode for Visual Studio C++ projects since VS 2005).
GetModuleHandleW requires a LPCWSTR string parameter, i.e. a const wchar_t*, which is a wchar-t string.
So, you have a mismatch in your GetModuleHandle call, as you passed a char string, but GetModuleHandle (which is expanded to GetModuleHandleW) requires a wchar_t string (LPCWSTR).
You can fix this error passing L"client.dll" instead of "client.dll"; in fact, L"client.dll" (note the L prefix) is a wchar_t string:
// Pass L"client.dll" instead of "client.dll"
uintptr_t gameModule = (uintptr_t)GetModuleHandle(L"client.dll");
Another option would be explicitly invoking the "ANSI" function GetModuleHandleA:
// Explicitly call GetModuleHandleA
uintptr_t gameModule = (uintptr_t)GetModuleHandleA("client.dll");
but I would stick with Unicode APIs.
You could even totally embrace the TCHAR model, and decorate your string literal with _T() or TEXT(), e.g.:
uintptr_t gameModule = (uintptr_t)GetModuleHandle(_T("client.dll"));
That would work in both ANSI and UNICODE builds.

How to convert a std::string_view to a QStringView

I tried the following conversions, but all give me a no matching constructor for initialization of 'QStringView' error (comments reflect the constructor I was trying to call):
string someString = "hello world";
string_view strView( someString );
// QStringView(const Char (&)[N] string = N) or
// QStringView(const Char *str, qsizetype len)
QStringView qStrView1(strView.data(), strView.size());
// QStringView(const Char *first, const Char *last)
QStringView qStrView2(strView.data(), strView.data() + strView.size());
// QStringView(const Char *first, const Char *last)
QStringView qStrView3(strView.begin(), strView.end());
QStringView qStrView3a(strView.cbegin(), strView.cend());
// QStringView(const Char *str)
QStringView qStrView4(strView.data());
(I thought the 1st or 2nd conversion might work, and tried the 3rd and 4th just out of disappointment.)
Can someone please point me to the right conversion? Do I miss something?
Or do I need to dublicate all std::strings as QStrings and create QStringViews from these, by reusing the begin/end positions?
( Besides that, I did not now figure out, how to insert a QStringView into a QTableWidgetItem. I would appreciate any help on this problem as well. )
Context
I read out file content as std::string and split it into lines of fields based on separators. In order to do this efficiently, I generated a std::vector of std::string_view to store the fields. Now I want to visualize the strings in a Qt GUI (actually aiming at QTableWidgetItems) and thought QStringView might serve me well for the GUI part. I want to keep BusinessLogic independent of the GUI an therefore avoided any includes of Qt libraries there.
Setting
Qt 5.15.0
CONFIG += c++17
MSVC2019 amd64
Windows 10
Errors
(I removed note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 2 were provided, note: candidate constructor template not viable: requires single argument 'str', but 2 arguments were provided and alike)
qStrView1:
qstringview.h:173:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatibleCharType<char>::value' was not satisfied [with Char = char]
qstringview.h:178:22: note: candidate template ignored: could not match 'const Char *' against 'std::basic_string_view::size_type' (aka 'unsigned long long')
qstringview.h:191:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatibleArray<const char *>::value' was not satisfied [with Array = const char *]
qStrView2:
qstringview.h:173:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatibleCharType<char>::value' was not satisfied [with Char = char]
qstringview.h:178:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatibleCharType<char>::value' was not satisfied [with Char = char]
qstringview.h:191:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatibleArray<const char *>::value' was not satisfied [with Array = const char *]
qStrView3:
qstringview.h:173:22: note: candidate template ignored: could not match 'const Char *' against 'std::basic_string_view<char, std::char_traits<char> >::const_iterator' (aka '_String_view_iterator<std::char_traits<char> >')
qstringview.h:178:22: note: candidate template ignored: could not match 'const Char *' against 'std::basic_string_view<char, std::char_traits<char> >::const_iterator' (aka '_String_view_iterator<std::char_traits<char> >')
qstringview.h:191:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatibleArray<std::_String_view_iterator<std::char_traits<char> > >::value' was not satisfied [with Array = std::_String_view_iterator<std::char_traits<char> >]
qStrView3a:
qstringview.h:173:22: note: candidate template ignored: could not match 'const Char *' against 'std::basic_string_view<char, std::char_traits<char> >::const_iterator' (aka '_String_view_iterator<std::char_traits<char> >')
qstringview.h:178:22: note: candidate template ignored: could not match 'const Char *' against 'std::basic_string_view<char, std::char_traits<char> >::const_iterator' (aka '_String_view_iterator<std::char_traits<char> >')
qstringview.h:191:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatibleArray<std::_String_view_iterator<std::char_traits<char> > >::value' was not satisfied [with Array = std::_String_view_iterator<std::char_traits<char> >]
qStrView4:
qstringview.h:103:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'std::basic_string_view<char, std::char_traits<char> >::const_pointer' (aka 'const char *') to 'const QStringView' for 1st argument
qstringview.h:103:7: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'std::basic_string_view<char, std::char_traits<char> >::const_pointer' (aka 'const char *') to 'QStringView' for 1st argument
qstringview.h:169:22: note: candidate constructor not viable: no known conversion from 'std::basic_string_view<char, std::char_traits<char> >::const_pointer' (aka 'const char *') to 'std::nullptr_t' (aka 'nullptr_t') for 1st argument
qstringview.h:196:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatibleArray<const char *>::value' was not satisfied [with Array = const char *]
qstringview.h:200:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatiblePointer<const char *>::value' was not satisfied [with Pointer = const char *]
qstringview.h:209:5: note: candidate template ignored: requirement 'std::is_same<const char *, QString>::value || std::is_same<const char *, QStringRef>::value' was not satisfied [with String = const char *]
qstringview.h:214:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatibleStdBasicString<const char *>::value' was not satisfied [with StdBasicString = const char *]
Thanks for pointing me towards std::wstring. It took me a while understand the wchar_t , wstring, wcout, wifstream, ... stuff, but finally I have the following code working:
Note: still requires CONFIG += 17 in .pro file and following includes
#include <QStringView>
#include <string_view>
#include <string>
// create wstring and convert to QStringView
wstring somewstring = L"abc -ä-ö-ü-ß-"; //!< example string
wstring_view wview(somewstring);
// either work:
QStringView qstrv2a(wview.data(), wview.size());
QStringView qstrv2b(wview.data(), wview.data() + wview.size());
where either of the above constructors works fine.
Example using QStringView inside a QLabel
// setup minimum GUI
QWidget* mainWidget = new QWidget(this);
setCentralWidget(mainWidget);
QVBoxLayout* mainLayout = new QVBoxLayout;
mainWidget->setLayout(mainLayout);
// create wstring and convert to QStringView
wstring somewstring = L"abc -ä-ö-ü-ß-"; //!< example string
wstring_view wview(somewstring);
// either work:
QStringView qstrv1(wview.data(), wview.size());
// convert to label
QLabel* otherLabel = new QLabel(qstrv1.toLocal8Bit());
mainLayout->addWidget(otherLabel);
Example from file to QLabel
// additinonal standard libararies:
#include <fstream>
#include <sstream>
// setup minimum GUI
QWidget* mainWidget = new QWidget(this);
setCentralWidget(mainWidget);
QVBoxLayout* mainLayout = new QVBoxLayout;
mainWidget->setLayout(mainLayout);
// read file with UTF-8 characters (to a std::string)
std::string tempFileContent;
ifstream file("./infile.txt", ios::binary);
file.seekg(0, file.end);
tempFileContent.resize(file.tellg());
file.seekg(0, file.beg);
file.read(&tempFileContent[0], tempFileContent.size());
file.close();
// convert std::string -> std::wstring
std::wstringstream wss;
wss << tempFileContent.c_str();
std::wstring fileContent = wss.str();
// convert std::wstring -> QStringView
// - either work:
QStringView qstrv2(fileContent.data(), fileContent.size());
// convert to label
QLabel* someLabel = new QLabel(qstrv2.toLatin1()); //!< WORKS
mainLayout->addWidget(someLabel);
/* these did not work:
// total garbage
QLabel(QString::fromUtf8(qstrv2.toString().toStdString().c_str()));
// misses just the 'ß'
QLabel* someLabel = new QLabel(qstrv2.toLocal8Bit());
*/
where file content was
a-b-c-ä-ö-ü-ß-0-1-2
interestingly reading a text file with wifstream to a wstring takes much longer than converting a read string to a wstring using wstringstream. (which is still much slower than reading to std::string itself)

RapidJSON how to query an object using a string variable

I'm getting an error when I try to query an object using a string variable, but not when I directly use a string.
JSON:
{"x": "hello"}
This works:
std::cout << document["x"].GetString();
This doesn't work:
std::string s = "x";
std::cout << document[s].GetString();
I'm getting this error:
error: no viable overloaded operator[] for type 'rapidjson::Document'
(aka 'GenericDocument<UTF8<> >')
std::cout << document[s].GetString();
~~~~~^~
note: candidate function not viable: no known conversion from 'std::string'
(aka 'basic_string<char, char_traits<char>, allocator<char> >') to 'SizeType'
(aka 'unsigned int') for 1st argument
What am I doing wrong?
Try
std::cout << document[s.c_str()].GetString();
It seems the operator was not overloaded for a std::string but for a C-string.
(Reference for the c_str member function)

'HMODULE GetModuleHandleW(LPCWSTR)': cannot convert argument 1 from 'const char *' to 'LPCWSTR'

I'm still rather new to C++ but I've ran into a problem I can't solve, this is my error message:
'HMODULE GetModuleHandleW(LPCWSTR)': cannot convert argument 1 from 'const char *' to 'LPCWSTR'
And this the line that is throwing the error:
ModuleHandle = (DWORD)GetModuleHandle(moduleName.c_str());
You are passing a char * to something that needs a wchar_t *. You'll have to either convert your stringtype to wchar_t *, for example using the MultiByteToWideChar function (https://msdn.microsoft.com/en-us/library/windows/desktop/dd319072%28v=vs.85%29.aspx), or you can use the non-wide version of GetModuleHandle by calling GetModuleHandleA() instead of GetModuleHandleW().

How can I use SimpleIni to return wchar_t and then convert to std::wstring?

SimpleIni Documentation says wchar_t is supported but I don't understand how to use it. This is what I tried:
CSimpleIniCaseW ini;
ini.LoadFile("myapp.ini");
std::wstring test(ini.GetValue("testsection", "testkey", ""));
error C2664:
'CSimpleIniTempl::GetValue'
: cannot convert parameter 1 from
'const char [12]' to 'const wchar_t *'
You need to specify your strings as L"testsection" << Note the L.