In this module we will see more tasks need to be done in order to fully updating your old, Managed Extension for C++ code to the new C++ .Net code. All the new keywords, operators etc. used in the new C++ .Net are discussed here. The code snippets used in this module if any, are the mix of Visual C++ .Net 2003 and 2005. If the old code used on VC++ 2005, compile with the /clr:OldSyntax option. The following are the topics in this module.
Managed Extensions for C++ Upgrade To new C++ Syntax Checklist
The Visual C++ 2005 includes new features for targeting virtual machines with garbage collection, such as the .NET Framework. In a future release, support for Managed Extensions for C++ will be removed (dominant in Visual C++ .Net 2003). C++/CLI represents a dynamic component programming paradigm extension to the ISO-C++ standard language. Code using Managed Extensions for C++ can still be compiled with /clr:oldSyntax. This topic lists the syntactic differences between Managed Extensions for C++ and the new C++ syntax.
To Update Managed Extensions for C++ Code
Check and do the following tasks for updating the Managed Extension for C++ code to new C++ code.
Remove #using <mscorlib.dll>. (This assembly is now referenced by default).
Replace __gc class with ref class.
Replace __gc struct with ref struct.
Replace __value class with value class.
Replace __value struct with value struct.
Remove default constructors from value classes. (Default constructors for value types are allowed in Managed Extensions for C++, but the nature of the CLR prevents them from being called reliably, so this feature has been removed).
Replace __gc __interface with interface class.
Replace __abstract with abstract, and move it after the class name.
Replace __sealed with sealed, and move it after the class name.
Replace __property with interface class, combine the get and set methods into a single property block, and remove the trailing underbar/name suffix from these accessors. For simple properties, the accessors need not be defined explicitly. The syntax for indexed properties requires bracket pairs after the property type.
Replace __event with event.
Replace __value enum with enum class.
Replace pointers to reference types followed (defined with __gc*) with ^.
Replace pointers to values types that potentially live in the garbage collected heap with interior_ptr.
Replace pointers declared with __pin with instances of pin_ptr, unless a whole object is being pinned, in which case use interior_ptr, and then use pin_ptr on the interior pointer. For Visual C++ 2005, this template is defined in the cli::language Namespace.
Replace new with gcnew.
Replace 0 or null with nullptr when used to indicate that a pointer or handle into the garbage collected heap does not indicate an object and when using comparisons to check pointers and handles.
Update garbage-collected arrays declared with __gc to use the array type. For Visual C++ 2005, this template is defined in the cli::language Namespace.
Remove all instances of __box (implicit Boxing.)
Remove the "S" prefix from string literals (the compiler now determines if a string is literal based on the context in which it is used).
Replace the explicit declaration and usage of overloaded operators required in Managed Extensions for C++ (such as op_Addition, op_Subtraction) with the traditional C++ syntax. The new syntax reverts to the traditional C++ using the operator keyword.
Update conversion operators (which use op_Implicit and op_Explicit in Managed Extensions for C++ syntax).
Replace __typeof with typeid.
Replace __try_cast with safe_cast. For Visual C++ 2005, this template is defined in the cli::language Namespace.
If you were signing your assembly (also known as giving your assembly a strong name) with the CLR's assembly-signing attributes, you should remove those attributes from your code and instead use the assembly-signing linker options.
/clr (Common Language Runtime Compilation)
This option enables applications and components to use features from the common language runtime (CLR). You can set this through the project property page as shown in the previous modules.
options - One or more of the following, separated by a comma.
/clr - Creates metadata for your application that can be consumed by other CLR applications, and allows your application to consume types and data in the metadata of other CLR components.
/clr:pure - Produces an MSIL-only output file with no native executable code, although it can contain native types compiled to MSIL.
/clr:safe - Produces an MSIL-only (no native executable code) and verifiable output file. /clr:safe enables verification diagnostics (PEVerify Tool -Peverify.exe).
/clr:oldSyntax - Enables Managed Extensions for C++ syntax, the original Visual C++ syntax for CLR programming. Managed Extensions for C++ syntax is deprecated in Microsoft Visual C++ 2005, You should only use /clr:oldSyntax if you are maintaining a Visual C++ application that uses Managed Extensions for C++. If you are developing a new application, use the updated syntax. If you have a Managed Extensions for C++ application, you can begin to port your project to use the new syntax.
/clr:noAssembly - Specifies that an assembly manifest should not be inserted into the output file. By default, noAssembly is not in effect, and an assembly manifest is inserted into the output file. A managed program that does not have an assembly metadata in the manifest is called a module. noAssembly can only be used when a DLL is the output file of the compilation. If you compile with /c (Compile Without Linking) and /clr:noAssembly, then specify the /NOASSEMBLY (Create a MSIL Module) option in the linker phase to create a module. noAssembly is deprecated in Visual C++ 2005. Use /LN (Create MSIL Module) instead. Before Visual C++ 2005, /clr:noAssembly implied /clr. However, /clr now also supports /clr:oldSyntax, so you must specify one /clr form when you specify /clr:noAssembly. For example, /clr:noAssembly /clr creates a module using the new Visual C++ CLR syntax and /clr:noAssembly, oldSyntax creates a module using Managed Extensions for C++. Before Visual C++ 2005, /clr:noAssembly required /LD. /LD is now implied with you specify /clr:noAssembly.
/clr:initialAppDomain - Allows a Visual C++ application to run on version 1 of the common language runtime. An application compiled with initialAppDomain should never be used by an application using ASP.NET. Upgrade to a newer runtime to do ASP.NET work with C++.
Managed code is code that can be inspected and managed by the common language runtime. Managed code can access managed objects. An application compiled with /clr may or may not contain managed data. Only CLR types will be instantiated on the garbage-collected heap. To compile a function to native code, use the unmanaged pragma. By default, /clr is not in effect. When /clr is in effect, /MD is also in effect. /MD ensures that the dynamically linked, multithreaded versions of the runtime routines are selected from the standard header (.h) files. Multithreading is necessary for managed programming in part because the CLR garbage collector runs finalizers in an auxiliary thread. If you compile with /c, you can specify the CLR type (IJW, safe, or pure) of the resulting output file with /CLRIMAGETYPE (Specify Type of CLR Image). /clr implies /EHa, and no other /EH options are allowed with /clr. All modules passed to a given invocation of the linker must have been compiled with the same run-time library compiler option (/MD or /LD).
Use the /ASSEMBLYRESOURCE (Embed a Managed Resource) linker option to embed a resource in an assembly. /DELAYSIGN (Partially Sign an Assembly), /KEYCONTAINER (Specify a Key Container to Sign an Assembly), and /KEYFILE (Specify Key or Key Pair to Sign an Assembly) linker options also let you customize how an assembly is created. When /clr is used, the _MANAGED symbol is defined to be 1. The global variables in a native object files will be initialized first (during DllMain() if the executable is a DLL), and then the global variables in the managed section will be initialized (before any managed code is run). #pragma init_seg only affects the order of initialization within the managed and unmanaged categories.