Calling C# Dll from unmanaged C++ application on WindowsCE [duplicate] - c#

This question already has answers here:
HOWTO: Call Managed C# Interface From Unmanaged C++ On WindowsCE Compact Framework
(2 answers)
Closed 9 years ago.
I have an off the shelf product which supports C++ plugins.
Im wondering if its possible to call a C# Dll from an unmanaged C++ dll so that i can do the grunt work in C# which im familure with and just use the C++ plugin to call and return values from the C# Dll.
Im using WindowsCE which uses .net compact framework which means compiling C++ with the /CLI flag is not an option.
Can anyone explain to me how i can do this. Im not a C++ Developer but im sure i can work it out if im given a simple example.
Ive had a look for something simple like a hello world app online but can find anything.
Thanks.

See: Hosting ActiveX Controls in the .NET Compact Framework 2.0
COM support in the .NET Compact Framework 2.0 is still somewhat
limited compared to its desktop computer counterpart. For example,
there is no support for external activation. You cannot write a
standalone COM component in managed code; if it were possible, a COM
component could have been instantiated inside a regular, unmanaged
Win32 process. Because managed code requires the Common Language
Runtime (CLR) to execute, such setup needs the Win32 process to host
CLR. Because CLR hosting is not one of the features of the .NET
Compact Framework 2.0, the whole external activation model is not
available.

Related

About .net and winRT

I'm used to code in .net but I'm kinda new to winRT and have a couple of questions:
When I create a Metro windows store app in c# using winRT and the subset of .net for windows store apps, is this a managed application? (hosted by the clr?) or is it unmanaged and the .net part is simply mapped to winRT.
What happens when there is a reference to a windowsRT component written in c# from an unmanaged language like c++? It makes use of the CLR or is it 100% native?
is this a managed application?
A WinRT app runs as an out-of-process COM server. Large parts of the code you use is unmanaged, anything you use in your XAML for example was written in C++. But that is an implementation detail that is pretty hard to see, it has all been wrapped carefully by the language projection built into the CLR. Which makes all the WinRT interop look like managed classes. Not unlike the way you can add a reference to a COM component and it will look like managed code as well.
For all practical purposes you can call your app a managed app and you'll use the managed debugger to fix bugs.
It makes use of the CLR or is it 100% native?
Native C++ is 100% native. Same scenario in reverse if you write a WinRT component in C# and it is used in a C++ or Javascript app, those apps have no idea that you wrote your code in C#. It is COM that's underneath it, the universal glue that Microsoft uses to allow different languages to talk to each other. The CLR takes care of the interop, just like it does if you write an add-in for an Office app or use Microsoft.Office.Interop.Excel to automate Excel.
Note how a .winmd file is pivotal to bridge the gap, the exact equivalent of a COM type library or .NET metadata that tells a compiler what the interfaces look like in a language-independent way. Also visible in some of the restrictions when you write your own WinRT component, your class needs to be sealed since COM does not support implementation inheritance. And cannot be generic. And you can expose a DateTimeOffset but not a DateTime, etcetera.

Call .NET DLL from Delphi [duplicate]

