How to call c# code from Fortran dll - c#

Currently I have abc.dll which is fortran dll. Now I want to call C# code from abc.dll. Is there any way to call the C# code from fortran dll ?
thanks
Sagar

Typically, if your program is written entirely in native code (as I believe the Fortran dll would be), you'll need to call a method that's been exported (dllexport) from another native code module. In this case, you'll want to create a Managed C++ dll that exposes a native interface and internally makes the call into the C# code.
Edit: If the hosting program is managed code, and you need to do a C#->Fortran (native)->C# calling sequence, then delegates as unmanaged function pointers can be used as linked in the comments above. However, if the executable is not managed code, you'll need to go the route I mentioned.

Compilers supporting recent Fortran language features (the 2003 standard) will support C-interoperation. You interface with other code through its C interface, using the ISO_C_BINDING module and the BIND construct. Most recent compilers have it, it's standard and you can find a lot of documentation (like this one) by Google'ing the keyword ISO_C_BINDING.

Related

VBA runtime error 453 with declare statement [duplicate]

In VS C/C++ you could use extern "C" __declspec(dllexport) -function declaration-.
How do I accomplish this in a C# dll? Is there C# code equivalent to the above code?
Edit: More info
I am trying to create an add in for Notepad++ and I want to use C#, but the common way I've seen so far is to use legacy C++ code with the above call to export a few of the functions that Notepad++ expects to import and call. There is an example app using C#, but this still requires a loader DLL, which I assume from the comments/answers below is the only way for C#.
Unmanaged Exports =>
https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports
DLLExport => https://github.com/3F/DllExport
How does it work?
Create a new classlibrary or proceed with an existing one.
Then add the UnmanagedExports Nuget package.
This is pretty much all setup that is required.
Now you can write any kind of static method, decorate it with [DllExport] and use it from native code.
It works just like DllImport, so you can customize the marshalling of parameters/result with MarshalAsAttribute.
During compilation, my task will modify the IL to add the required exports...
I've seen people do this before, but it required ildasm, adding the MSIL .export directive, and then reassembling. A program named dll_tool can do these steps for you.
If you want to build a mixed-mode DLL with both native and managed exports, you should be using C++/CLI, which is specially designed for this purpose.
Yes, it is possible to export functions from a C# dll in much the same way that C++ does it! You need a little help from an add-in Unmanaged Exports (DllExport for .Net) that facilitates this process, or from a similar method such as Exporting functions in C#/VB.NET to native code.
Please see Code to Export C# DLL to Metatrader Build 600+ for a working example using Robert Giesecke's C# Project Template for Unmanaged Exports to export a C# dll to a legacy application (Metatrader) that has a great deal of similarity to C++.
Additionally, you might find Native and .NET Interopability interesting though it is mostly geared toward accessing native code from within .NET rather than the other way around.
No, you cannot do that in the same sense as you do in C and C++.
But you can create COM API to achieve that which then you can use in C and C++ code.
See these articles
C# Classes as COM Objects
Calling Managed .NET C# COM Objects from Unmanaged C++ Code
COM Interop Part 1: C# Client Tutorial

c# dll in __cdecl conventions

My bos, who is not a CS guy, asked me to develop a dll in c# for him.
further investigation on my side reveal that the dll should be in cdecl conventions.
If I'll take this project it will be my first dll development.
browsing through google tell me this is possible, but before I tell the bos
I can do it, I wanted to get your advice regarding this issue.
(lets assume nor time or code-quality are the main issue, but it needs to be done)
Any comments regarding such possibility would be much appreciated.
It sounds like you are looking for unmanaged function exports. There's no out-of-the box way to achieve that in C#. You can use Robert Giesecke's Unmanaged Exports. This performs some magic during compilation to modify the emitted IL to allow unmanaged exports.
The officially supported way to export unmanaged functions from a .net DLL is to make a mixed mode C++/CLI assembly.
Yet another way to export your functionality to unmanaged code is to expose a COM server from your C# code.

C++ program hosting .net exposing objects from C++ to C#

i'm exploring the idea of use .net as a scripting language for my program.
i found a lot of examples on internet that shows how to host .net and then, from c++ call functions from .net.
But i need to go on the other way too, from this code in c#, i need to be able to create/call c++'s objects from this c# script, so i need to expose functions/objects to control it from C#.
How can i do it?
Simple Example to show what i'm talking about
I call my c++ method "CreateGUI". It'll call .net code: "InitializeGUI", and this "InitializeGUI" need to check if an object (for example, the texture) is instantiated inside C++.
Can someone help me?
Thanks!
If you use managed C++, you can create a managed C++ component which can be called from your C# code. If you use unmanaged C++, you can either create a C++ COM component and use it through C# via RCW (Runtime Callable Wrapper) mechanism or you can create a C++ dll and call it from C# via PInvoke.
You might also investigate Mono, which is explicitly intended to be embedded and will run any compiled .NET assembly, meaning the full powers of C#, VB.NET, even F# will be available.

C++ interop CLI vs PInvoke?

I believe lot of people already asked this question before, but i kept getting confused more and more. I am looking for a answer in layman's terms. I have a c++ library to perform one action. I need to call the functions from this library from my C# program. What is better way to do this and why? whether pinvoke from C# app or write a wrapper in C++/CLI.
There is only 1 function in C++ library (ReadNextRecord) which will be called from C# program. C# program should first create object of class defined in C++ library and then call a function to get the next record from a data source. Function is called many times ( >50000 times) so efficiency is an issue.
In most documents I see how to wrap a function to call from C# code. I dont see more complicated example where a c++ object is created in C# space and then a func is called on the object.
Please advice.
Regards,
Alok
When dealing with C++ code, it is easier to use C++/CLI (at least, assuming you are working with the source code for the C++ library or at least know it is compiled with MSVC, C++ is not ABI-compatible across compilers in general). PInvoke is mostly useful for C functions, but can be made to work with some C++ functions (see this question).

Creating C++ Dll, and call it from C#

In my project I got a device which comes with C++ Sample codes. The codes are ok and the device is working as expected.
But I need it to talk with my C# interface because all other devices are currently using C# interface.
So I am planning to create a DLL Wrapper for the driver. I will create a C++ Library of my own (from source code with proper interface) and Call this C++ Library from C# using DLLImport (just call my interfaces there.).
I am pretty sure it can be done this way, but I have never created a C++ Library and used it from C# yet. So, can anyone refer me to some tutorial that goes with my problem?
I am using C++/C# int VS.NET 2008.
Regards,
Maksud
Have a look at
using a class defined in a c++ dll in c# code
Another useful tool you have at your disposal is C++ CLI.
You can use C++ CLI to create an intermediate library - one that exposes managed classes but runs unmanaged C++ code. You can actually mix managed and unmanaged C++ in the same DLL.
The unmanaged portion can accesses the unmanaged DLLs without having to use the PInvoke functions.
Your C# code can access the managed classes in this intermediate library.
Depending on the DLL and what you need to do you may not need to create a wrapper directly. You might be able to get away with P/Invoke for the functions. You will need to evaluate your specific needs and what is already available in the libraries/code provided.
For anyone who comes to this question and are looking for answers, you may want to try xInterop NGen++ , a C# wrapper generator for native C++ DLL, which has been just released to the public, the tool can generate C# wrapper for native C++ DLL automatically and instantly by using advanced P/Invoke technologies. Check out the current version and a free version will be out soon.
(I am the author of the tool)

Categories

Resources