How do I create a std::filesystem::exists() compatible path from Windows::Storage::StorageFile? [duplicate] - c++-cli

I'm trying to open a file using a string parameter, however I'm getting the following error:
error C2664: 'void std::basic_ifstream<_Elem,_Traits>::open(const wchar_t *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'System::String ^' to 'const wchar_t *'
How do you convert System::String ^ to const wchar_t *?

As Hans points out, simple conversion is necessary. It would look similar to the following:
System::String ^str = L"Blah Blah";
pin_ptr<const wchar_t> convertedValue = PtrToStringChars(str); // <-- #include <vcclr.h>
const wchar_t *constValue = convertedValue; // <-- Unnecessary, but to be completely verbose
void std::basic_ifstream<_Elem, _Traits>::open(constValue, mode, i);

Related

i have error its meaning :Error C2440 'return': cannot convert from 'void (__cdecl &)(yield_context)' to 'void (&)(yield_context)'

I am making program in which i try to make boost packaged_task then take its future in vector and launch it with asio post.
when i try to make packaged_task ,it gives me this error:
Error C2440 'return': cannot convert from 'void (__cdecl &)(boost::asio::yield_context)' to 'void (&)(boost::asio::yield_context)'
this is the relevant code:
typedef boost::packaged_task<std::string(boost::asio::yield_context)> task_t;
boost::shared_ptr<task_t> task = boost::make_shared<task_t>(boost::bind(&HTTPRequest::Execute, mClientRequestsVariables[m_HttpClient_request_name]->shared_from_this() , boost::placeholders::_1, m_HttpClient_request_name, Get_mHTTPClient_Responses_Map_shared_pointer()));
boost::future<std::string> fut = (task->get_future());
mPendingData.push_back(std::move(fut)); // C++11 possible: (std::move(fut) when fut is a unique_future);
mIos.post(boost::bind(&task_t::operator(), task));
and this is the definition of HTTPRequst::Execute :
HTTPRequest::HTTPRequest(boost::asio::io_service& ios, unsigned int id, std::string URL, const HTTPServiceResolve& resolve_addr, boost::shared_ptr<LoggingClass_2> mHTTPRequest_LoggingInstance_shared_pointer)
and the class HTTPRequest is derived from enable_shared_from_this .
the error is in bind.hpp so i find in vs output windows clues to the included part of code.
why is this error happening?and what is the solution??

Managed c++ to clr __pin to pin_ptr or interior_ptr

I am trying to compile a project that was previous using /oldsyntax, but now with /clr.
std::string Jhc::Interop::stlString(System::String^ s)
{
std::string out;
const wchar_t __pin * str = PtrToStringChars(s);
int len = s->Length*4;
char *buf = new char[len];
memset(buf,len,0);
I have changed System::String *s to System::String^ s, but how do I covert the line with PtrToStringChars(s);?
I have tried using pin_ptr and interior_ptr, but cannot make it work.

Cannot initialize a variable of type 'void *' with an rvalue of type 'void *const *'

I am trying to partially file a buffer in core-audio for iOS, in order to do this I need to change the start address I pass to memcpy. My code looks like this...
UInt32 bytesToRead = inBuffer->mAudioDataBytesCapacity;
int srcOffset = 0;
while (bytesToRead > 0) {
UInt32 maxBytesFromCurrentPiece = self.currentAudioPiece.audioData.length - self.currentAudioPieceIndex;
//Take the min of what the current piece can provide OR what is needed to be read
UInt32 bytesToReadNow = MIN(maxBytesFromCurrentPiece, bytesToRead);
NSData *subRange = [self.currentAudioPiece.audioData subdataWithRange:NSMakeRange(self.currentAudioPieceIndex, bytesToReadNow)];
//Copy what you can before continuing loop
void *srcStart = (&inBuffer->mAudioData + srcOffset);
memcpy(srcStart, subRange.bytes, subRange.length);
srcOffset += subRange.length;
bytesToRead -= bytesToReadNow;
Appcode shows no error but the compiler shows this:
"Semantic Issue" - Cannot initialize a variable of type 'void *' with
an rvalue of type 'void *const *'
On the line with "*srcStart".
Try changing
void *srcStart = (&inBuffer->mAudioData + srcOffset);
to
char *srcStart = ((char *)inBuffer->mAudioData) + srcOffset;
This will skip the appropriate number of bytes.

MessageBoxW cannot convert

I am using wxWidgets 2.9.4 in Visual Studio 2012 and I keep getting these two error messages:
Error 1 error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char *' to 'LPCWSTR'
IntelliSense: argument of type "const char *" is incompatible with parameter of type "LPCWSTR"
My code is:
#ifdef _WIN32
std::string msg;
StringFromFormatV(&msg, format, args);
retval = IDYES == MessageBox(0, msg.c_str(), "ERROR! Continue?", MB_ICONQUESTION | MB_YESNO);
You are compiling your project using multi-byte characters as default. You can change that in your project's properties, or you can use msg.wc_str(), or even enforce the use of MessageBoxA instead of using the macro MessageBox.

...array<Object^>^ args

I'm reading C++/CLI. I see this stuff:
Object^ CreateInstanceFromTypename(String^ type, ...array<Object^>^ args)
{
if (!type)
throw gcnew ArgumentNullException("type");
Type^ t = Type::GetType(type);
if (!t)
throw gcnew ArgumentException("Invalid type name");
Object^ obj = Activator::CreateInstance(t, args);
return obj;
}
When calling it:
Object^ o = CreateInstanceFromTypename(
"System.Uri, System, Version=2.0.0.0, "
"Culture=neutral, PublicKeyToken=b77a5c561934e089",
"http://www.heege.net"
);
What is ...array^ args? If I remove ... ,there's a complied-error:
error C2665: 'CreateInstanceFromTypeName' : none of the 2 overloads could convert all the argument types
1> .\myFourthCPlus.cpp(12): could be 'System::Object ^CreateInstanceFromTypeName(System::String ^,cli::array<Type> ^)'
1> with
1> [
1> Type=System::Object ^
1> ]
1> while trying to match the argument list '(const char [86], const char [21])'
Like C++, C++/CLI has a mechanism for a variable amount of arguments. That is what the ... in front of the ...array<Object^>^ parameter means.
For type safety the C++/CLI designers added managed syntax to declare the type of the variable array.
Since it's just passing that parameter to the Activator::CreateInstance() function, I would look at what variable parameters the Activator function is looking for.