TheSwamp
Code Red => .NET => Topic started by: It's Alive! on January 04, 2007, 12:21:45 AM
-
Has anyone looked at the new C++/CLR syntax that VC2005 uses? Its 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
C++/CLI ^ is kind of a managed *
Its a handle to an object on the managed heap
% is kind of a managed &
Its 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());
Heres 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
-
Thanks Daniel.
I sure hope you continue to visit after the move. You help keep me thinking about new things.
-
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:
// 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.