Could not add reference to WinRT C++ DLL project - c#

THE SCENARIO:
I am making a [edit: native WinRT] C++ Direct X DLL project for Windows Store apps based on the "Direct3D App" Visual Studio code sample.
This must be a separate DLL project from my Windows Store App project because I will be using my XAML UserControl with Direct X renderer inside it both in the Windows 8 Metro app and a Windows Phone version of the app.
THE PROBLEM:
I cannot reference the C++ project in the C# Windows Store app project. Visual Studio refuses with the following words:
A reference to <project-name> could not be added.
I've seen suggestions to compile the C++ project using the /clr flag, but this is not compatible with:
the /ZW flag required by some API within the project
the "wrl.h" header
a multitude of other command-line flags in the project settings
So the /clr flag does not appear to be an option in this situation.
THE QUESTION:
How can I reference the C++ project so that I can use my XAML Direct X renderer within the C# Windows Store App project? Or is there a better alternative?
Thanks in advance.

I solved the problem by re-creating the C++ project as a Windows Runtime Component project. I had originally created the project from the Class Library (Windows Store apps) template. It must be that the Win RT template has some configuration settings which enable it to work.
After making this change, the project could be added as a reference in the C# project.
It is because of #user1610015's answer that I realised I was actually developing native Win RT code, not managed code, which lead me to try this solution - thanks!

The /ZW flag is for C++/CX projects. C++/CX is almost identical in syntax to managed C++ (C++/CLI), except that C++/CX is completely unmanaged. This may be what made you think you were developing managed C++, when in fact you weren't.
I haven't used C++/CX, but from what I've heard, Windows Runtime classes are COM classes. So I think you can use the TlbImp.exe tool from the Windows SDK to create a managed DLL from your unmanaged DLL, which you can then reference from your C# project. Try it and let me know if it works.
Another option is to talk to the Windows Runtime without C++/CX. While C++/CX makes Windows Runtime programming a lot easier, it's not strictly necessary. You can also use the WRL library. Since the WRL library is pure C++, it should also be usable from C++/CLI.

Related

Create A DLL to work on a solution targeting Master ARM64? (Hololens 2)

I develop applications targeting the Hololens 2 platform (mixed reality).
The general idea of the application I am trying to build is using a point cloud registration algorithm, which targets a previously scanned model on one hand and the visible point cloud generated by the Hololens' SceneUnderstanding ability on the other.
My main problem is that all the different implementations that I found of the algorithm are using libraries (DLLs) that:
Uses a lower-level language (C++) than the C# scripting language used in Unity's scripts.
Are not targeting the needed configuration and architecture needed for deploying an application on the Hololens 2 (Master configuration, ARM64 architecture).
In the process of trying to generate that kind of library myself (using visual studio 2019 to build "Dynamic-Link Library with Exports [DLL]"),
I created the simplest library in C++ - A single function that returns an integer (without the dependency of any other library). Obviously, when targeting the Unity Editor application it was easy to use the library's function from the C# script attached to the Unity project, but when I try to convert the solution's configuration and architecture to Master ARM64, building a Visual Studio solution and deploying the application on the Hololens 2 generates a script error: "Unable to load DLL '<dll_name>': The specified module could not be found."
I can think of two major reasons that will cause this problem:
The program can not find the library needed for function execution,
JUST BECAUSE the location of the library is not correct (I have NO IDEA
where the library should be placed in the solution hierarchy).
I am probably not using the right technique for converting the library to the correct target solution properties (in addition of the previous scenario)
(and as well, I have no idea what is the correct procedure for achieving correct targeting properties)
Using: Visual Studio 2019, Unity 2019.4.1f1, Hololens 2nd generation
It would mean a lot to hear from anyone who have encountered any sort of similar problem, thank you in advance!
To add a custom DLL to a Unity project for HoloLens 2, you have to copy the DLL to a folder called Assets/Plugins/WSAPlayer/ARM64 in your project hierarchy in Unity. Then, you should also make sure that the correct Platform settings are specified for the Plugin in the Inspector, i.e., SDK should be set to UWP and CPU to ARM64:
This git repo has an example of this (DLL and Unity project).
Note that the DLL has to be compatible with Universal Windows Platform (UWP), so when creating a new library with Visual Studio, you should select either the DLL (Universal Windows) or Windows Runtime Component project templates as your starting point.
If you add any dependencies to your DLL, e.g., for point cloud registration, you will also have to build those libraries for UWP / ARM64, and add those DLLs to the same folder as well!

How to use a C++ dll in Windows Phone 8.1 XAML App within C++ Runtime Component

