Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Windows 10 functions from Unity IL2CPP project via a class library

Tags:

I have been trying to create a UWP class library that gives me access to Windows 10s native features such as Windows.Security.Authentication.OnlineId. I would like to get a username and ID from the device for use in a Unity UWP IL2CPP project. I am currently able to do this with Unity's built in social class for ios and there is code which google has written that allows this to work seamlessly with the same class but for android's Google Play Games.

I've downloaded a sample off github (https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/WebAccountManagement) which demonstrates how to call the relevant classes and functions in a UWP app and works well but the samples appear to be are accessing classes from the "Windows.Foundation.UniversalApiContract" class.

I can't seem to find a way to add this to a basic UWP class library so I can call on the required classes such as Windows::Security::Credentials::WebAccountProvider.

The best I've been able to do is create a basic function in the class library that returns a small hardcoded string just to test if the concept was remotely possible. :-

extern "C" __declspec(dllexport) wchar_t* __stdcall GetMyString() {     wchar_t* myString = L"Guuuper";     auto resultBufferLength = wcslen(myString) + 1;     wchar_t* result = static_cast<wchar_t*>(CoTaskMemAlloc(resultBufferLength * sizeof(wchar_t)));     wcscpy_s(result, resultBufferLength, myString);     return result;  } 

My whole journey in attempting to do that can be found here:- http://forum.unity3d.com/threads/returning-c-string-to-il2cpp-windows-store-project.395284/

I've been able to successfully call this code from within unity via a UWP build but my main question is how I would go about adding the appropriate references or how I would create this class library to access the WebAccountProvider class?

Any help would be much appreciated

Update: I have asked the MS team at their own site about this challenge and they appear to be working on a solution.

like image 937
SammyG Avatar asked Jul 18 '16 19:07

SammyG


People also ask

Is IL2CPP faster?

In Unity 2021.2, IL2CPP enhances calls to this method, so that they occur more than 100 times faster.

What is il2ccp?

The IL2CPP scripting backend converts IL code from scripts and assemblies in a Unity project to C++ code which is then compiled using platform native compilers. For the most part, the scripting backend should not matter. However, IL2CPP does provide a few useful options which can be controlled.

How does IL2CPP work?

The il2cpp.exe utility accepts managed assemblies compiled with the Mono compiler that ships with Unity and generates C++ code which we pass on to a platform-specific C++ compiler. The other part of the IL2CPP technology is a runtime library to support the virtual machine.

Why IL2CPP?

IL2CPP (Intermediate Language To C++) is a Unity-developed scripting backend which you can use as an alternative to Mono when building projects for various platforms. IL2CPP (An ahead-of-time (AOT) compiler) supports debugging of managed code in the same way as the Mono scripting backend.


1 Answers

Apologies for 'answering' and not 'commenting' but I'm still a new contributor.

My initial first instinct is to suspect that your problem isn't with Unity3D; it's with C# and the entire programming environment inside Unity3D. C# does not allow for unmanaged memory allocation. There's an entire art of getting C++ libraries to work in C#, and it's called "Marshaling" code, and there's an entire industry based around marshaling plugins from C++ to Unity3D as a result.

The reason you're not getting a string, is because you're literally sending and receiving a pointer to a single character.

Unfortunately, my single experience marshaling C++ code for Unity was five years ago and I'm a little rusty on what my solutions were. What I do remember is that the most 'hacky' but obvious solution was to work out the maximum size of string that could possibly be passed, and, on both sides of the divide, pass and receive strings of that predefined size.

https://docs.microsoft.com/en-us/dotnet/framework/interop/default-marshaling-for-strings

Let us know if that sends you in the right direction.

like image 59
Gaming Impteraix Avatar answered Sep 21 '22 04:09

Gaming Impteraix