Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question The application called an interface that was marshalled for a different thread

Discussion in 'Windows' started by rekatha, May 25, 2021.

  1. rekatha

    rekatha

    Joined:
    Dec 18, 2017
    Posts:
    22
    Hello
    I build UWP with Facebook Login feature. I use winsdkfb.dll for Facebook Login feature. When I click Login game throw error

    The application called an interface that was marshalled for a different thread


    Here is my code for login button using winsdkfb.dll
    Code (CSharp):
    1. void Start()
    2.     {
    3.         btnLogin.onClick.AddListener(() => FacebookLogin());
    4.     }
    5.  
    6. public void FacebookLogin()
    7.     {
    8. #if UNITY_WSA && ENABLE_WINMD_SUPPORT
    9.         FBSession sess = FBSession.ActiveSession;
    10.         sess.FBAppId = "xxxxxxxxxxxxxxxx";
    11.         sess.WinAppId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    12.         List<string> permissionList = new List<string>();
    13.         permissionList.Add("public_profile");
    14.         permissionList.Add("user_friends");
    15.         permissionList.Add("user_likes");
    16.         permissionList.Add("user_location");
    17.         permissionList.Add("user_photos");
    18.         permissionList.Add("publish_actions");
    19.         FBPermissions permissions = new FBPermissions(permissionList);
    20.  
    21.         var result = await sess.LoginAsync(permissions);
    22. #endif
    23. }
    What I understand is (CMIIW) uwp unity hase two thread. Essentially the 'UI thread' is the main thread from the point of view of the WinRT/UWP runtime. In order not to block that thread, all Unity-specific code - MonoBehaviour scripts, coroutines, etc - runs on a separate thread, the 'application thread,' which is the main thread from the point of view of the Unity engine. So possibly that error because implement of winsdkfb.dll need to be run on 'UI Thread'. I try to put implementation of winsdkfb in
    InvokeOnUIThread


    Code (CSharp):
    1.  
    2. void Start()
    3.     {
    4.         btnLogin.onClick.AddListener(() => FacebookLogin());
    5.     }
    6. public void FacebookLogin()
    7.     {
    8. #if UNITY_WSA && ENABLE_WINMD_SUPPORT
    9.         UnityEngine.WSA.Application.InvokeOnUIThread(() => OnFacebookLogin(),
    10.                 waitUntilDone: true);
    11. #endif
    12. }
    13.  
    14. #if UNITY_WSA && ENABLE_WINMD_SUPPORT
    15.     async private void OnFacebookLogin()
    16.     {
    17.         FBSession sess = FBSession.ActiveSession;
    18.         sess.FBAppId = "xxxxxxxxxxxxxxxx";
    19.         sess.WinAppId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    20.         List<string> permissionList = new List<string>();
    21.         permissionList.Add("public_profile");
    22.         permissionList.Add("user_friends");
    23.         permissionList.Add("user_likes");
    24.         permissionList.Add("user_location");
    25.         permissionList.Add("user_photos");
    26.         permissionList.Add("publish_actions");
    27.         FBPermissions permissions = new FBPermissions(permissionList);
    28.  
    29.         var result = await sess.LoginAsync(permissions);
    30. }
    31. #endif
    32.  
    But the issue is not solved.
     
    Last edited: May 26, 2021
  2. timke

    timke

    Joined:
    Nov 30, 2017
    Posts:
    407
    Your understanding of UWP threading (in Unity) is correct and generally invoking APIs on the UI thread will address these threading errors.

    In this case, it appears you're using MediaCapture, or at least the FaceBook API is using it. MediaCapture has an extra catch: it's not an "Agile" API and all of it's functions must be invoked on the same thread it was created on. Note that I'm just going off on the name of your method
    CaptureImageByMediaCapture 
    and don't know how MediaCapture is actually being used here.

    To fix the problem:
    • Make sure MediaCapture (somehow) is initialized on the UI thread
    • All calls that invoke MediaCapture APIs (directly or indirectly) are made from the UI thread
     
  3. rekatha

    rekatha

    Joined:
    Dec 18, 2017
    Posts:
    22
    Sorry for the naming, it should be
    OnFacebookLogin
    , not
    CaptureImageByMediaCapture

    Let's call it
    OnFacebookLogin
    from now on. (I just edited it)

    Doesn't
    UnityEngine.WSA.Application.InvokeOnUIThread
    makes my
    OnFacebookLogin
    method runs on the UI thread ?
     
  4. timke

    timke

    Joined:
    Nov 30, 2017
    Posts:
    407
    Yes, the code block will run on the UI thread and the
    LoginAsync
    should also run on the UI thread. By default C# await schedules the code to run on the same thread (context) as the invocation.

    In general though, the
    The application called an interface that was marshalled for a different thread
    does mean you're trying to call a non-Agile WinRT method from a different thread than the object was created on.

    So something within winsdkfb.dll is using a non-Agile API (MediaCapture isn't the only one).
     
  5. timke

    timke

    Joined:
    Nov 30, 2017
    Posts:
    407
    I found the GitHub repo for winsdkfb.dll and briefly looked through the sample code, but unfortunately nothing stands out to why you're still getting the error. LoginAsync simply spawns a bunch of other tasks and task chains to perform the authentication and login operations.

    Can you debug the script and/or IL2CPP to see where exactly this error is coming from? The LoginAsync may just be a red herring.
     
  6. rekatha

    rekatha

    Joined:
    Dec 18, 2017
    Posts:
    22
    Hello, sorry, I miss read the log after I use .
    UnityEngine.WSA.Application.InvokeOnUIThread

    at first yes I got this error
    The application called an interface that was marshalled for a different thread


    but after I use
    UnityEngine.WSA.Application.InvokeOnUIThread
    , I got different error :
    upload_2021-5-27_9-51-37.png

    Does this mean I can't acces
    ActiveSession 
    from
    FBSession
    ?
     
  7. timke

    timke

    Joined:
    Nov 30, 2017
    Posts:
    407
    Something is wrong with the integration of the native library with Unity; basically it looks like it's calling an invalid native address for ActiveSession. From GitHub code, it should be executing this C++/CX code to initialize and return FBSession object:


    static property FBSession^ ActiveSession
    {
    FBSession^ get()
    {
    static FBSession^ activeFBSession = ref new FBSession();
    return activeFBSession;
    }
    }


    Are you building this DLL yourself or using pre-built versions?
    Are you sure you're matching the CPU architecture (x64, x86, etc.) of the DLL with Unity's build?
    Does this library work in a non-Unity project, i.e. a simple UWP app from Visual Studio?

    BTW you're not the first to have trouble integrating this library with Unity, several others have reported (different) issues on the Unity forums in the past:

    https://forum.unity.com/threads/winsdkfb-integration-for-windows-10-universal.427086/
    https://forum.unity.com/threads/typ...ted-windows-runtime-is-not-registered.346754/
    https://forum.unity.com/threads/how-to-use-fbwinsdk-in-unity.381892/

    AFAIK nobody has succeeded in getting this library to work in Unity. Since the code is old and the GitHub repo is "archived" there's probably no support for it right now. I recommend finding another FB plugin.
     
  8. rekatha

    rekatha

    Joined:
    Dec 18, 2017
    Posts:
    22
    Are you building this DLL yourself or using pre-built versions?

    No, I download it from https://www.nuget.org/packages/winsdkfb/

    Are you sure you're matching the CPU architecture (x64, x86, etc.) of the DLL with Unity's build?

    Yes

    Does this library work in a non-Unity project, i.e. a simple UWP app from Visual Studio?

    Yes, it works.

    BTW you're not the first to have trouble integrating this library with Unity

    I know, I ask about how to integrating this lib in this thread https://forum.unity.com/threads/winsdkfb-cant-be-load-in-unity.1114243/#post-7174543. I though it was succesfull just because no compile error or crash at runtime. Not trying login at that time. After Im trying to login and got error, I made this thread.

    Sadly I can't find any FB Plugin for UWP except this one. There is https://www.nuget.org/packages/Microsoft.Toolkit.Uwp.Services/7.0.0-preview2 but this library dependen to this winsdkfb library.
     
  9. timke

    timke

    Joined:
    Nov 30, 2017
    Posts:
    407
    Unfortunately, I'm out of ideas on this problem, and I'd have to debug it myself to figure out why it's failing (unfortunately don't have time for that right now).

    One last suggestion: build your Unity project as a XAML app (instead of D3D) and use the XAML controls/code for logging in and managing the FB session. Since XAML always runs on the UI thread, this would hopefully sidestep the problem in Unity. This probably isn't what you'd like for your app but it could help get things working.
     
  10. rekatha

    rekatha

    Joined:
    Dec 18, 2017
    Posts:
    22
    Thank you for your support and ideas.
    I'll share to you if I can make this work.

    Wish me luck!