C++ cli System::String^到TCHAR*

C++ cli System::String^到TCHAR*,c++-cli,clr,tchar,C++ Cli,Clr,Tchar,我有一个类,它收集给定文件夹中.txt文件的所有路径,并将它们存储到一个向量中。我使用的大多数函数都需要使用TCHAR*来获取/设置当前目录等等 该类如下所示: typedef std::basic_string<TCHAR> tstring; class folderManager { private: TCHAR searchTemplate[MAX_PATH]; TCHAR directory[MAX_PATH]; WIN32_F

我有一个类,它收集给定文件夹中.txt文件的所有路径,并将它们存储到一个向量中。我使用的大多数函数都需要使用TCHAR*来获取/设置当前目录等等

该类如下所示:

typedef std::basic_string<TCHAR> tstring;
class folderManager
{
private:
    TCHAR searchTemplate[MAX_PATH]; 
    TCHAR directory[MAX_PATH];          

    WIN32_FIND_DATA ffd;
    HANDLE hFind;        

    vector<tstring> folderCatalog; 
    vector<tstring> fileNames;     

    bool succeeded; 

public:
    // get/set methods and so on...
};
// Changed TCHAR* dir to tstring dir
void folderManager::setDirectory(tstring dir)
{
    HANDLE hFind = NULL;
    succeeded = false;

    folderCatalog.clear();
    fileNames.clear();
    // Added .c_str()
    SetCurrentDirectory(dir.c_str());
    GetCurrentDirectoryW(MAX_PATH, directory);

    TCHAR fullName[MAX_PATH]; 

    StringCchCat(directory, MAX_PATH, L"\\");

    StringCchCopy(searchTemplate, MAX_PATH, directory); 
    StringCchCat(searchTemplate, MAX_PATH, L"*.txt");

    hFind = FindFirstFile(searchTemplate, &ffd);    

    if (GetLastError() == ERROR_FILE_NOT_FOUND) 
    {
        FindClose(hFind);
        return;
    }
    do
    {
        StringCchCopy(fullName, MAX_PATH, directory);
        StringCchCat(fullName, MAX_PATH, ffd.cFileName);

        folderCatalog.push_back(fullName);  
        fileNames.push_back(ffd.cFileName); 
    }
    while (FindNextFile(hFind, &ffd) != 0);

    FindClose(hFind);
    succeeded = true;
}
这就是我需要将System::String^转换为TCHAR*的地方

private: System::Void dienuFolderisToolStripMenuItem_Click(System::Object^
    sender, System::EventArgs^  e)
{
    FolderBrowserDialog^ dialog;
    dialog = gcnew System::Windows::Forms::FolderBrowserDialog;

    System::Windows::Forms::DialogResult result = dialog->ShowDialog();
    if (result == System::Windows::Forms::DialogResult::OK)
    {   
                     // Conversion is now working.          
         tstring path = marshal_as<tstring>(dialog->SelectedPath);
         folder->setDirectory(path);
    }
}
对特定数据对象执行封送处理,以在托管数据类型和本机数据类型之间进行转换。 有一个表,用于可能的类型转换

我这样使用它:

marshal_as<std::wstring>(value)
#include <msclr\marshal.h>

using namespace System;
using namespace msclr::interop;

int main(array<System::String ^> ^args)
{
    System::String^ managedString = gcnew System::String("Hello World!!!");

    marshal_context ^ context = gcnew marshal_context();
    const wchar_t* nativeString = context->marshal_as<const wchar_t*>(managedString);
    //use nativeString
    delete context;

    return 0;
}
TCHAR可以是char或wchar\u t,它们都作为专门化出现在marshal\u中,我想您需要将TCHAR*作为模板参数:

TCHAR* result = marshal_as<TCHAR*>(value)
实际上,MSDN说您必须以这种方式使用它:

marshal_as<std::wstring>(value)
#include <msclr\marshal.h>

using namespace System;
using namespace msclr::interop;

int main(array<System::String ^> ^args)
{
    System::String^ managedString = gcnew System::String("Hello World!!!");

    marshal_context ^ context = gcnew marshal_context();
    const wchar_t* nativeString = context->marshal_as<const wchar_t*>(managedString);
    //use nativeString
    delete context;

    return 0;
}

我已经尝试了您提到的第一种方法,但是我遇到了一个将wstring转换为TCHAR*的问题。第二个选项给了我一个很长的错误,库不支持此转换…std::wstring和TCHAR*都是本机类型,您不需要像本例中那样使用marshal_。你的问题是如何将String^转换为TCHAR*?我已经开始工作了,准备编辑解决方案的主要帖子,谢谢!只要在任何地方使用wstring。所有能够运行现代版.NET的Windows版本都有unicode API。