Link to home
Create AccountLog in
Avatar of openshac
openshac

asked on

How to call a C# .Net component from within MatLab

I would like to call a DLL written in C# from within MatLab?
 
My current belief is that as of this moment, the easiest way for Matlab to call into .Net is via COM.
 
Only problem is that the only COM component I have managed to call from within matlab was an .ocx ActiveX control (using h=actxcontrol('ProgId') ... invoke(h,'MethodName') etc etc), which I don't believe can be used as a COM Wrapper around a c# .Net dll.
 
I need to create a COM interop for a .Net dll, and be able to invoke it from within Matlab.
 
I can see a lot of examples on the internet on how to call Matlab from C#, but very little about calling c# from within matlab.
 
Any help much appreciated
 
Thanks
Avatar of Henrici
Henrici

The best way I found to call anything external from Matlab is to use S-functions.  I built an entire framework once by simply using S-functions to wrap my DLL functions.  I did this in C/C++ however.

I think a COM wrapper would work as you suggested.  I also think you could make an unmanaged DLL that serves as the gateway between Matlab and your managed C# assembly.  That is, Matlab would call into your unmanaged C/C++ DLL, which would in turn call into the managed C# assembly.  The first part (Matlab to C/C++ DLL) is easy.  Second part C/C++ wrapper around managed C# may take a little work.

Hope it helps
Avatar of openshac

ASKER

Henrici,

Thanks for your reply, I am specifically looking for a worked C# example.

Thanks

Charlie
A nice all worked out protocal for calling C# from Matlab does not exist at the moment.  I described a framework for doing it in general.  I know the pieces I described are doable, although it will take some work.  I really think this comes down to how much of this C# code  you need to call in the future.  If this is a one-time thing then I can see where you are looking for a quick fix (it doesnt't exist.)  If you foresee calling C# often, then I would take a crack at my framework (again, 100% sure it can be done.)  Maybe wait a few days to see if anyone else has a better idea.

Here is another possibility.  Try to convert your C# code to unamanaged code C++ (unfortunately, I do not think a good quality converter is free.)  Then you can call the C++ code as I described above.  Also, if this is a one-time thing, can the C# code be re-written to Matlab script or C++?  Or can you turn the problem around and let C# host Matlab?

Henrici,
Thanks for your post.
>>I know the pieces I described are doable, although it will take some work
I  guess what I'm after is someone whose done this before and has some example code they can provide.

>>Here is another possibility.  Try to convert your C# code to unamanaged code C++
Unfortunately I am required to find a C# solution.

Thanks again

Charlie
ASKER CERTIFIED SOLUTION
Avatar of Henrici
Henrici

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Henrici,

Thanks for the post post but:

>>Unfortunately I am required to find a C# solution.

C++ is not an option for me.

Charlie
C# from Matlab without doing anything else does not exist.  If it did, Matlab would be advertising it all over the place just like they do with mex files and C/C++.

By the way, you stated in the original post you were thinking about COM.  That surely isn't C#.  If I had known you wanted C#, and nothing but C#, I would not have bothered since it does not exist.  Why mention COM when you only want C#?
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.