This question already has answers here:
Call C# dll from Delphi
(3 answers)
Closed 7 years ago.
As my first time as a student employee programming I have to create a DLL in C#.NET which is callable from Delphi.
I've read about this being not so easy due to Delphi being native code and .NET being managed.
I've also read about 'Unmanaged Exports' from Robert Giesecke. Could this be the solution?
Since I do not have Delphi on my machine (Delphi isn't free), I can't test this out for myself.
Also, will I have problems with my datatypes when I implement the Unmanaged Exports?
Last but not least, is there a language I could use that would have the same problem as Delphi for loading the DLL? It'd be nice for me to be able to test.
UnmanagedExports is one option. It certainly works very well. Other options include:
Exposing the functionality using COM.
Exposing the functionality via a C++/CLI mixed mode assembly.
Hosting the CLR from your Delphi code.
The final option is not very tractable. Don't attempt to do that.
You'll almost certainly encounter problems with interop data types unless you are already an expert in the field.
You can certainly test this out with FreePascal. Very likely you can write code in FreePascal that consumes your .net assembly and later use the exact same code in Delphi. Even if you have to change it somewhat, the lessons you will learn will be exactly the same.
You can make your .Net DLL as Com visible. Check this link.
Delphi can load Com objects without problems. This in theory, I don't have specific code samples, but you can try with different tutorials, they practically explains the same.
I don't know about the data types, but COM always tries to cast to a base type (double, float to decimal, number to integer, and complex objects to dynamic objects).
You can use the .Net Runtime Library for Delphi in http://dotnetruntimelibraryfordelphi.sourceforge.net/
The runtime library allows you to host and access .net library types.

Exporting a native C function from a .net DLL? [duplicate]

This question already has answers here:
Is is possible to export functions from a C# DLL like in VS C++?
(4 answers)
Closed 6 years ago.
I have a .net assembly written in C#, and I'm looking to export a native C function from the assembly.
I have an application which will perform a 'LoadLibrary()' on any DLL's found in a 'plugin' folder. After loading the DLL, the application performs a 'GetProcAddress' looking for a function named 'Register'. The application expects this function to follow the C calling convention.
How can I export a function named 'Register' from my .net assembly, so I can successfully hookup with the plugin system for this application?
Thanks,
Andrew
Have a look at Unmanaged Exports.
Write a .Net library in Managed C++ and there you can export a "Native" method/function.
Sadly Microsoft does not support this feature, and you have to change the msil after the build to expose those methods.
It is possible as one guy has shown a reasonable solution
on codeproject or here,
but it requires a post build step and you are on your own after that.
I don't know if this hack will work on .net 4.0 or later though.
Hopefully Microsoft will listen to us and support this simple feature in C# since the CLR support is already there.
What you want is a Reverse P/Invoke. You can't actually embed a C function in a C# dll, if by that you mean a function actually implemented in C, but by following the tutorials given on the linked page you can create a DLL export that's callable by unmanaged C/C++ code.

Connecting C++ to C#

I've been writing a program in C++ and noticed there is a library in C# that someone else wrote that I would like to link in to my code.... but I'm not sure how to do that. Can someone suggest something? Doubt this matters, but I'm using Windows 7 with MSVC2010.
Thanks in advance!
You can try compiling your C++ program in C++/CLI mode. Then the compiler will produce a .NET executable which can create C# objects and use their methods.
C++/CLI is discussed here: C++/CLI
If you're familiar with COM you could access the .NET library through COM. If the library doesn't provide COM interop out of the box you could write a wrapper around it using C# and expose that through COM.
If you're going to pull in a .NET library you should be aware that it requires a .NET runtime which may take up valuable resources. If you ware building the application in C++ for performance reasons, maybe you're better off porting the parts of the library you need to C++.

Reverse PInvoke and create a full unmanaged C# program

I know this is a strange question but the idea is simple: I prefer C# syntax rather than C++:
-Setters and getters directly inside a property
-interfaces
-foreach statement
-possibility to declare an implicit cast operator
other small things...
What I really don't know is if is possible to import a c++ dll (expecially std libraries) in C# if I don't use any namespace (even System)
The idea is just to write a program using everything that you will normally use in C++ (nothing from CLR so), even printf for example
Thanks for any answer
No it is not possible to simply import existing C or C++ files into a C# project. They are very different languages and cannot be mixed at the source level.
The way to mix C# and C++/C applications is at the PInvoke or COM Interop level. This works by duplicating the signatures in C# and allowing the C# compiler to determine the binary layout of the native type in order to marshal between the languages.
There's now something close to this
.NET Native compiles C# to native machine code that performs like C++. You will continue to benefit from the productivity and familiarity of the .NET Framework with the great performance of native code.
It's for Windows Store apps only (desktop apps may come in the future):
Desktop apps are a very important part of our strategy. Initially, we are focusing on Windows Store apps with .NET Native. In the longer term we will continue to improve native compilation for all .NET applications.
And
apps will get deployed on end-user devices as fully self-contained natively compiled code (when .NET Native enters production), and will not have a dependency on the .NET Framework on the target device/machine
No; this is not directly possible. In particular, C++ templates are not supported by C#.

Categories

Resources