Calling C# Methods

I recently came across this C# library https://github.com/Tyrrrz/YoutubeExplode

It seems to be a very useful tool so I would like to use it, as I don't speak C# I would like to know if there is a way to use it in C++.

I have downloaded the YoutubeExplode.dll,

Thanks.
Last edited on
This looks like a .NET library so it can only be called from a .NET App.
You could use C++ CLI, but learning it might take the same effort like learning C#
https://docs.microsoft.com/en-us/cpp/dotnet/dotnet-programming-with-cpp-cli-visual-cpp?view=msvc-160
I read about it but I need it to be from native C++.

https://www.codeproject.com/Articles/16206/Call-C-code-from-C-and-read-an-array-of-struct-whi
This article explain how to do it in a simple way but I am getting |error: stray '\377' in program| errors when compiling the code with :
#import "MyInterOp.tlb"

Also I can't seem to get a .tlb file from YoutubeExplode.dll using resgasm. Throws this error:
 
error RA0000 : Could not load file or assembly 'System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 
I'd suggest just learning C#. It's a pretty cool language. It's replaced C++ as my go-to language for stuff I want to throw together quickly.
I say this from experience, calling .NET functions from native code is a pain (the reverse not so much, BTW). You really don't want to do it unless you have no alternative, in which case C++/CLI is the way to go, as the runtime takes care of setting up the environment for you.
@helios

I'd suggest just learning C#

That was my first thought, and really think it is not difficult because I was peeking the code in that library and I could "understand" most of what was going on.
But now is not a good time because I have a lot on my plate right now. I just thought learning Java, compilers(to be implemented in Java), database, web development(all for school), and I still have ongoing C++ projects.

I say this from experience, calling .NET functions from native code is a pain

I am seeing that, but is there a why these type of things are very hard and a real pain, why isn't created a ready/easy-to-use tool or way to call functions among different languages. Maybe it's not a necessity but it sure would be useful.

Your suggestion though, is really good, I might follow it in the near future just to be able to use this library.
why isn't created a ready/easy-to-use tool or way to call functions among different languages.
In the case of C++ and .NET, that tool is C++/CLI.
In the general case, it doesn't exist because it couldn't possibly exist. Languages have too much variation in the types they support, the ways they manage memory and resources, the runtime environments they need to run, etc. There's just no way.
There's just no way.

I like to think that since all languages are just meanings to talk to machines and that's all they do in the end it shouldn't be hard but unfortunately it is.

I noticed that it's relatively easy to call c++ code from languages like Python, Java and C# but the other way around is really hell and non-existent(couldn't find a way for python).
Is there a reason for that?
I like to think that since all languages are just meanings to talk to machines and that's all they do in the end it shouldn't be hard
If all you want is to pass byte arrays (which is ultimately what the underlying machine deals with) back and forth then the problem becomes somewhat easier. You just create different processes that each runs whatever language you need and connect them via network sockets or pipes. You will still need to decide on a common data interchange format with well-defined semantics so that information can be de/serialized.

couldn't find a way for python
In Python this is called "embedding". The Python code becomes a slave to the other language, which must call the library's relevant functions to initialize its data structures and load code. The master can then instantiate classes and call functions.
https://docs.python.org/3/extending/embedding.html

In the case of Java there's the JNI, which is, IMO, fairly cumbersome but not much more so than I would expect. Not quite as easy as .NET's P/Invoke, though.

Is there a reason for that?
The reason is that C has stupidly simple functions and types. Take a look at what a C interface for FFI looks like: it's always very narrowly defined and all that gets passed around is integers, raw buffers, function pointers, and opaque pointers. It rarely strays very far from that, because it quickly becomes unusable.
Conversely, try instantiating a C++ class from C and then calling one of its polymorphic members, or calling a variadic template function instantiation. Let me know how that goes, because I've never even bothered trying.
Generally, the more complex the language's type system, the more difficult it's going to be to do FFI.
Last edited on
You will still need to decide on a common data interchange format with well-defined semantics so that information can be de/serialized.

Yeah that's kinda what I meant, I was thinking about creating something like Java interface for example.

you need and connect them via network sockets or pipes

Isn't this the way they do in multi-languages projects?

https://docs.python.org/3/extending/embedding.html

I think I can't thank you enough, I have found a lot of interesting libraries written in Python, it's good to know that I can use it in my C++ project.
Edit: I took a peek at the content in the link. From what I saw it will be better learning python and only and if necessary embed python into C/C++ because to be able to use it you will have to have a good understanding of python.

Generally, the more complex the language's type system, the more difficult it's going to be to do FFI.

Thanks for the info, really useful.
Last edited on
Isn't this the way they do in multi-languages projects?
Not exactly. The system gets broken up into different processes based on how to best distribute the design's complexity. Once you've accepted that you're going to have separate processes anyway, you can design each program completely independently of the rest of the system, using whatever technologies are most appropriate. The fact that different languages get used is incidental.

I have found a lot of interesting libraries written in Python, it's good to know that I can use it in my C++ project.
That's probably not a great idea. The typical use case to embed Python in C/++ is to add scripting to a complete program, for example to implement plugins, or to allow the user to automate parts of the program. You don't normally want the native parts to be unable to function without logic in the scripted parts. If that's what you need you might want to look into compiling the Python code down to C.
Thanks for all the info. learned a lot.
Topic archived. No new replies allowed.