How to Invoke C# dll from Matlab? [duplicate] - c#

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Interoperating between Matlab and C#
I need to use some functionality from a Dot Net C# managed-dll How can I call this Dot Net C# dll in matlab ?

One way to make this work is to use COM. You can implement a COM object using .NET, and MatLab can work with COM objects (e.g. see http://www.mathworks.co.uk/help/matlab/matlab_external/exploring-your-object.html).
If your managed C# DLL is not yet exposed to COM, you may have to write a little wrapper (depending on whether or not you have access to the source code).

You could use Matlab's system command and this approach:
Need to run a c# dll from the command line

Related

How do I call a native library from C#? [duplicate]

This question already has answers here:
C# library to native C++ application
(3 answers)
Closed 3 years ago.
What is the best solution to access c# code via native code (C++)?
I have C# code which I want to call from native project, so I'm considering writing a COM wrapper but I wonder whether there is a better option (framework/design pattern/architecture etc.) available in .NET?
COM interop is without a doubt the best bet. It's a known, supported framework for achieving exactly what you want to achieve.
There is another alternative however - it is possible to edit the IL code inside a compiled .NET assembly to flag methods within the assembly as native exports. A detailed breakdown of the changes can be found in this CodeProject article.
Robert Giesecke has created Unmanaged Exports to simplify this process using simple method attributes. A NuGet package can be found here.

is it possible to import a dynamic library written in c++ into a c# program [duplicate]

This question already has answers here:
Using C++ Class DLL in C# Application
(5 answers)
Closed 8 years ago.
All in the title really. I have a library, written in c++ and compiled into a dll. I would like to use this functionality in a c# program. Is it possible to use the classes/functions from the library straight from c#, would I need to write some wrapper code to use it in a managed environment? Can they be used in an unsafe context? Sorry if this is a silly question - I'm a c++ programmer trying to move over to c#.
p.s. the library is from a third party, so I cannot port it direct to c# even if I had the time.
It depends on how the C++ dll exports it's functionality.
If it's through C++ classes, then your best bet is to build a wrapper in C++/CLI that will consume the C++ classes and expose .NET classes to interact with them.
If it's through "classic" C-style functions, then you can use p-invoke to call the functions directly. It would be similar to the ways system DLLs like System32 and User32 are accessed.
Yes it's possible!
First add the reference to System.Runtime.InteropServices:
using System.Runtime.InteropServices;
After you have to import the function on the external Dll:
[DllImport("ExternalDLL.dll", EntryPoint = "FunctionNameOnExtDll")]
public static extern CSharpDataType FunctionNameOnExtDll(ParameterDataType ParameterName);
And finally use it!!
FunctionNameOnExtDll(Parameter);
Yes it is possible, just knowing the signature of the functions you want to call and use System.Runtime.InteropServices facilities.
To help, use this tool PInvoke Interop Assistant that can help define data structures to be passed in calls.

Calling C# Dll from unmanaged C++ application on WindowsCE [duplicate]

This question already has answers here:
HOWTO: Call Managed C# Interface From Unmanaged C++ On WindowsCE Compact Framework
(2 answers)
Closed 9 years ago.
I have an off the shelf product which supports C++ plugins.
Im wondering if its possible to call a C# Dll from an unmanaged C++ dll so that i can do the grunt work in C# which im familure with and just use the C++ plugin to call and return values from the C# Dll.
Im using WindowsCE which uses .net compact framework which means compiling C++ with the /CLI flag is not an option.
Can anyone explain to me how i can do this. Im not a C++ Developer but im sure i can work it out if im given a simple example.
Ive had a look for something simple like a hello world app online but can find anything.
Thanks.
See: Hosting ActiveX Controls in the .NET Compact Framework 2.0
COM support in the .NET Compact Framework 2.0 is still somewhat
limited compared to its desktop computer counterpart. For example,
there is no support for external activation. You cannot write a
standalone COM component in managed code; if it were possible, a COM
component could have been instantiated inside a regular, unmanaged
Win32 process. Because managed code requires the Common Language
Runtime (CLR) to execute, such setup needs the Win32 process to host
CLR. Because CLR hosting is not one of the features of the .NET
Compact Framework 2.0, the whole external activation model is not
available.

Convert Java class to DLL [duplicate]

This question already has answers here:
Is there any way to compile Java code into a DLL?
(3 answers)
Closed 9 years ago.
I need to use Java code in .NET (C#) and am looking for a way to convert a Java class to a DLL file (which I can then reference in my .NET app). Is this a valid scenario? If yes, How can I do it?
You can use IKVM.NET to use Java classes and libraries from .NET.
Basically that is not a good idea. Since java emphasizes on platform independence, but dll is platform dependent. So, think about pack into a jar file, execute and consume it somehow
May be this link will be useful for you:
http://www.codeproject.com/Articles/13549/Using-Java-Classes-in-your-NET-Application
For this you can use IKVM.NET.

Exporting a native C function from a .net DLL? [duplicate]

This question already has answers here:
Is is possible to export functions from a C# DLL like in VS C++?
(4 answers)
Closed 6 years ago.
I have a .net assembly written in C#, and I'm looking to export a native C function from the assembly.
I have an application which will perform a 'LoadLibrary()' on any DLL's found in a 'plugin' folder. After loading the DLL, the application performs a 'GetProcAddress' looking for a function named 'Register'. The application expects this function to follow the C calling convention.
How can I export a function named 'Register' from my .net assembly, so I can successfully hookup with the plugin system for this application?
Thanks,
Andrew
Have a look at Unmanaged Exports.
Write a .Net library in Managed C++ and there you can export a "Native" method/function.
Sadly Microsoft does not support this feature, and you have to change the msil after the build to expose those methods.
It is possible as one guy has shown a reasonable solution
on codeproject or here,
but it requires a post build step and you are on your own after that.
I don't know if this hack will work on .net 4.0 or later though.
Hopefully Microsoft will listen to us and support this simple feature in C# since the CLR support is already there.
What you want is a Reverse P/Invoke. You can't actually embed a C function in a C# dll, if by that you mean a function actually implemented in C, but by following the tutorials given on the linked page you can create a DLL export that's callable by unmanaged C/C++ code.

Categories

Resources