I have a control problem which i solved by using Model predictive control (MPC). I have stated the problem in MATLAB and used FORCES http://forces.ethz.ch/ to solve it. FORCES is a web service from ETH Zürich which generates library-free ANSI-C code. For testing the code they provide a script to compile the c-code to a MATLAB .mex file. This all works great.
I now want to use this function in my C# Windows Forms programm. To use C-code in C# I have to compile the c-code to a dll and then use P/Invoke? Or is there an other way? How can I compile it? Can I just use the code as it is or do I have to edit it (adding __declspec or something like that)? Is it possible to compile the dll directly out of MATLAB?
The .c and .h files can be found on my homepage: http://n.ethz.ch/~rehofman/download/.
I run 64bit Windows 7, MATLAB 2013b, Visual Studio 2012.
I have MinGW already installed if that is needed.
EDIT:
Ok here is a more specific question: I have a function(myMPC_solve) written in c-code which I want to call from c#. What is the best way to do so? The arguments and datatypes are all in the .h file.
You could do any one of the following:
Compile the C code to a DLL, and include it using p/invoke.
Compile the C code into a C++/CLI mixed mode assembly, wrap the functionality in a ref class, and consume that from C#
Wrap the C code with a COM server and consume that from C#.
Leave the code in MATLAB and consume that from C#. There are multiple ways in which MATLAB code can be consumed by C# code.
And I'm sure there are other options beyond the above.
Now, if you are hoping for a detailed step-by-step instruction of what to do next, I'm sorry to disappoint you. You asked a broad question and I've given you a correspondingly broad answer. What you should do next is to pick one of the options above, and implement it. When you get stuck, do feel free to ask a more specific question.
Related
Use Case
I have a DLL written in C/C++ that is the .dll file and a .h. I need to write an ElectronJS app (.exe) with NodeJS as backend to access some function calls as part of a bigger program. I also have a C# program that has implemented a basic program that uses the DLL.
So Far
Im able to interact with some basic functions of the DLL with node-ffi-napi and ref-napi. The main problem here is that the dll uses some really big structs (>15members of short/long/DWORD/etc) and struct pointers to obtain information. While there is ref-struct-di it seems that it would be very hardwork to try to implement big structures and pointers to make an API wrapper on Node since is not native code.
Options
I think there is 3 options but since im not very familiar with Node i dont know which would be the best, what tools/packages to uses or if there is another way.
Hard work trying to implement my structs and structs* with ref-struct-di and a really extensive testing to check every parameter to see if the struct is well implemented and fail proof (seg faults) since is not a native tool.
Use the basic implemented C# tool that already does a wrapper to the dll API and have the functions implemented. Is there a way from my Node app to have some C# code running also? So i can call the C# wrapper that has native structs and see how to convert them to a dictionary so Node use them.
Implement a C++ wrapper to do similar to the C# program. This way i can access the dll api (and the .h) easier to handle the calls and structs/structs* and then from Node, call some C++ functions like for example GetInformation() and return a dictionary instead of the dll original funcion.
Since the ElectronJS app will be an .exe i need to handle everything on the same program. Its possible?
To sum everything up, what would be the best way to handle the DLL? Is there a way from Node to have some C++/C# code running also and interact with the app in runtime so i can call the dll? Anyone know any tool/package/github/link to use/read more about?
Thank you in advance.
EDIT:
Im reading about Node-API to integrate C++ code as addon. As my understanding, i will have a C++/.h files inside my Electron app and then compile everything. So my C++ code calls the DLL and then from Node i call the C++ wrapper. Is this correct?
Another way would be to create a new DLL in C++ that calls the original DLL. Then with ffi from Node access the new DLL that returns me a json/string or something less "complex" with only the info i need and not all the big structs. Right? Ideas? this way seems easier to write two separate programs without the N-API but would require two compilations.
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:)
Good day,
I want to take an existing C# project and wrap functions into a C++/CLI DLL. I need to be able to read this DLL from VB 6. I choose this route because I won't have to register a .net DLL in order to use it with VB 6. Frankly I have no experience with this kind of thing so I would greatly appreciate a good example. I know there are plenty of similar questions like this but I haven't been able to find anything simple enough for me to understand.
This is a fairly unwise route to pursue, making C# code [ComVisible] is pretty trivial and Regasm.exe should not scare you. The usual mistake with Regasm is to forget to use its /codebase option on your dev machine.
If you insist on not taking advantage of this then you'll need to find a way to get the CLR loaded yourself so it can execute your C++/CLI and C# code. There are three basic ways to do so. You've written off COM interop and hosting the CLR yourself isn't very practical if the host app is VB6. You however can take advantage of the C++/CLI compiler's ability to generate unmanaged DLL entrypoint stubs that load the CLR for you and switches to managed code execution. Do so by writing a static function that you decorate with __declspec(dllexport). The technique is shown in this answer. Beware that it isn't particularly performant and you'll have to live with the restrictions imposed by the VB6 Declare statement.
Also check this post-processing tool that can inject these stubs directly into a C# assembly. Not sure how reliable it is, I haven't used it myself.
Be a man and make that DLL under a couple of different compilation/linking/function-call-protocol configurations. Then go ahead and try a couple of different referencing configurations in your VB6 project. Post what works. Contribute, go forth!
Take a look at Export Managed Code as Unmanaged
and C# Project Template for Unmanaged Exports
I´m really in the need of getting a deeper understanding of how to set up things right to get an elegant interaction between my C++ and C# code bases.
What I want to achieve is an in-game editor written in C# for my game engine (C++/DX). For doing so I let VS build my engine as a C++ dll with some additional functions (unmanaged code) to access the required functionality of my engine from the C# editor code base. So far so good.
The first thing which is bugging me is that I´ve to build the dll with CLR support. Otherwise C# does not accept the dll for some reason. It doesn´t even allow me to add it to the resources ("A reference to 'C:\Users...\frame_work\Test\frame_workd.dll' could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component.").
And when I build the dll with CLR support and add it to the references in C# ,re-build without CLR support, start my editor and make a function call from the dll then I get an Exception HRESULT: 0x8007007E. I searched for it but the only thing I found had to do with dependencies but that doesn´t fit to the alert I get when adding the dll to the resources.
The other point is that I always have to switch the configuration type between application (.exe) and dll. in VS C++ depending on whether I want to run my engine directly or from the editor and every time the complete project is build completely new.
So, could someone explain to me how to organize this the right way? And what could be a possible reason why C# wants the dll to be compiled with CLR support?
Thank you guys/girls.
There are two ways to deal with this.
Either you make your C++ code provide an API which has a fully compliant COM object. If the object is COM then C# can directly interop with it. (This is why you can't add it as a reference directly)
However I think what you are really wanting to do will involve a P/Invoke (calling C/C++ native code from C#). This is entirely possible but it's not always easy. You need to deal with conversions between your C++ API and your C#, pointers and you need to be very careful to pin any references that your C++ code writes to in the C# app.
C# code is managed code (runs in the CLR), and can only directly* reference managed assemblies. So of course you're getting an error when you build against a managed assembly, and then sneak in and replace that managed DLL with an (incompatible) unmanaged DLL. You're basically trying to lie to the compiler, and that generally doesn't end well.
If you want your C++ DLL to be accessible from C#, the simplest way to do it is to build it as a managed assembly (i.e., CLR support). Which you're already doing. Just take out the extra step where you replace the working managed DLL with a non-working unmanaged one.
Also:
C++ dll with some additional functions (unmanaged code) to access the required functionality of my engine from the C# editor
That won't help you, because C# can't directly* call unmanaged code. The simplest way to make this work will be to make additional managed classes and methods in your C++ DLL. Then your C# assembly will be able to directly use those managed classes.
* As Spence noted, you can use -indirect- means (P/Invoke and COM) to access unmanaged code from C#. But that will make your life much more complicated than it is now, not to mention how it will complicate your build and deployment. You're already really close to something that should work -- don't add all that extra complexity.
When calling functions with P/Invoke, you don't add the DLL to the C# project resources (or what you probably meant, references, either).
You will add it to the file list in your MSI project, of course.
I am very new to C#, and i am trying to use a help package for my project.
The package is written in c and has
1) /bin/ several .dll files
2) /include/ has a header file
3) /lib/msvc/ .lib file
my question is how can i use those files in my C# WPF project?
i know there is no "#include" in C#, and the .dll can not be imported by adding to the project's reference. so how can i do it in C#?
Thanks
You need to add a series of C# Platform Invocation method definitions. This tells C# how to call into the .dll, and use the C API directly.
The header and library files are completely unused.
Just adding to the answers, you may want to take a look in this blog post. It has a link to a Visual Studio addin that can generate P/Invoke signatures from your headers.
Best
You might find managed C++ useful. You can write a managed C++ library that uses the header files and the .lib directly, and wraps them with a set of .NET classes to be used by C#.
You need to use pInvoke
http://msdn.microsoft.com/en-us/library/aa288468%28VS.71%29.aspx
There are a lot of ways to do this "interop" - I once did a talk called "head spinning interop". The key is the structure of your native DLL. Since you say it's in C, the chances are it's in perfect shape to be used with PInvoke, which works best with C functions or C-style functions in C++. The other answers have excellent pointers to the syntax and some help to get your function (and the parameters it takes) declared on the C# side.
The other two main options are a C++/CLI wrapper, a good choice when the functions take a lot of complicated types, or COM, which would almost certainly require you to change the native code so is not a good fit in this situation.