C++ CLI System.String^ to MFC LPCTSTR

后端 未结 2 657
青春惊慌失措
青春惊慌失措 2021-02-14 09:39

How would I convert a System (.net) C++\\CLI String^ into a MFC C++ LPCTSTR string.

It is very easy to get a LPCTSTR

2条回答
  •  不要未来只要你来
    2021-02-14 10:34

    If you have Visual Studio 2008 or above, you should be able to do this using the C++/CLI marshaling library, like so:

    #include 
    
    using namespace System;
    using namespace msclr::interop;
    
    ...
    
    String^ cliString;
    marshal_context context;
    
    LPCTSTR cstr = context.marshal_as(cliString);
    

    More information on marshaling between types on MSDN: Overview of Marshaling in C++

提交回复
热议问题