2017-02-18 32 views
0

我一直工作在C++應用程序..我想通了如何從SaveFileDialog搶目錄名和結合起來,與文本的保存一堆文件相同的文件夾,但是當我嘗試將新的FileWithPathName轉換爲LPCTSTR時,代碼最終未分配。如何轉換C++ SaveFileDiolog信息,以LPCTSTR

我已經找遍了這個網站,似乎無法找到我要尋找一個真正明顯的例子。如果有人能指示我一個明確的鏈接,或者告訴我我做錯了什麼,那會很好。 ;-)

  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 
+0

這是C++/CLI,而不是C++。已更改標籤。 –

+0

thx巴特沃斯先生的。我在辯論使用哪一個。 –

+0

https://msdn.microsoft.com/en-us/library/bb384865.aspx –

回答

1

有幾種不同的方法來進行轉換。您可以使用:

#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); 

here

+0

關鍵信息位於include和top使用語句中。現在一切正常。非常感謝你爲這些重要的項目。 –