#include#include int main(int argc, char** argv) { try { // Initialize the ORB CORBA::ORB_var orb = CORBA::ORB_init(argc, argv); // Get the name service object reference CORBA::Object_var nsObj = orb->resolve_initial_references("NameService"); if (!CORBA::is_nil(nsObj)) { std::cout << "Name service object reference is valid\n"; } else { std::cout << "Name service object reference is invalid\n"; } } catch (const CORBA::Exception& e) { std::cerr << "CORBA exception occurred: " << e << '\n'; } return 0; }
#includeThis example creates a MathHelper class that uses ORB_var to hold the ORB reference. It then uses the ORB reference to get a reference to a remote Math object and call its add method. ORB_var automatically manages the lifetime of the ORB reference and cleans it up when the program exits. Package library: omniORB4#include class MathHelper { public: MathHelper(CORBA::ORB_var orb) : m_orb(orb) {} double add(double a, double b) { CORBA::Object_var obj = m_orb->string_to_object("corbaloc::localhost:1060/Math"); Math_var math = Math::_narrow(obj); if (CORBA::is_nil(math)) { throw std::runtime_error("Failed to narrow Math object"); } return math->add(a, b); } private: CORBA::ORB_var m_orb; }; int main(int argc, char** argv) { try { // Initialize the ORB CORBA::ORB_var orb = CORBA::ORB_init(argc, argv); // Create a Math helper object MathHelper mathHelper(orb); // Call the Math object's add method double result = mathHelper.add(3.0, 4.5); std::cout << "3.0 + 4.5 = " << result << '\n'; } catch (const CORBA::Exception& e) { std::cerr << "CORBA exception occurred: " << e << '\n'; } return 0; }