Consider the following code (Which works!):
int SetInputFile( const CString& fileName );
int SetInputFile(System::String^ fileName)
{
const char* str = (char*)(void*)Marshal::StringToHGlobalAnsi(fileName);
return m_Native->SetInputFile(str);
}
How do I handle/convert a case with this input?
int SetInputFiles( const CStringArray& fileNames );
Thank you Alex Farber and Nostromoo
After searching the web - I was unable to find and such conversion. There for I had the interface changed to receive a list of strings in a const CString& with a seperator character. Naturally your suggestions are as well good and valid solutions.
Related
I am using Il2CppInspector to generate scaffolding for a Unity game. I am able to convert System.String (app::String in Il2CppInspector) to std::string using the functions provided below.
How would I reverse this process; how do I convert a std::string to System.String?
helpers.cpp
// Helper function to convert Il2CppString to std::string
std::string il2cppi_to_string(Il2CppString* str) {
std::u16string u16(reinterpret_cast<const char16_t*>(str->chars));
return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(u16);
}
// Helper function to convert System.String to std::string
std::string il2cppi_to_string(app::String* str) {
return il2cppi_to_string(reinterpret_cast<Il2CppString*>(str));
}
In short, I am looking for a function that takes in a std::string and returns an app::String
// Helper function to convert std::string to System.String
app::String string_to_il2cppi(std::string str) {
// Conversion code here
}
The accepted answer is actually wrong, there is no size parameter and copying stops at the first null byte (0x00) according to the MSDN documentation.
The following code fixes these problems and works correctly:
app::String* string_to_il2cppi(const std::string& string)
{
const auto encoding = (*app::Encoding__TypeInfo)->static_fields->utf8Encoding;
const auto managed_string = app::String_CreateStringFromEncoding((uint8_t*)&string.at(0), string.size(), encoding, nullptr);
return managed_string;
}
A quote from djkaty:
To create a string, you cannot use System.String‘s constructors –
these are redirected to icalls that throw exceptions. Instead, you
should use the internal Mono function String.CreateString. This
function has many overloads accepting various types of pointer and
array; an easy one to use accepts a uint16_t* to a Unicode string and
can be called as follows [...]
Export Il2CppInspector with all namespaces, which will give you access to Marshal_PtrToStringAnsi.
app::String* string_to_il2cppi(std::string str) {
return app::Marshal_PtrToStringAnsi((void*)&str, NULL);
}
Limitation: do not attempt to convert a string with null terminators inside of them example:
std::string test = "Hello\0world";
Use BullyWiiPlaza's solution if this is an issue for you.
I'm making a program which recursively lists all files in a certain directory and uploads each file separately to an FTP server using WinINet.
The problem I'm encountering is using filesystem::path::filename in the FtpPutFile() function because a LPCWSTR is needed.
Whats the best and easiest way to convert it (or somehow use it as is)?
std::string path = "C:\\Programs";
for (const auto & entry : std::experimental::filesystem::recursive_directory_iterator(path))
FtpPutFile(hIConnect, entry.path().filename(), entry.path().filename(), FTP_TRANSFER_TYPE_BINARY, 0);
The error I get is:
no suitable conversion function from "const std::experimental::filesystem::v1::path" to "LPCWSTR" exists
EDIT: Here is a solution that worked for me, by following Lightness solution:
std::string path = "C:\\Programs";
for (const auto & entry : std::experimental::filesystem::recursive_directory_iterator(path))
FtpPutFile(hIConnect, entry.path().wstring().c_str(), entry.path().filename().wstring().c_str(), FTP_TRANSFER_TYPE_BINARY, 0);
LPCWSTR is Microsoft's obfuscation of the const wchar_t* type, and filesystem paths conveniently have a wstring() member function. As you may recall, C++ strings give you access to their character buffer, too, via c_str().
So, entry.path().filename().wstring().c_str() is a LPCWSTR you can use (ugh!). Be careful to use that immediately, or store the result of wstring() somewhere for as long as you need the LPCWSTR to survive, because wstring() returns by value and you don't want a dangling pointer.
// Untested, but a logical adaptation of your code
const std::string path = "C:\\Programs";
std::experimental::filesystem::recursive_directory_iterator it(path);
for (const auto& entry : it)
{
const std::wstring filename = entry.path().filename().wstring();
FtpPutFile(
hIConnect,
filename.c_str(),
filename.c_str(),
FTP_TRANSFER_TYPE_BINARY,
0
);
}
I have c++ code (Includes Qt also), i want to call those functions by using Perl.
Which we can do by using SWIG, so I have implemented interfaces and did all the stuff need to use them in Perl script.
I have a function in c++ which returns a QString value,
QString get_string()
{
return QString("mystring");
}
I have written one more class which will be used in perl script where i have a function which calls this get_string() function and returns const char*.
const char* get_const_string()
{
QString str = get_string();
**//here I print str and str .toLocal8Bit().constData()
//both are printing the text which i shoud get here**
return str.toLocal8Bit().constData();
//here I have tried diff combinations also, as
// return str.toStdString().c_str();
}
The problem is, in get_const_string() function, I could get the string I wanted, but when I call this function in my perl script, I am getting undefine value i.e null string
.
Any idea, what is the problem here ??
I am using perl5, Qt4.8.4
Thanks in advance.
if you cant use a QString return value, maybe you can use std::string.
if both fail and you do not have limitations, you could do some dirty trick:
QString get_string()
{
static QByteArray arr;
QString str = getString();
arr = str.toLocal8Bit();
return arr.constData();
}
note that the arr variable will not be free'd untill your app is running
edit: found a possible solution to just use std::string ... string arguments are not recognized by SWIG
void GenerateDecryptedData("/home/merve/merve.enc", "/home/merve/merve.dec","dEneMe!1234");
I want to call my function like dEneMe!1234.
void GenerateDecryptedData(const char* pathToEncryptedFile,
const char* pathToDeccryptedFile,
std::string Pwd);
But when I wrote the function prototype like this, I'm taking string has not been declared- error! How can I take my password in string type?
You need the string header at the top of your source file: #include <string>. I'd consider Googling error messages more often. :)
string header - http://www.cplusplus.com/reference/string/
cstring header - http://www.cplusplus.com/reference/cstring/
What's wrong with:
void GenerateDecryptedData(const char* pathToEncryptedFile, const char* pathToDeccryptedFile, const char* Pwd);
I think my problem is simple to solve, but due to my lack of knowledge, I can't find the answer.
I have this structure :
struct variableOutil {
std::string Nom;
float Valeur;
std::string Module;
std::string Unite;
std::string Format;
std::string Accesibilite;
std::string Description ;
};
And the problem come from here :
struct variableOutil ToleranceTensionAvion;
ToleranceTensionAvion.Nom = "ToleranceTensionAvion";
QTableWidgetItem* newItem = new QTableWidgetItem();
newItem->setText(ToleranceTensionAvion.Nom);
this->ui->variableTableWidget->setItem(1,0,newItem);
I've got this error :
No matching function for call to 'QTableWidgetItem::setText(std::string&)
The problem is that setText need a const QString &text and tell me that I put in parameter a std::string& , I don't understand why does the type don't match, and what's the difference, after all, this is a simple String.
Thank you.
You should convert std::string to QString by :
newItem->setText(QString::fromStdString(ToleranceTensionAvion.Nom));
Or
newItem->setText(QString::fromUtf8(ToleranceTensionAvion.Nom.c_str());
Try:
newItem->setText(QString::fromUtf8(ToleranceTensionAvion.Nom.c_str());
QString allows only Unicode.
std::string just stores the bytes and does not work with encodings. The best way to store your texts would probably be UTF-8 encoding.
They are completely different classes, and std::stringcan not be used in place of QString. You can build QString fromstd::string and pass it.
QString s =QString::fromStdString(ToleranceTensionAvion.Nom);
newItem->setText(s);
just use c._str() on your std::string and it will work for anything that expects a QString