Search Unity

How to defeat MethodAccessException in Unity?

Discussion in 'Scripting' started by ElTotoro, Jun 15, 2018.

  1. ElTotoro

    ElTotoro

    Joined:
    Dec 14, 2013
    Posts:
    4
    Hello everyone!

    I wrote a small code to initialize the ICS under Windows. But alas, I ran into one problem...
    when I execute the line:
    netSharingManager = Activator.CreateInstance (type);

    - an exception occurs:
    MethodAccessException: Method `System.__ComObject..ctor()' is inaccessible from method `System.__ComObject..ctor()'
    System.__ComObject..ctor () (at <e392dcd9fc5b44f3b60850d757273e30>:0)
    (wrapper cominterop-invoke) System.__ComObject..ctor()
    System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) (at <e1a80661d61443feb3dbdaac88eeb776>:0)
    Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
    System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) (at <e1a80661d61443feb3dbdaac88eeb776>:0)
    System.RuntimeType.CreateInstanceMono (System.Boolean nonPublic) (at <e1a80661d61443feb3dbdaac88eeb776>:0)
    System.RuntimeType.CreateInstanceSlow (System.Boolean publicOnly, System.Boolean skipCheckThis, System.Boolean fillCache, System.Threading.StackCrawlMark& stackMark) (at <e1a80661d61443feb3dbdaac88eeb776>:0)
    System.RuntimeType.CreateInstanceDefaultCtor (System.Boolean publicOnly, System.Boolean skipCheckThis, System.Boolean fillCache, System.Threading.StackCrawlMark& stackMark) (at <e1a80661d61443feb3dbdaac88eeb776>:0)
    System.Activator.CreateInstance (System.Type type, System.Boolean nonPublic) (at <e1a80661d61443feb3dbdaac88eeb776>:0)
    System.Activator.CreateInstance (System.Type type) (at <e1a80661d61443feb3dbdaac88eeb776>:0)
    AnotherWifi.Hotspot.Init () (at Assets/Scripts/NewHotSpot/Hotspot.cs:20)



    Current program code:
    Code (CSharp):
    1. public class Hotspot
    2. {
    3.     [DllImport("ole32.dll")]
    4.     static extern int CLSIDFromProgID([MarshalAs(UnmanagedType.LPWStr)] string
    5.         lpszProgID, out Guid pclsid);
    6.      
    7.     private dynamic netSharingManager = null;
    8.      
    9.     public Hotspot()
    10.     {
    11.         Init();
    12.     }
    13.  
    14.     private void Init()
    15.     {
    16.         Guid g = Guid.Empty;
    17.         CLSIDFromProgID("HNetCfg.HNetShare.1", out g);
    18.      
    19.         var type = Type.GetTypeFromCLSID(g);
    20.         netSharingManager = Activator.CreateInstance(type);
    21.     }
    22. }
    I could not find a solution to this problem. Can anyone know how to deal with this?
    Thank you!