Author Topic: C++/CLR syntax  (Read 4367 times)

0 Members and 1 Guest are viewing this topic.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8718
  • AKA Daniel
C++/CLR syntax
« on: January 04, 2007, 12:21:45 AM »
Has anyone looked at the new C++/CLR syntax that VC2005 uses? It’s actually pretty cool. I never even considered using C++ with .NET. I was always scared of all those macros  like __gc. But after playing around with it, I found it quite pleasant. It seems C++ might just be the language of choice for people who want to just stick with one language and program using managed and/or unmanaged code.

Here are some examples of the new C++/CLR syntax
Code: [Select]
C++/CLI ^  is kind of a managed *
It’s a handle to an object on the managed heap

% is kind of a managed &
It’s a tracking reference to an object on the managed heap
 
resbuf
C#                   ResultBuffer resbuf = new ResultBuffer();
C++/CLI              ResultBuffer^ resbuf = gcnew ResultBuffer();

array
C#                    TypedValue[] value = args.AsArray();
C++/CLI               array<TypedValue>^ TV(args->AsArray());

cast
C#                    (String)object;
C++/CLI               (String^)object;//I like
C++/CLI               safe_cast<System::String^>(object); //MS likes

C#                  List<TypedValue> lisplist = new List<TypedValue>(args.AsArray());
C++/CLI          List<TypedValue>^ lisplist = gcnew List<TypedValue>((IEnumerable<TypedValue>^) args->AsArray());



Here’s a link to the language specification just incase someone wants to explore this further http://download.microsoft.com/download/9/9/c/99c65bcd-ac66-482e-8dc1-0e14cd1670cd/C++-CLI%20Standard.pdf
« Last Edit: January 09, 2007, 12:47:09 AM by Danielm103 »

Chuck Gabriel

  • Guest
Re: C++/CLR syntax
« Reply #1 on: January 04, 2007, 11:24:03 AM »
Thanks Daniel.

I sure hope you continue to visit after the move.  You help keep me thinking about new things.

TonyT

  • Guest
Re: C++/CLR syntax
« Reply #2 on: March 12, 2007, 07:26:38 AM »
Hi Dan.

I've actually been doing a lot of C++/CLI programming with
AutoCAD development. For most other types of development,
C# is preferred, unless you must call native C++ apis, as is
the case with ObjectARX.

One of the coolest things about C++/CLI is something called
"implicit P/Invoke", where calling native code requires only
the .h file with the native function/class declarations, and
requires no [DllImport] declarations, and so you can easily
use native C++ classes, from managed C++/CLI, in an almost
completely transparent way.

For AutoCAD development, C++/CLI is actually a better
choice than C# given sufficient skills, because it makes
accessing the entire native C++ ObjectARX API easy.

For example, to deal with the problem the managed
SymbolTable class has with the indexer returning erased
entries, I wrote this in C++/CLI, which lets me call the
underlying AcDbSymbolTableRecord directly from managed
code:

Code: [Select]

// Returns ObjectId.Null if non-erased record is not found.

static ObjectId^ GetSymbolTableRecordId(SymbolTable^ table, String^ name)
{
   AcDbSymbolTable* tbl = dynamic_cast<AcDbSymbolTable*>(table->UnmanagedObject.ToPointer());
   if( tbl == NULL )
      throw gcnew InvalidOperationException("Invalid SymbolTable object");
   if( String::IsNullOrEmpty(name) )
      throw gcnew System::ArgumentException("Invalid SymbolTable name");
   tbl->assertReadEnabled();
   AcDbObjectId id;
   id.setNull();
   Acad::ErrorStatus es = tbl->getAt(StringToWchar(name), id, false);
   if( es == Acad::eOk )
      return ObjectId(id.asOldId());
   else
      return ObjectId();  // not found or erased
}


Has anyone looked at the new C++/CLR syntax that VC2005 uses? It’s actually pretty cool. I never even considered using C++ with .NET. I was always scared of all those macros  like __gc. But after playing around with it, I found it quite pleasant. It seems C++ might just be the language of choice for people who want to just stick with one language and program using managed and/or unmanaged code.


« Last Edit: March 12, 2007, 07:41:30 AM by TonyT »