MATLAB class in .NET - c#

I have some custom classes in MATLAB and I want to use them in my C# project. I read a little bit and used deploytool to generate dll files. Sadly, its not working as I expect it to work. The classes are exported as methods and I cant access properties.
I read here and it says you cant export MATLAB class to C/C++ ( I assume not as a .NET assembly either). Is there a way around it?
I have huge amount of code in MATLAB, and its best if I use it directly than re-writing in C#.

There exists a Matlab to .net porting utility see here :
http://www.mathworks.com/products/netbuilder/

Related

C++ project using C#

I have a problem with two projects. My main project is in C++. Another one is in C# and it is measuring current network bandwidth (updated every second). What I need to have is that C++ project can get those values.
Mt first thought was to let the C# export calculated values to .txt file. Another project could read those values. But problem is that it would mean that both would use that file at the same moment what seems to be impossible (or maybe I could synchronize it somehow?)
I was reading a lot about creating and using library, but it looks complicated to me.
Are there any other ways to do that?
Please, I need help...
The simplest way is using stdin / stdout for this purpose. Just write the values to Console from the C# program and read it from pipe in the C++ program.
Or maybe you'd like to extend your project to C++/CLI (.NET-based C++ extension) and directly reference your C# library.
As well as the other options outlined by the others, you also have the option of making the functionality of your c# code into a DLL and then calling that from your C++ code to allow you to use the functionality of the DLL to get network information. I have a how-to type link here on how to create a C# DLL. You can then reference said DLL in C++ and utilise its functionality as needed.
Hope this helps, and let me know if you need any further information:)

How to call a c# dll from java program

I have a c# dll that needs to be called in Java.I see that there is a method using jni to call c++ dlls.How can I do it for a c# dll..Please help..I couldnt find any good material on this
From here:-
IKVM.NET is an implementation of Java for Mono and the Microsoft .NET
Framework. It includes the following components:
A Java Virtual Machine implemented in .NET
A .NET implementation of the Java class libraries
Tools that enable Java and .NET interoperability
You can use Java Native Interface. Or you can create a COM assembly from the C# code and use J-Interop to invoke it.
If you have C# dll sources you need to use maybe the better way will be to translate it to Java using some tools like GrassHopper.
According to GrassHopper key feature explanation it can convert MSIL to Java bite code. So can use without sources of c# dll
Check this: http://www.javonet.com
If you look for quick and easy solution then Javonet should work fine for you. It is light counterpart of IKVM and J-Integra works also as native bridge.
All you have to do is:
add Javonet.jar do your project call
call Javonet.addReference("yourlib.dll")
use your .NET library like it was almost JAVA package
Sample:
NObject obj = Javonet.New("yourDotNetClass");
obj.invoke("YourMethod","arg1", 2);
The syntax is not strongly-typed and works like reflection but gives you fastest access to any custom .NET code, third-party libs or .NET framework as no changes are needed on .NET side. If you need it is also possible to implement custom strongly-typed wrappers.
I do recommend this bridge as in my opinion it is easiest to quickly get things done but also other native bridges are worth checking as this is best approach for such case.
I would avoid going into custom JNI or COM unless you have a lot of time and you just want to learn, if you need quick and reliable solution take one of third-party bridges.

How to smoothly develop a C lib for C#/.NET?

I am developing a complex library in C++ and i plan on having a C interface so others can load up the DLL and easily access the lib. I haven't tried writing code in C# that access C code. I did a quick google and found code that uses a lot of attributes.
What can I do to keep my interface simple enough to not cause a headache trying to keep .NET in sync with it? Is there some kind of header generation tool i may use? Do i only use simple POD structs? I'm unsure how i should handle types as they are passed around as pointers. I am also thinking maybe i should avoid using anything that is a not an int/string or array.
I am developing it using MSVC but mostly using it with GCC. I know i should use the calling convention __stdcall. Beyond what i said i am totally clueless. I actually dont know how to load the DLL into .NET.
What can i do to ensure everything works correctly when writing my C lib and getting it to run with .NET?
Consider putting together a COM interface. Consuming COM from .NET is marginally easier than P/Invoke; at least you won't have to spell out prototypes for all functions in C#, the COM typelib importer will do that for you.

Calling UNIX and Linux shared object file .so from c#

Is there a way for a Shared Object file written in C and built on Unix to be called from C# P/Invoke?
Or do I need to use Java or something like that?
Mono has the ability to integrate with native libraries from within C# built on top of dlopen(3). You just have to use the DllImport statement with the name of the library (i.e. 'libform.so.5'), then wrap the native code and data types with a friendly C# class that takes care of all the low-level stuff. This page has a good overview with lots of information on how to deal with marshaling pointers and other unsafe types.
Once you've got your wrapper class written, you can just use that without worrying about the fact that it's using a native shared library underneath.
I would say at the least there's likely to be no easy way, especially if you mean C# on Windows. In that case you would need something that would be able to decode the shared object and get to the code in it, sort of a re-implementation of the ABI for GNU/linux. Also, any other libraries would have to be present and usable as well, such as the C runtime library and the like. This would likely be a very significant effort.
As for doing it directly under linux/Mono, see this answer: Calling UNIX and Linux shared object file .so from c# .
You could also try to see if what open office does, http://packages.debian.org/lenny/cli-uno-bridge could be helpful; but this is more of an interface rather than directly linking the two together.

how to import a header file into a C# project which imports a C++ dll

The header file used by the C++ dll contains a few user defined type definitions and function declarations.I have browsed many sites and all the posters say that its not possible to import header file into C#.I would like to know if there is any way to import header file into C# code as it is required to declare the functions of the imported dll in the C# application class.
Thanks
This is more or less a duplicate of Importing a C++ dll in C# however that question has some poor "information lite" answers.
Unfortunately there is no short answer to this, however you do have several options.
Wrap your C++ class in a managed C++
class. Then your C# project just
references the managed C++ project,
and everything works.
Like 1, create a managed C++ class,
but create a facade. This a
simplified interface to your class
exposing only the functionality you
need.
The other approach is to use PInvoke
to call the methods on the class.
You'd need to built a C#
representation of the class for the
pinvoke calls. This can be
problematic and involves a lot of
trial and error if you don't know
what you're doing.
All of the above involve learning some technologies that will be new to you (manged C++ or the intracies of PInvoke). Unfortunatly there's no other way.
If you can I'd go with 2.
Have you considered bridging this gap with a C++/CLI project? If you write all your wrapper library and interoperability code in a C++/CLI project you can easily define managed types that expose the defines in C/C++ header files.
When I have submbled upon the same problem - given .h and .lib files intended for C/C++ use, I have written a wrapper for it in C++ with managed extensions. It is really quite simple. Your C++ assembly will play neatly in your Visual Studio solution and you'll be able to single step right from C# into managed C++ into unmanaged C++ and back again light as a breeze! :-)

Categories

Resources