22

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.

SammyG
  • 274
  • 1
  • 10

1 Answers1

1

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://learn.microsoft.com/en-us/dotnet/framework/interop/default-marshaling-for-strings

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

  • `C# does not allow for unmanaged memory allocation`. Did you ever hear of [Marshal.AllocHGlobal](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.marshal.allochglobal?redirectedfrom=MSDN&view=netcore-3.1)? – Julian Kirsch Sep 12 '20 at 03:31
  • It looks like System.Runtime.InteropServices Namespace that has the Marshal class came out in 2015, five years ago, just in time for me to already be done with my project and not to be able to benefit from it, ;) But, yeah, that's why it's called Marshal, it's clearly for Marshalling unmanaged to managed. Appears to replace, or at least be a cleaner alternative to, the PInvokes. Looks like there's also the StringBuilder class to try. – Gaming Impteraix Sep 12 '20 at 03:47