-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how can i call dotnet function from c #34535
Comments
Tagging @vitek-karas, @swaroop-sridhar as an area owner |
You could use COM Callable Wrapper @Wahitler : .NET classes can be seen as COM classes from C. |
This is what
|
It seems that sometimes when docs are referencing to C++, they also mean C. See this doc https://docs.microsoft.com/en-us/dotnet/core/tutorials/netcore-hosting for the case of |
@am11 is correct - the approach described by https://docs.microsoft.com/en-us/dotnet/core/tutorials/netcore-hosting should work for C just fine. Side note: That doc only says that the sample it's using is written in C++, but it states that any native code should be able to so this. I do agree that maybe spelling out C explicitly would be good though. If you can share: What is the scenario you need to call managed code from native app? |
You have no other option than passing COM interfaces back and forth. |
You should not need any special hosting interfaces. The initial PInvoke can pass a function pointer to the native library. Managed code can marshal delegates to native function pointers relatively easily. The native code would then call the function pointer to interact with the C# host. The same can be done with COM, but it doesn't have to be COM, pure function pointers would work as well. |
I have an unmanaged "plain" C application that places a GUI around the SQLite database machine. The assembly I use ( my first venture into C#)is based on a Console Application.using System; namespace DotNetConsole
|
I suggest you look at the official sample for this: https://github.com/dotnet/samples/tree/master/core/hosting/HostWithHostFxr You should be able to build that from scratch and get that to run (please let us know if there are issues with that - follow the readme there for details on how to do it). Once you have that working you can start by tweaking it to match your needs. That's probably a faster way to get started than writing it from scratch on your own. To answer your questions:
As a side note: .NET Core doesn't really have the duality of Debugging hints: The If that's not enough details make sure you can access the stderr output of the process (if this a UI app on windows this can be a bit tricky to do, but you should be able to start it from console and redirect the error output to a file. Typically this can be done by adding If the error which should be printed out to stderr is still not enough, you can try enabling diagnostic tracing from the host by setting If the problem occurs during the managed portion you can also attach a debugger. Attach VS to the process and when attaching select both "native" and "Managed .NET Core" debuggers. This will enable so-called "Mixed mode" debugging where the debugger will be able to see both native and managed code at the same time and you will be able to debug both (breakpoints and so on). Make sure to enable stopping on all managed exceptions which should help with diagnosing the failures. |
Cheers. The interpreter visible to the user follows a broadly VB / VBA syntax.
The interpreter is augmented with built-in commands so that information can be exchanged between the SQLite database and Excel.
So it was time to start learning (again, again,...). The first success came with MetaHost.h ,mscoree.lib and CorBindToRuntimeEx and ExecuteInDefaultAppDomain. So I moved on to MetaHost.h ,mscoree.lib and CLRCreateInstance and ExecuteInDefaultAppDomain. But at least, by including a global variable in my target test EXE/DLL I was able to verify that :
Next I tried a coreclrhost.h based approach. Due to my impatience, I actually started with the nethost approach, but success has not yet been forth coming. This is the slightly reduced source code for my test assembly:
But it fails in my C host
with rc = COR_E_ARGUMENTOUTOFRANGE Now that I look at it, I wonder whether delegate_type_name is my problem. My goal is to enable a user to write a script that accesses the functionality resident in a .NET assembly, for example WINSCP. Q) if the nethost approach needs a json file, does it mean that for managed code that is not .NET Core, that I must use the coreclrhost.h approach? Your side note on the duality of .dll and .exe was very enlightening. my thanks, and once more, I will follow the suggestions that you made. Oh, one last thing. Why don't I just use say PowerShell?
Cheers There is still a long plod ahead. |
In your C host code point it to the Note that in this case you get back a function point which is directly calling the managed method. So you can call it multiple times without going through the hosting layers again. |
/cc @AaronRobinsonMSFT @elinor-fung Can you please be more specific in what exactly you have difficulty with? |
@Wahitler As @vitek-karas said having specific examples of what is happening would help us to offer next steps. Some observations on the interop though.
Providing the output of /cc @jkoritzinsky |
@Wahitler Is there any chance you could post text instead of screenshots for the native and managed definitions? trying to build a local repro is very annoying and error prone when one needs to transcribe from an image. If you have a small repro already existing that would be idea though. |
Closing due to inactivity. Let us know if this is still a problem. |
halo,have a good nice day.
i want to call dotnet function from c , not c++ ,but i can't find any solution.
I check dotnet Interoperability docs,i cant find anything about my need.Here are all about how call unmanaged code from dotnet context.
i found java language have JNI,and there is jni.h,Its aim is to make calling Java from C/C++ or C/C++ from Java easily.
Please reply me thanks
The text was updated successfully, but these errors were encountered: