///////////////////////////////////////////////////////////////////// // ManagedApp.cpp - Demonstrates use of .Net and std libraries // // in managed C++ code // // // // Jim Fawcett, CSE775 - Distributed Objects, Spring 2003 // ///////////////////////////////////////////////////////////////////// // Built with Managed C++ Application wizard // ///////////////////////////////////////////////////////////////////// #include "stdafx.h" //#using // don't need this with C++/CLI #include using namespace System; using namespace System::Text; #include #include using namespace std; //----< convert from System::String^ to std::wstring* >-------------- // // user must eventually call delete on returned pointer // std::wstring* convert_SysStrToStdStrPtr(System::String^ str) { std::wstring* stdStr = new std::wstring(); for(int i=0; iLength; ++i) stdStr->append(1,str[i]); return stdStr; } //----< convert from System::String^ to std::wstring >--------------- std::wstring convert_SysStrToStdStr(System::String^ str) { std::wstring stdStr; for(int i=0; iLength; ++i) stdStr.append(1,str[i]); return stdStr; } //----< convert from std::wstring* to System::String* >-------------- System::String^ convert_StdStrToSysStr(std::wstring str) { System::String^ sysStr = gcnew String(str.c_str()); return sysStr; } //----< display a message on console >------------------------------- void msg(const std::string& s) { cout << "\n " << s; std::string underline(s.size()+2,'-'); cout << "\n " << underline << endl; } // //----< using .Net and std libraries >------------------------------- int _tmain(void) { cout << "\n Managed C++ Using both .Net and std Libraries " << "\n ===============================================" << endl; //////////////////////////////////////////////// msg("managed string lives on managed heap"); //////////////////////////////////////////////// String^ sysStr = gcnew String(L"\n Hello World"); Console::WriteLine(sysStr); ////////////////////////////////////////// msg("converting to unmanaged string"); ////////////////////////////////////////// wstring stdStr = convert_SysStrToStdStr(sysStr); wcout << stdStr.c_str() << endl; //////////////////////////////////////////////////// msg("unmanaged string lives on unmanaged heap"); //////////////////////////////////////////////////// wstring* anotherStdStr = new wstring(L"\n hello again"); wcout << anotherStdStr->c_str() << endl; //////////////////////////////////////////////////////// msg("converting from unmanaged to managed strings"); //////////////////////////////////////////////////////// String^ anotherSysStr = convert_StdStrToSysStr(*anotherStdStr); Console::Write("{0}\n",anotherSysStr); delete anotherStdStr; //////////////////////////////////////////////////////// msg("as always std::strings can live on the stack"); //////////////////////////////////////////////////////// wstring stkStdStr = L"a stack-based std::string"; wcout << L"\n " << stkStdStr << L"\n" << endl; // Managed Strings must live on the managed heap // String stkSysStr = *new String("this won't work"); // ///////////////////////////////////////////////////////////////////////////// msg("value types reside on the stack and primitives need no conversion"); ///////////////////////////////////////////////////////////////////////////// int three = 3; double pi = 3.1415927; Console::Write( "\n integer: {0}, and double: {1}\n",three.ToString(),pi.ToString() ); wcout.precision(8); wcout << L"\n integer: " << three << L", and double: " << pi << L"\n" << endl; /////////////////////////////////////////////////////////////// msg("managed array of managed type lives on managed heap"); /////////////////////////////////////////////////////////////// array^ arr = gcnew array(5); for(Int32 i=0; iLength; ++i) // fill it arr[i] = i*i; Console::Write("\n "); for(int i=0; iLength; ++i) // show it Console::Write("{0} ",arr[i].ToString()); Console::Write("\n"); /////////////////////////////////////////////////////////// msg("managed array of std type lives on managed heap"); /////////////////////////////////////////////////////////// array^ anotherArray = gcnew array(5); for(int i=0; iLength; ++i) // fill it anotherArray[i] = i*i; cout << "\n "; for(int i=0; iLength; ++i) // show it cout << anotherArray[i] << " "; cout << endl; ////////////////////////////////////////////////////// msg("unmanaged array of std type lives on stack"); ////////////////////////////////////////////////////// int unManagedArray[5]; // fill for(int i=0; i<5; ++i) unManagedArray[i] = i*i; cout << "\n "; for(int i=0; i<5; ++i) // show cout << unManagedArray[i] << " "; cout << "\n" << endl; return 0; }