在VC ++ 2010中为LPCTSTR的String ^(Windows窗体应用程序) [英] String^ to LPCTSTR in VC++2010 (Windows form application)

查看:192
本文介绍了在VC ++ 2010中为LPCTSTR的String ^(Windows窗体应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将System :: string ^转换为LPCTSTR?
由于我的要求是使用功能 CopyFile 克隆文件,如果我将Fix名称(OldFile.jpg和LatestFile.jpg)赋予其参数(以下代码,则可以正常工作):工作正常

How to convert System::string^ in to LPCTSTR ? As my requirement is to clone a file using function CopyFile, it works fine if i give Fix name (OldFile.jpg and LatestFile.jpg) to its parameters (below Code: Works Fine)

LPCTSTR in_f,out_f;

in_f    =   _T("d:\\Old.jpg");
out_f   =   _T("d:\\Latest.jpg");

CopyFile(in_f,out_f,false);

以上代码将Old.jpeg克隆到Latest.jpg中,但是当我尝试命名时( Latest.jpg)是从某个字符串中出来的,它不会创建文件(以下代码:不起作用

above code clone the Old.jpeg in to a Latest.jpg but when i trying to give name (Latest.jpg) which is coming out from some String it won't create file (below Code: NOT Working)

  String^   Name    =   "Latest";

//------Capture Current Date & Time
    DateTime datetime = DateTime::Now;

//-------Convert Date Timt in to String
    Name    =   Name + String::Format("{0}",datetime); 
    Name    =   Name->Replace('/','-');
    Name    =   Name->Replace(':','-');
    Name    =   Name    +   ".jpg";    

    LPCTSTR in_f,out_f;

    in_f    =   _T("d:\\Old.jpg");
    out_f   =   (LPCTSTR)Name; //Trying to Assign Current Latest file Name With date Time here

    CopyFile(in_f,out_f,false);

问题是CopyFile接受了LPCTSTR类型作为参数,而我给出的是系统类型: string ^,所以建议我如何将System :: string ^转换为LPCTSTR,以便我可以在文件名中添加当前日期时间。
我正在使用VC ++ 2010和Windows窗体应用程序

The Problem is CopyFile Took LPCTSTR type as an argument , where as i am giving a type System::string^, So suggest me how to convert this System::string^ in to LPCTSTR so that i can add the current date time with in the name of my file. I am Using VC++2010 and Windows form Application

推荐答案

标准警告:虽然当然可以用C ++ / CLI编写应用程序的主体,甚至可以使用WinForms用C ++ / CLI编写GUI,但不建议这样做。 C ++ / CLI适用于互操作方案:在C#或其他.Net代码需要与非托管C ++接口的情况下,C ++ / CLI可以在两者之间提供转换。对于初级开发,建议将C#与WinForms或WPF一起使用(如果要管理代码),或者将C ++与MFC一起使用(如果要不受管理)。

Standard warning: While it's certainly possible to write the main body of your application in C++/CLI, or even write the GUI in C++/CLI using WinForms, it is not recommended. C++/CLI is intended for interop scenarios: where C# or other .Net code needs to interface with unmanaged C++, C++/CLI can provide the translation between the two. For primary development, it is recommended to use C# with either WinForms or WPF if you want managed code, or C++ with MFC if you want unmanaged.

我不确定我是否同意Hans的评论,即TCHAR已作废,但将其显式转换为宽字符串并调用 CopyFileW 是一个不错的选择

I'm not sure I agree with Hans's comment that TCHAR is obsolete, but doing an explicit conversion to a wide string and calling CopyFileW is a good option.

另外,一个人可以朝另一个方向发展,从非托管字符串转换为托管字符串,然后使用.Net方法复制文件, File :: Copy(String ^,String ^,Boolean)

Also, one could go the other direction, and convert from unmanaged to managed strings, and use the .Net method to copy files, File::Copy(String^, String^, Boolean).

要转换为 LPCTSTR ,我将使用marshal_as 。因为它是用模板实现的,所以编译器将解析您的 LPCTSTR 调用 LPCSTR LPCWSTR 版本。

To convert to a LPCTSTR, I would use marshal_as. Because it's implemented with templates, the compiler will resolve your LPCTSTR to call either the LPCSTR or LPCWSTR version, as appropriate.

Microsoft没有每个 marshal_as 的模板版本的专用文档页面,但是在C ++中进行封送处理概述页面是一个不错的选择开始。

Microsoft doesn't have dedicated documentation pages for each templated version of marshal_as, but the Overview of Marshaling in C++ page is a good place to start.

我的测试程序:

#include <msclr\marshal.h>

int main(array<System::String^>^ args)
{
    String^ managedStr = "I came from managed land!\r\n";

    // This controls the lifetime of the LPCTSTR that marshal_as returns. 
    // When this goes out of scope, the LPCTSTR will no longer be valid, 
    // so be aware of its lifetime.
    msclr::interop::marshal_context context;

    LPCTSTR unmanagedStr = context.marshal_as<LPCTSTR>(managedStr);

    OutputDebugString(unmanagedStr);

    return 0;
}

结果:

I came from managed land!

这篇关于在VC ++ 2010中为LPCTSTR的String ^(Windows窗体应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