Can a C/C++ program be compiled into a .NET portable class library using C++/CLI - c++-cli

I've read the documentation on .NET programming in C++/CLI, and understand at a high level that it can compile C or C++ to .NET MSIL. I also understand that native structures are translated in the process.
The question is, can I compile a C/C++ codebase into a .NET Portable Class Library using C++/CLI? The intention is to use the result across various platforms, e.g. the Xamarin platforms and UWP.
Edit: Is it easier to do this for plain C, rather than C++?

Short answer: AFAIK no.
Long answer:
As far as I know the C++/CLI source code is compiled in "mixed" mode. It means that if you learn C++/CLI language and create managed classes with it, they run in .NET natively. That's good. But if you simply take your existing C++ code and compile it, the result is the native x86/Windows code, which cannot be used on other platforms. It is called "mixed" because the compiler puts native and .NET IL code together to single executable file.
C++/CLI is usually used in situations where you want to use the existing C++ code
as a part of .NET program in Windows. So you create a library in C++/CLI and create an interface for it in managed C++/CLI. This managed C++/CLI interface is a bridge between native C++ code and the rest of your program in .NET.
Also, as far as I know, C++/CLI is generally not supported by CoreCLR.

Related

Distinction between programming language and framework

I'm a student and started developing some projects in VB.Net and I'm liking it a lot. However, I asked this question to some colleagues and they couldn't answer. Even my teacher wasn't able to clarify what was exactly VB and .Net. I have checked this question here but even the accepted answer gives me some doubts.
For example, for this bit of code here
Class Example
Private _value As Integer
Public Sub New()
_value = 2
End Sub
Public Function Value() As Integer
Return _value * 2
End Function
End Class
Module Module1
Sub Main()
Dim x As Example = New Example()
Console.WriteLine(x.Value())
End Sub
End Module
How can the language be separated from the framework? I know the language is syntax and all but, where is the framework?
I've read that frameworks contain libraries: what are exactly libraries? The language (syntax and all) I can see it, it can be seen, but is the framework visible too? If so, where?
So in a VB.Net application, when I look at the code, all I can see is VB: the same goes for C#.Net applications.
Maybe silly example: For instance, for a car to function, all components must be good - from the tires to the hood, the engine, the fuel - but all of them are visible, and while from the outside we can't see the oil or fuel running inside or the pistons working, we can see them if we want.
Is it possible to see all the components when looking at a VB/C# .Net application? I mean, to look at code and clearly distinguish what is VB or C# and .Net?
Thanks
There are three primary parts to the .NET framework:
Compiler (MSBuild)
Runtime environment (CLR) - virtual machine, garbage collector, etc.
Supporting libraries (FCL)- System, ADO.NET, LINQ, WPF, WCF, etc.
All three of the above are included with the freely-distributed .NET Framework. The term .NET Framework refers collectively, and loosely, to all of those things, but they each perform very different roles. The answer to your question is different for each of those parts.
Compiler
As you probably know, you cannot run VB.NET code directly. It must first be compiled into an executable which can be launched as a Windows process. The compiler is used to compile a variety of high level languages (such as VB.NET and C#) into assemblies (e.g. EXEs, DLLs). Unlike native compilers, though, .NET assemblies do not contain native machine code. Instead, the .NET compiler compiles into .NET assemblies which contain MSIL code. MSIL is a slightly-higher-level-than-machine-code language which, in theory, can be run by a virtual machine on any platform.
The compiler is obviously not visible in your code. Obviously there is no place in your code where you can say "that's the compiler". Rather, the compiler is the tool that you use to compile all of your code. The compiler, though, does dictate what languages you can use. If the .NET framework does not contain a Ruby compiler, then you can't very well write a .NET application in Ruby. So, in that way, the fact that your code is written in VB.NET or C# at all is the indication that it will be compiled using the .NET Framework.
You may be thinking, "The compiler isn't part of the framework--that's part of Visual Studio." But if you were thinking that, you would be wrong. The compiler is installed as part of the .NET framework. You don't technically need Visual Studio to compile a .NET application. You could write an application in any .NET language, using Notepad, and then compile and run it with the .NET Framework without ever having installed Visual Studio.
The compiler portion of the .NET Framework is referred to as MSBuild.
Runtime Environment
Since the compiler compiles your high-level code into MSIL assemblies, rather than into native machine-code binaries, you must have the .NET Framework installed in order to run any .NET application. When you run a .NET application, the Framework is responsible for just-in-time (JIT) compiling the MSIL code into machine code so that it can be executed on your current platform. It also provides the memory management features such as garbage collection.
The same runtime environment is shared by all .NET languages (since all .NET languages compile to MSIL code). Therefore, it's not a VB.NET runtime environment nor a C# runtime environment, but rather, it's a MSIL runtime environment. Instead of having one runtime environment per language, there is actually one runtime environment per platform. A separate environment must be custom implemented for each processor architecture and each operating system on which the Framework will run.
This part of the framework is largely invisible to you and your code. There are some methods you can call to access the GAC, or the garbage collector, or the .NET security system, but other than that, you don't even know it's there. Unless the Framework isn't installed, that is--in that case, it becomes clear very quickly just how necessary it is.
The runtime-environment portion of the .NET Framework is referred to as the Common Language Runtime (CLR).
Supporting Libraries
The .NET Framework comes packed with many extraordinarily useful libraries (DLLs). For instance, every application references the mscorlib.dll and System.dll libraries, which provide many of the core data types in the System namespace. There are many other libraries which you can optionally reference with your projects as necessary.
These supporting libraries are provided as already-compiled MSIL assemblies, so they can all be referenced and used by projects written in any .NET supported language. When you call Console.WriteLine("Hello World") in VB.NET and Console.WriteLine("Hello World"); in C#, you are calling the exact same .NET Framework library. The Console class is defined in the mscorlib.dll library. It doesn't matter which language you use to call it, it always does the same thing.
The supporting libraries are very visible in your code. Any time you use anything that is not explicitly defined by you in your own code, you are using the Framework (unless, of course, it's something from a third-party library). In your example code above, you are using Integer and Console. Both of those types are defined by the .NET Framework's supporting libraries (both are defined by mscorlib.dll). In other words, the language is all of the syntax that you use to make the calls to the Framework. Everything else is the Framework.
If you want to "see" the supporting libraries, the best place to start is in Visual Studio. You can use the Object Browser to browse through all of the types defined by the libraries that you currently have referenced in your project. If you want to reference more .NET Framework libraries, go to your Project Properties designer's References tab, click the Add... button, then select the Asseblies > Framework option. That will list all of the supporting libraries that are provided as part of the .NET Framework. If you want to find out more about any of the functionality provided by any of those libraries, your best resource is the MSDN (and Stack Overflow, of course).
The supporting libraries portion of the .NET Framework is referred to as the Framework Class Library (FCL).
Here is how I envision the two
The framework is the set of libraries, APIs and runtime components that are available for the language to use
The language is the syntax by which the framework is accessed
In many cases it's hard to separate the two because the language when used is nearly always accessing some part of the framework. Take for instance even this very simple definition
Class Example
End Class
Even though it is unstated, this definition depends on the targeted framework having a definition of System.Object. This is necessary for the language to setup the appropriate base class for Example.
You have to keep in mind that if you're using a programming language, at some point it all must boil down to binary 1's and 0's.
This means that when your program gets compiled and run, it's making use of the C# compiler. The C# compiler is a program written in a native language (probably c or c++). This program allows yours to be converted into stuff that the .net framework runtime can understand. Essentially, your C# code becomes some sort of intermediate bytecode.
Similarly, after your program is compiled into this intermediate bytecode, it is run on top of a virtual machine (JVM for Java or the .net framework runtime for C#). This virtual machine is a pre-compiled program written in a native language (c or c++), which allows your program to interface with the computer's processor in a way that both understand.
All of these pre-written, pre-compiled pieces are part of a framework. They are installed to your machine in some particular location (which is why they might not be readily visible with the rest of your code and assets) and provide APIs to anything that wants to make use of them.
The APIs are analogous to the understanding between a gas pedal and an engine. An engine accepts more fuel as a result of someone pressing the gas pedal. In some sense, the engine "talks" to the gas pedal and understands that if you press firmly on the pedal, the engine should respond by injecting more gasoline into the combustion chambers.
These individual pieces, along with their "understandings" represent a framework. You might add a "Driver" who can operate a gas pedal as your "C# Application".
How can the language be separated from the framework?
For example... take for instance English, English is a language, while a book would be a Framework. In this example, you can write whatever you want in English, but you can't write in "The Hobbit", because it is a book, not a language.
However, you can make references to the book. For instance you can talk about Trolls, and you don't have to define it's behavior or looks, because the book already defines them for you.
Where is the framework?
The framework exists whenever you make reference to it. In the Trolls example, if you ask "Do you know how do Trolls look like?", I would need a context for that, it may be Harry Potter's, Tolkien's, World of Warcraft's, etc. And I will give you compilation errors because I can't understand you.
If however we "use" the same "Framework" (book), or if you tell me about them before, I would be able to respond you (compile).
Is the framework visible too? If so, where?
VB/C# are languages, you can't "really see" where it is, because you write it. It is like asking where is English. You can however compile it, and that is what you can "find", just like you can find a printer.
The Framework is a set of libraries/utilities already packed into a beautiful black box that you download and trust your life to.
VisualStudio helps you a lot, and it will hide "complicated stuff", imagine like it is a movie called The Lord of The Rings, and it removes a lot of stuff that you can't really look into unless you read the book.
You can't really dive into "what .NET does internally", because it is closed software. Imagine that I burn down all the books and you have to trust the movie.
But you could use an Open Source example such as Struts (a JavaEE Framework) because it is open source (books should not be burned in Open Source examples).
Here is an abstract example.
Speech is similar to a framework, you have phonation, producing sound;
resonance; intonation, variance of pitch, etc. Those are all the building blocks for communicating.
To speak a specific language, you must utilize phonology, morphology, language syntax, proper grammar; semantics, etc - all the rules for that Language.
...
In a programming framework, you have building blocks (code libraries, dependent files, specific folder/file layout, ect.) that you might need to complete a programming task.
A programming language is the set of rules you must abide to in order for your application to correctly work.
....
In the Java programming language, I could open notepad and write java code, compile it, and run it on any machine that has the JVM installed.
Java Server Faces (JSF) is a framework where a Model-View-Controller (MVC) paradigm exists as the basis to run web applications. You have necessary dependent files (like web.xml) that must exist for the server to correctly implement, structured layout of classfiles, and implementation/configuration of other libraries. In an essence, your files are still written in the Java Programming language and your syntax must be correct, but if you want to build a web application using that framework, you must have adhere to those other specific patterns and constraints.

Can C++-CLI dll be used in native application?

I am writing a dll in native code (C++) that is to be consumed by other native C++ applications.
One of the tasks of the dll is to perform XML file operations. C++ has poor support in dealing with XML files, e.g searching, writing, reading, as far as I know.
So, I am thinking if I can compile my dll code in clr:mixed mode and use the .NET XML assembly to do the XML related stuff. However, I still want my dll to be consumed by native C++ applications without any added hassles.
Will this approach work ? Any caveats to this ? Btw, is there any other XML library for C++ besides XMLlite ?

SSCLI and VB.NET

I am interested to learn more about how the .NET framework works internally. I have downloaded Reflector and also SSCLI. The following link says: "The Shared Source CLI is a compressed archive of the source code to a working implementation of the ECMA CLI and the ECMA C# language specification": http://www.microsoft.com/en-gb/download/details.aspx?id=4917.
Does the SSCLI not include the source code for the implementation of VB.NET i.e. only C sharp?
I realise that the VB.NET specification can be downloaded here: http://www.microsoft.com/en-us/download/details.aspx?id=15751
The SSCLI distribution was intended to help anybody to port a compliant implementation of the CLR to another platform. Most of it is C++ source code, the language used to implement the CLR, just-in-time compiler and the C# and JScript compilers. The .NET framework libraries were written in C#, that's included as well. They are a pretty decent snapshot of the source code used in .NET 2.0, albeit that it is hard to tell what might have been removed or substituted. I've never run into an obvious mismatch, but it gets less and less obvious with this source code aging and departing more and more from current .NET releases.
But no, there is no VB.NET compiler included, nor the source code for the classes in the Microsoft.VisualBasic namespace. Nor is there a C++/CLI compiler. The included C# and JScript compilers are also not a complete implementation of the Microsoft version of those compilers, the Windows and VS specific bits were removed. Particularly the C# compiler source code is dated, a lot of work was done on it since.

firemonkey + xcode, mixed code

Using Delphi XE2 we can export sources into xcode and compile them with free pascal compiller. But all examples i'v ever seen dont modify .pas files in xcode, just export and run.
I wonder if I could add any delphi units or libraries into Objective C application and compile them together? How to include .pas unit into .xcodeproj?
I wish to port delphi application to iOS, exporting all project is impossible, it seems a good idea to make a GUI for iOS in native ObjectiveC and import logic and methods from delphi units. Is it possible?
Since Delphi XE2 is using the Free Pascal Compiler when targeting iOS, you can use any programming technique of FPC.
In fact, if you do not need FireMonkey, and prefer using native User Interface, you do not need Delphi XE2 at all!
You can use the free FPC to compile your application from XCode.
The best reference web site is http://web.me.com/macpgmr/ObjP
In particular, "Developing with Objective Pascal" articles is exactly what you are looking for:
Develop the UI within XCode;
Use FPC to code your business logic in Pascal;
For both Max OSX and iOS.
See just the introduction page, and I suspect you'll be amazed, like I was.
From my POV, the "Object Pascal" paradigm used with FPC (i.e. object pascal "dialect", which can coexist with the default object pascal code) is more advanced, lightweight and integrated than the interface-based "plumbing" of Delphi XE2 compiler (with on-the-fly marshalling using RTTI). This is perhaps a matter of taste...

Unmanaged and Native terms

Does "native" and "unmanaged" code refer to the same concept! or they are different!
Is there an unmanaged code which is not native!
There are three modes the C++/CLI compiler can operate in:
Compiling standard C++ "native types" to machine code (unmanaged)
Compiling standard C++ "native types" to MSIL (managed)
Compiling managed types to MSIL (managed)
As you can see, it is possible for standard ISO C++ code to be compiled and executed in the managed environment.
Unmanaged code refers to native code. It means that it's not managed by the .NET Framework.