Click here to Skip to main content
15,843,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
void CMFCwithCSharpDlg::OnBnClickedBtnAdd()
	CSharp::CSharpMath^ mathClass = gcnew CSharp::CSharpMath();
	int result = mathClass->Add(2, 6);

	char buf[200];
	sprintf_s(buf, "result: %d", result);
	CStringA cstr1("Hello");

	CString str = theApp.m_lpCmdLine;
	std::wstring ws(str);
	std::string s;

	s.assign(ws.begin(), ws.end());
	//MessageBoxA(GetSafeHwnd(), buf, "Message", MB_OK);


What I have tried:

I tried to use string but in c++ there is std::string and in c# System::string, so I don't know how I can do
Updated 30-Nov-23 8:32am
CPallini 30-Nov-23 9:51am    
And... Where is the C# System::String?
Member 14594285 30-Nov-23 10:00am    
In fact I dont know how I can pass to std::string to System string to pass variable to PrintLabelAction

To convert a 'std::string' to a C# 'System::String' in C++, you can use 'msclr::interop::marshal_as' This header gives you utilities to use for converting between managed and native types, more on this here -
Managed C++ to C# Conversion

#include <msclr/marshal_cppstd.h>

//Using your std::string named 's' that you want to convert...
std::string s = "YourStandardStringHere";

//Convert your std::string to System::String...
System::String^ csString = msclr::interop::marshal_as<System::String^>(s);
Share this answer
.NET string type constructor accepts the raw character array as an arguments.
So, along with marshal_as you can do it next way:
std::string std_string = "Some text";
String^ s = gcnew String(std_string.c_str());

Share this answer
Since you are using C++/CLI then you should also use String Class (System) | Microsoft Learn[^].
Share this answer
Member 14594285 30-Nov-23 10:17am    
but I can use System only in c#..and for c++?
Richard MacCutchan 30-Nov-23 10:18am    
You are using gcnew which is part of .NET, so you must have access to all its other features.
Member 14594285 30-Nov-23 10:22am    
I have error if I write using System..I don't know why
Richard MacCutchan 30-Nov-23 10:43am    
Sorry, but we cannot guess what you are doing, or what happens. You need to provide a proper detailed explanation of your problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900