Consuming a DLL(Universal Apps) from a WinRT Component - c#

I have a C++ (Native code) DLL project developed for iOS and Android. I would like to port it to a C++ DLL (Universal Apps) to be consumed by a C# Universal Store Application. The code isn't HW dependent.
As a first step, before moving all the code, I created a small test solution as follows:
I created a C++ DLL (Universal Apps), myDll, that has a C++ Add1(int, int) function.
I created a C++ WinRT component (Universal Apps) that has C++ Add2(int, int) function.
I created a C# Universal Application, myApp, that calls Add2 which calls Add1.
Compilation passes OK, however when I run myApp the application crashes and report that myDll wasn't
loaded.
My questions are:
Is the scenario I described above possible? And If so, what can be the problem causing myApp to crash?
Is there a better way for me to port the iOS/Android C++ code to be consumed in a C# Universal Application?
Thx

1) Like Hans, my first guess is that you're not including the Dll in the apps package. If it's not deployed in the package it isn't available to be loaded. Since you can't add a reference to the DLL you'll need to add it explicitly:
Add the files to the project, open the files' properties in the Solution Explorer window, and mark them as content to be included in the app package.
Check that out is actually in the appx dir after you deploy.
2) That's probably the easiest. You could also include just the Dll and pinvoke. Either way you'll need to make sure the dll is valid for Windows Store apps

Related

How does Xamarin launch your Android project?

Android projects run natively on Java, and NDK projects basically work by having a special Activity that launches your native code. For example, SDL has an Activity that initializes SDL, sets up a display window, and then launches your project.
I'm trying to see how Xamarin does it. Technically, CLR code is managed code rather than native, but the CLR itself is native code and the whole thing would look like an NDK project to the Android runtime. I've been looking around, but I can't find where the Android project launcher is in Xamarin, and Googling it turns up nothing useful.
What is the mechanism by which Xamarin launches your CLR project on an Android device?
What is the mechanism by which Xamarin launches your CLR project on an Android device?
You can refer to Application Startup.
When an app is launched in Android device, Android will load it by specified #android:name in manifest, then usually all types will be instantiated by invoking ContentProvider.attachinfo() method, Xamarin.Android then here adding a mono.MonoRuntimeProvider ContentProvider during the build process. The mono.MonoRuntimeProvider.attachInfo() method will then replace the native ContentProvider.attachinfo() method to load Mono runtime into the process.
This is how I understand that document, please advise me if there is any mistake, many thanks.

How do I create a Windows desktop app from UWP code