I want to use a C++ dll (compiled for arm, x86) within my C# windows phone 8.1 XAML visual studio project.
I created a C++ runtime component to interact with my C++ code. This code needs a precompiled dll. I added my lib file to my project settings. The compilation is running fine.
If I start my app with the windows phone 8.1 emulator I get:
The specified module could not be found. (Exception from HRESULT: 0x8007007E)
My Visual Studio Project Structure:
c#
Windows Phone Project
Windows Project
Shared XAML
Managed C++
RuntimeComponent Windows Phone
RuntimeComponent Windows
Shared C++ Code (dll used here)
Things I have tried:
I dragged and dropped the dll into my C++ project and changed properties of content to true.
I dragged and dropped the dll into my C# project and enabled the option to copy the dll to the output directory
So, how can I use a .lib/.dll library within a C++ Runtime Component Project?
Some more details:
I want to use Angle (which allows OpenGL Code to be executed as DirectX on Windows/Windows Phone with the Microsoft fork: https://github.com/MSOpenTech/angle/tree/future-dev)
There are templates provided for Windows Phone 8.1 for pure C++ based on libEGL.lib/dll and libGLES.lib/dll (manually compiled for arm, win32)
But I want to use a C# project with C++ Runntime Component to be able to use third party libs like the admob ad framework for Windows Phone
I found out that cocos2d is using Angle but I have not figured out how they did it (https://github.com/cocos2d/cocos2d-x/tree/v3/templates/cpp-template-default/proj.wp8-xaml)
I managed to get it working.
You have to import the dll into the C#-project. It's important that the dll is in the root the C#-project and that: copy to output directory is not activated.

Consume C# dlls in C++ for Windows Phone 8

I have a windows phone 8 app created with Visual Studio 2012 - "Windows Phone Direct3D App (Native only)" project under Visual C++, and a library created with "Windows Phone Class Library" project under Visual C# group having target platform "Windows Phone OS 8.0". All I need is to consume the C# dll in the C++ code. The restriction is that I cannot change the architecture of the application, so the main entry-point must be in the C++ project, and from here to call somehow the code written in C#.
If I try to reference the C# dll in C++ app I get this error
a reference to [dllname] cannot be added because the two projects
target different runtimes.
Based on http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj714080(v=vs.105).aspx , I tried also to reference the C# dll indirectly through a Visual C++ "Windows Phone Runtime Component" but again I get the same error when I try to add reference to the runtime component. I tried to change some project settings and nothing worked.
In all samples and demos which I found on the web, for WP8, the main entry-point is always in C#. Only some hints regarding windows store apps seem to indicate that could be possible to consume C# from C++, but I don't find any reference saying explicitly that my scenario on wp8 is possible or impossible.
Thanks in advance to anybody which shares the solution or any suggestion.
Late answer, but the short story is that you can't. In Windows 8 C# WinRT dlls are full WinRT dlls that can then be consumed by any framework (WinJS, Native etc.) but not so for the phone.
On the phone C# can consume C++ WinRT objects (from C++ dlls), but not vice versa. You can create trickery by having your C# dll call into your C++ dll and pass around function pointers, but that's about it.
I've liked this: http://www.developer.nokia.com/Community/Wiki/C%2B%2B_support_from_Windows_Phone_8 . You may inject an interface into the .winmd C++ side metadata, that could be referenced and implemented in C#. Much cleaner than using delegates ... Still have to be initiated from C# ...

Problems referencing Facebook SDK C# WP8 from C++ Windows Phone 8 app

I try to reference facebook.dll - wp8 branch - within an app of type "Windows Phone Direct3D App (Native only)" project under Visual C++ VS2012, and I get the error:
a reference to [dllname] cannot be added because the two projects
target different runtimes.
The restriction is that I cannot change the architecture of the application, so the main entry-point must be in the C++ project, and from here to call somehow the code written in C# for facebook.
Based on http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj714080(v=vs.105).aspx , I tried also to reference the C# dll indirectly through a Visual C++ "Windows Phone Runtime Component" but again I get the same error. I tried to change some project settings and nothing worked. Trying to consume C# dll in C++, in all samples and demos which I found on the web, for wp8, the main entry-point is always in C#. Only some hints regarding windows store apps seem to indicate that could be possible to consume C# from C++, but I don't find any reference saying explicitly that my scenario on wp8 is possible or impossible.
How can I use facebook SDK from my WP8 app? Or what options I have?
Thanks in advance to anybody which shares the solution or any suggestion
C++ projects can't just invoke any C# DLL in WP8. You'll have to change how the class is built. Start a new C# XAML + C++ D3D project and have a look at IDrawingSurfaceManipulationHandler and Direct3DInterop. Basically you'll have to recreate and recompile the Facebook C# SDK to use it in WP8 C++.
However, installing and using the Facebook SDK on WP8 C# projects works OK on my machine. Have you considered usign a mixed C# and C++ project? It does have a small perf hit, but as long as you use DrawingSurfaceBackgroundGrid you should still have relatively good perf.

WinRT and DLLs: What exactly am I allowed to use?

It's not all together clear to me, so I am hoping someone here knows the answer. I am using the C# Json.Net and the C# HtmlAgilityPack both built from sources for "Any CPU". I've noticed that both of these include using directives that reference assemblies that don't seem to be available in Metro yet they both build fine.
If I reference them, does this mean my app will thunk to unmanaged code?
What exactly causes thunking to unmanged code?
Why are DLLs allowed to reference assemblies that are not available to WinRT and my app is not?
Will this fail the windows store approval process?
What are the general rules for DLLs I include with my app that I need to worry about?
Thanks in advance!
When developing apps that target WinRT, the app should only use the API's available in Windows 8 WinRT. The API's are defined in WinMD files. If the app used the desktop API's, then it will be rejected during the Windows store approval process.
Imagine this scenaio. You developed a WinRT app that uses .NET DLL's from desktop mode (client profile). The app is deployed in Windows store and user downloads it on ARM tablet. The application will crash as it does not have the desktop (client) .NET DLL's on the ARM devices.
so this brings us to the question on what .NET classes we can use while developing apps for WinRT. The article http://kishore1021.wordpress.com/2012/08/06/what-is-portable-class-library-project-in-visual-studio-2012-net-4-5/ lists the .NET classes available for WinRT application development.
Change all open source projects dependencies to target WinRT. See if some methods / classes in WinRT are missing so the code does not compile. Try to find the alternative methods/classes.
Such as List.FindAll(), change to Linq Where()

Categories