MERLIN
MERLIN

Reputation: 436

How to convert C++ SaveFileDiolog info to LPCTSTR

I've been working on a c++ app.. and I figured out how to grab the directory name from a SaveFileDialog and combine that with text for a saving a bunch of files to the same folder, but the code ends up unassigned when I try to convert my new FileWithPathName to an LPCTSTR.

I have searched all over this site and can't seem to find a really clear example of what I am looking for. If someone can direct me to a link that is clear on this or tell me what am I doing wrong that would be great. ;-)

            FileInfo^ fi = gcnew FileInfo(saveFileDialog1->FileName);

            String^ fileNameWithPath = gcnew String(fi->DirectoryName) + "newName.txt";

            //LPCWSTR lfileNameWithPath = (LPCWSTR)(pfileNameWithPath[0]);  // get temporary LPSTR // fails to get initialized
            //LPCTSTR lfileNameWithPath = (LPCTSTR)(Marshal::StringToHGlobalAnsi(fileNameWithPath)).ToPointer(); // data returned like Chinese characters. epic fail

Upvotes: 0

Views: 46

Answers (1)

mpiatek
mpiatek

Reputation: 1343

There are couple of different methods to make that conversion. You can use:

#include <msclr/marshal.h>
using namespace msclr::interop;
using namespace System;

String^ fileNameWithPath = gcnew String(fi->DirectoryName) + "newName.txt";

marshal_context context;
LPCTSTR lfileNameWithPath = context.marshal_as<LPCTSTR>(fileNameWithPath);

more here

Upvotes: 1

Related Questions