I have an existing project for Windows 10 (UWP).
It creates an app that can go into Windows Store and be sideloaded, and that works fine.
... but now I would like to create a desktop-version (an exe-file) from the same code base (to avoid sideload-security issues in local network).
I created a VS 2015 4.5.2 WPF project and started to reference the components (dll's) used in the UWP project. But the importer states that some of the components depends on .Net-Core, and cannot be referenced.
So: Is it possible to mix .NET-Core and .NET-Framework components in the same WPF project?
Or: Can I create a .Net-Core console-app, create the used windows, use XAML, and export an exe (with dependencies) that can run standalone?
Thx!
Unfortunately what you are tying to achieve is not possible.
UWP and WPF are different stacks and unless you are using PCLs code cannot be shared between them.
Even then, the XAML layer is different and not compatible from one stack to another.
So no, you cannot reuse your code in a WPF application
As for the other solution, you cannot have your UWP app run as an .exe because Universal Apps run a different Application Model called appx which is fundamentally different than the exe application model.
There are ways to wrap an exe application in an appx (See the Desktop App Converter) but there is no converter/repackager to take you from appx to exe.

where do include all additional unmanaged dependency dll in windows phone app

i want to import some C++ unmanaged dlls into my windows phone app. can i do this ?? while debugging the dll not found exception thrown. can anyone help me to where do i place my those dependency dlls .
thank you ...
It depends on the DLL. If the DLL is built using only API available to Windows Runtime apps then you can use it. See Win32 and COM for Windows Runtime apps on MSDN for a list of the available API. You won't be able to use a DLL built for a desktop system (you cannot run x86 or x64 on ARM) but will need a specific phone build.
To use it you will need to include it in your appx package. In Visual Studio's Solution Explorer add the DLL to your project and edit its properties to mark its Build Action as Content. When you build the app check to make sure it is in the appx directory. That will deploy the DLL with the app so it is available at runtime.
For me it was due to having WIC code in my App and/or calling CoCreateInstance in a windows phone environment ( on PC it works flawlessly though )

Make a .net plugin for MFC application?

I need to write a plug-in for a MFC application:
- plug-in is deployed as dll.
- On the runtime, the MFC application will call the plugin and the plugin shows a custom dialog.
- the dll needs to be dynamically loaded, the MFC application should not be re-complied if the dll is updated.
I mainly code in .net and after playing with MFC, everything just seems too odd for me (no events/delegates etc). I'm fine to write a MFC dll but if I can do that in .net then I'll willing to give it a try. A possible solution is that I can write a C# dll and expose it as COM. Since I don't know anything about COM, here are my questions:
- Does the .net framework needs to be installed on client's machine?
- Can COM be dynamically loaded from a MFC application?
- I read something about registering the assembly with COM, does this happen on client's machine? (if yes, then the installer might need to be updated)
- Is it easy to use COM in a MFC application (this requires MFC devs to modify their code)
Or, given if there is too much effort to make the connection between .net, COM and MFC, would you suggest me to code in MFC? Thanks.
UPDATE:
I decide to use C++/CLI as a bridge between MFC and .Net. A mixed C++/CLI dll can be called from MFC application and the dll can utilize .net framework. Thus I can code the UI with C# in another dll and that dll can be called by C++/CLI.
References:
Native and .NET Interoperability
Integrate Windows Forms Into Your MFC Applications Through C++ Interop
Does the .net framework needs to be installed on client's machine?
Yes the frame work must be installed on the client's machine
Can COM be dynamically loaded from a MFC application?
It can, the MFC application will load your DLL dynamically, it can call it without being updated as long as your class's public interface doesn't change
I read something about registering the assembly with COM, does this happen on client's machine? (if yes, then the installer might need to be updated)
Yes the .NET COM object (or any COM object for that matter) must be registered on the client's machine. You can use the building setup project in Visual Studio do to that
Is it easy to use COM in a MFC application (this requires MFC devs to modify their code)
It should be reasonably easy, but that's more a question for the MFC dev's
Would you suggest me to code in MFC
Do you know C++ or MFC? If not it's probably a lot more work.
Another option would be to write the component in C++/CLR. It can expose unmanned functions/classes which would be directly callable from the MFC application.

Cannot access a Linux gcc compiled .so shared library from Windows Mono C# Project

Here's the setup:
I've got a shared library (c++ with extern "C" methods) I've compiled in linux and created a library.so file.
I've used Mono Develop on the same box (Ubuntu) and was able to DLLImport("library.so") and access my extern functions no problem.
The problem comes in when I copied that .so file to a windows machine (Win7) and I try to do the same thing, but this time running Mono under windows with MonoDevelop.
I get a System.BadImageFormatException. I've tried doing a "./" before the library.so file, but nothing helps. I've checked and double checked that it's looking at the right directory and it is.
Is there something big I'm missing why I can't access this .so file under Windows/Mono?
You can't use a .so elf binary on Windows for your native code. You need to recompile it into a native binary supported by Windows (namely a .dll).
I suggest you read our wiki page about cross platform interop between managed an unmanaged code.
The problem is that the Linux SO is not a valid windows image. You will need to compile the C++ code under windows as a DLL and export the method so that you can call it using p/Invoke.
C/C++ is portable (mostly) at the source level, not at the binary level, so you cannot copy a Linux .so to Windows and expect that to be executable on the Windows platform. I am sure this raises the question, why can you copy your .NET/Mono dlls and exes from window to Linux and have them run, that is because the Mono CLI implmentation has code which is able to load the IL from the Windows image (PE) and then execute the loaded IL in the VM, so in this case it is not the native binary code that is being executed, but just the platform idependent IL which is loaded from the DLL/EXE container file.

Categories

Resources