반응형

Convert String^ to const char*

//String^을 const wchar_t*
#include " vcclr.h"
using namespace System;
using namespace System::Runtime::InteropServices;

//String^ 을 const char*
  String^ fileName = "name";
  const char* filePath = (const char*)(Marshal::StringToHGlobalAnsi(fileName)).ToPointer();
  Marshal::FreeHGlobal((IntPtr)(char*)filePath); // 반드시 호출해 줘야함. 안하면 memory leak 

Convert const char* to String^

using namespace System;
using namespace System::Runtime::InteropServices;

String^ ConvertCharPtrToString(const char* text)
{
    String^ resultStr =  Marshal::PtrToStringAnsi((IntPtr)(char*)text);
    return resultStr;
}

Convert strint to STring^

#include <windows.h>
#include <string>
using namespace System;
using namespace System::Runtime::InteropServices;

String^ ConvertstringToStringCli(std::string text)
{
    String^ resultStr = gcnew String(text.c_str());
    return resultStr;
}

'Programing > C++|CLi' 카테고리의 다른 글

C++ Managed , Exception Catch & Handling  (0) 2022.08.23
C++/CLI 에서 Lock 거는 법  (0) 2019.12.03

+ Recent posts