Accessing native libraries?

Oct 16, 2014 at 4:02 PM
I am a little confused with how c# native deals with unmanaged libraries such as c++ and c libs. If I wanted to access unmanaged libraries, would I pinvoke it like I normally would if it was standard managed .net/mono?
Coordinator
Oct 17, 2014 at 9:37 AM
Edited Oct 17, 2014 at 11:01 AM
as C# native compiles native code you do not need to use PInvoke (and it is not supported yet). Here is the example how to redirect call to "native/unmanaged code". (example taken from CoreLib)

1) You need to declare method as you would be in C++/C

For example you want to use C function wprintf.
[MethodImplAttribute(MethodImplOptions.Unmanaged)]
public static extern int wprintf(char[] chars);
2) use it
public static void WriteLine() {
     wprintf(NewLine.ToCharArray());
} 
3) do not forget to include library into compilation when you use g++

it will be converted into LLVM code:

1) declaration
declare i32 @wprintf(...) #0
2) execution
%.r3 = call i32 (...)* @wprintf(i16* %.r2)

have a look at Babylon 3D project how to use Unmanaged method to operate with http://babylon.codeplex.com, open C# project BabylonNativeCs\BabylonGlut file Gl.cs to see more examples

Regards
Alex
Oct 18, 2014 at 2:47 AM
Edited Oct 18, 2014 at 4:03 AM
Wow, you have got to be kidding me. Just want to make sure I have the concept right:
  • -C# native uses standard c# syntax.
  • -Corelib acts as a core library to give core features like mscorelib or similar to a standard library and you can add parts of the .net framework code into it as long as you are adding all depending code on those .net features, an example being items from mscorelib like what is in there. You can also call native libraries directly from the corelib aswell.
  • -Then you compile the C#/IL from the project, your libraries that you created and also your corelib into IR then it gets compiled to a native assembly.
  • -In the C# project you can call native libs, create your own, or compile some .net libs as long as you follow the dependency chain. (if this part is true then this is why I was mentioning a tool that looks at the project and it's .net features, then reflects the .net framework to pull only the bare minimal code by following the dependency chain and grabbing only dependent code so you can compile it natively, if you choose to go down that route.) Not too sure about that now though. I think that is what MS is doing in their implementation. It still might be too much bloat, but maybe give the option. I am thinking a good corelib that is similar to the c++ standard lib that would work cross platform would be ideal, and from their developers can start building native libraries from scratch.
I am just a little confused as the babylon project has a WPF project that contains .net wrappers around DX and GL. Although I am guessing that it is just your original WPF version to compare versions, right?

This project is amazing and is definitely going to be a hit. There is bits of my project I am working on that I wished could be done in C#, such as the trivial parts that aren't so much in c++ like the majority of the front end code. With that said I am working on a dx8-11 to GL translation shims right now for my project, as it deals with cross platform graphics. I think it would be cool if I created an opengl implementation of WPF to go along with c# native. For cross platform UI capabilities.

I will definitely help you get some exposure. You have basically implemented something that MS has been unwilling to do for years, aswell as seemingly uncapable from the sounds of it as of late, and is probably not going to have their c# native for other platforms other than RT for a long time and will probably never going to support it for other OSes hence why they are supporting mono . So you have a huge upperhand.

There is far too many reasons why this project rocks, and why it only makes sense that C# moves in this direction. Native performance, no jit, no buggy mono depends if doing xplat dev. Native c# is a no brainer. You have the simplicity of c# with the power of c++. Mono could never support high performance applications and even .net on windows couldn't and mono is just far too buggy. The way everything is moving, we need a tool that allows for cross platform native development that isn't ridiculously verbose like C++. Like I said this is the only direction c# can go and it is a good one. Vala is the closest thing to c# cross platform that is native and it isn't good enough anywhere else but linux.

C# native allows businesses and devs to seamlessly switch over to native development, and the fact that it isn't tied to microsoft, is open source, isn't jitted and isn't tied to Miguel should keep the linux powers happy.

Might I suggest you move the project over to github?

I am definitely going to help out with the opengl implementation of wpf once I get to that point in my project. I think it would be cool to create a cross platform IDE written in c# native not far down the road. Maybe a VS extension in the meantime. They can be a huge pain though.
Coordinator
Oct 18, 2014 at 3:24 PM
Edited Oct 18, 2014 at 3:24 PM
You got everything right. Ideally you use any language which genetayes MSIL code, you just need to reference CoreLib. (I have tried F#, but is uses additional DLL for its functionallity so I have just concenrtaed on C# generated code).

regarding Babylon WPF: yes, it is right, I was using WPF project to build working app, then I used C# native to compile library Babylon 3D and additionally added BabylonGlut just to be able to execute OpenGL stuff.

I was thinking to use github but I have decided to try Codeplex before. As I what to finish all I can and then open github project.


WPF is very good feature but it uses a lot of reflection functionality which I do not support yet (as it required to compile in a lot of RTTI-kind information to embed).