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:)
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 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.
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/
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 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.