///////////////////////////////////////////////////////////////////// // CppCLI-client.cpp - C++\CLI using native DLL with C++11 thread // // // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2014 // ///////////////////////////////////////////////////////////////////// /* * C++\CLI code will not compile if it attempts to use a C++11 thread. * You need to isolate the threaded code behind an interface and * object factory in native C++ (don't compile with /CLR option). * * Then package that component as a DLL and have the C++\CLI code make * a reference to the DLL. Now it will compile and run. * * This code shows you how to do that. Compile the C++\CLI code with * the /clr option set in the General Project Properties. Make sure * that it has a reference to the native DLL and includes the DLL Lib file. * * Use the dll protocol shown in Comm.cpp and compile without /CLR option. * Now you can build and run the project. */ #include "../MockChannel/IComm.h" #include using namespace System; using namespace System::Text; String^ convert(const std::string& s) { StringBuilder^ sb = gcnew StringBuilder(); for (char ch : s) sb->Append(static_cast(ch)); return sb->ToString(); } std::string convert(String^ s) { std::string temp; for (int i = 0; i < s->Length; ++i) temp += static_cast(s[i]); return temp; } int main(array ^args) { Console::Write("\n C++\\CLI client using native C++ DLL with C++11 thread"); Console::Write("\n =======================================================\n"); // using mock channel that pretends to send and receive messages IComm* pIComm = IComm::Create(); String^ msg = L"first message"; Console::Write("\n sending {0}", msg); pIComm->PostMessage(convert(msg)); msg = "second message"; Console::Write("\n sending {0}", msg); pIComm->PostMessage(convert(msg)); msg = "third message"; Console::Write("\n sending {0}", msg); pIComm->PostMessage(convert(msg)); msg = "stop"; Console::Write("\n sending {0}", msg); pIComm->PostMessage(convert(msg)); Console::Write("\n receiving {0}", convert(pIComm->GetMessage())); Console::Write("\n receiving {0}", convert(pIComm->GetMessage())); Console::Write("\n receiving {0}", convert(pIComm->GetMessage())); Console::Write("\n receiving {0}", convert(pIComm->GetMessage())); Console::Write("\n\n"); return 0; }