【问题标题】:System::String^ to TCHAR*System::String^ 到 TCHAR*
【发布时间】:2014-05-03 13:59:17
【问题描述】:

我有一个类,它收集给定文件夹的 .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);
    }
}

【问题讨论】:

  • 只要在任何地方使用 wstring。所有可以运行现代 .NET 版本的 Windows 版本都有 unicode API。
  • Ben 所说的几乎可以肯定是you don't need TCHAR

标签: c++-cli clr tchar


【解决方案1】:

marsha_as "对特定数据对象执行封送处理以将其在托管数据类型和本机数据类型之间转换"。 Here 有可能的类型转换表。

我是这样用的:

marshal_as<std::wstring>(value)

TCHAR 可以是 char 或 wchar_t,它们都存在于 marshal_as 特化中,我想你需要将 TCHAR* 作为模板参数:

TCHAR* result = marshal_as<TCHAR*>(value)

其实MSDN说得这样用:

#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_as。您的问题是如何将 String^ 转换为 TCHAR*?
  • 我得到了它的工作,将编辑解决方案的主要帖子,谢谢!
猜你喜欢
  • 1970-01-01
  • 2010-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多