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# ...
Related
I recently developed native extensions for mobile projects on Adobe AIR (Android and iOS).
I want to port these ANEs for desktop projects on Windows and OSX. The OSX part is not a problem because it uses the same mechanisms as for iOS. The problem is essentially on the Windows side.
Adobe AIR offers bridges to write the native part in C / C ++, I prefer to use C# to simplify the task and access more simply .NET libraries.
Has anyone ever heard of experiments or viable projects to code a native Adobe AIR extension with C#?
I have not found a complete solution to achieve this:
Create a DLL in C # including access to FlashRuntimeExtensions.h (C Header file)
Be able to use .NET libraries from this unique DLL
Produce only one DLL file
Do not use the flash.desktop.NativeProcess library
Thank you for your help or a different point of view on these issues.
According to Extending AIR, you just need access to any function on your DLL regardless what native method you used to create it.
This means that you still need to use a C++ project to link AIR to your native library but the main code can be done on C#. So you export your main code/logic into a DLL from C# and then use the C++ bridge project to Link both DLL and flash.
This link could also be useful : Windows ANE - tutorial introduction
A developer sent me this link to TUARUA's FreSharp GitHub page.
It corresponds exactly to the subject of my question. I share it so that everyone can see how to create an ANE (AIR Native Extension) from C# under Windows.
So I will be able to resume my development and port my libraries for desktop computers.
Thanks to everyone.
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.
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.
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.
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()