Any ideas how this could be converted to a std::string().

So files8 will be converted to a std::string()

System::String ^ files8 = folderBrowserDialog1->SelectedPath->ToString();

Recommended Answers

All 3 Replies

This is my problem how to convert in this case: files8 into a LPCSTR.
As I understand from googling around LPCSTR is a const *char but I really cant figure it out where to start.
I would really appreciate codestart help to get on the right track.

System::String ^ files8 = folderBrowserDialog1->SelectedPath->ToString();

http://msdn2.microsoft.com/en-us/library/1b4az623.aspx
http://msdn2.microsoft.com/en-us/library/1b4az623(VS.80).aspx
http://blogs.msdn.com/slippman/archive/2004/06/02/147090.aspx

// .NET Framework 2.0 or higher

LPSTR to_LPSTR( System::String^ s, char buffer[], std::size_t sz  )
{
   using namespace Runtime::InteropServices ;
   void* temp = Marshal::StringToHGlobalAnsi(s).ToPointer() ;
   errno_t result = strcpy_s( buffer, sz, (const char*)temp ) ;
   Marshal::FreeHGlobal( IntPtr( temp ) );
   return result==0 ? buffer : 0 ;
}

System:String^ files8 = folderBrowserDialog1->SelectedPath->ToString();
char buffer[ MAX_PATH ] ;
LPCSTR pcstr = to_LPSTR( files8, buffer, sizeof(buffer) ) ;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.