# System.AccessViolationException on Initialize

**URL:** <https://devforum.zoom.us/t/system-accessviolationexception-on-initialize/49138>\
**Category:** Windows\
**Created:** [April 28, 2021, 11:05pm UTC](https://devforum.zoom.us/t/system-accessviolationexception-on-initialize/49138 "2021-04-28T23:05:53Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![tyler](https://avatars.discourse-cdn.com/v4/letter/t/35a633/32.png) [@tyler](https://devforum.zoom.us/u/tyler)\
**Post date:** [April 28, 2021, 11:05pm UTC](https://devforum.zoom.us/t/system-accessviolationexception-on-initialize/49138/1 "2021-04-28T23:05:53Z")

</div>

**Description**  
I’m trying to use the Zoom Client SDK in a .NET 4.7.2 Web API. I’m trying to setup my wrapper as a Singleton so I’m instantiating it in the `WebApiConfig.Register` method but even here it won’t work (instantiating it inside the controller methods created a similar Exception).

Here is my code:

```auto
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            DebugLogService debugService = new DebugLogService();
            ZoomService zoomService = new ZoomService(debugService);
            Task<bool> initializeZoom = zoomService.InitializationAsync("...", "...");
            initializeZoom.Wait();
            bool result = initializeZoom.Result;
            debugService.Log($"result = {result}");
        }
    }

```

ZoomService.cs:

```auto
...
        public async Task<bool> InitializationAsync(string appKey, string appSecret)
        {
            try
            {
                var sdkInitialized = SdkInitialize();
                if (!sdkInitialized) return false;

                Initialized = await SdkAuthenticationAsync(appKey, appSecret);
            }
            catch (Exception)
            {
                Initialized = false;
            }

            return Initialized;
        }
...
        private bool SdkInitialize()
        {
            InitParam param = new InitParam
            {
                web_domain = "https://zoom.us",
                enable_log = true,
                
            };

            // This line throws System.AccessViolationException
            SDKError error = CZoomSDKeDotNetWrap.Instance.Initialize(param);

            return error == SDKError.SDKERR_SUCCESS;
        }

```

I’m assuming something is wrong in my setup, but I copied most of this from an existing working WPF solution. Any advice would be helpful.

**Which Windows Client SDK version?**  
5.5.12509.0330  
Assembly zoom\_sdk\_dotnet\_wrap, Version=1.0.7575.37543

**Device (please complete the following information):**  
Windows 10

---

<div class="post-metadata">

**Author:** ![Michael\_Condon](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/michael_condon/32/13543_2.png) [@Michael\_Condon](https://devforum.zoom.us/u/Michael_Condon)\
**Post date:** [April 29, 2021, 6:31pm UTC](https://devforum.zoom.us/t/system-accessviolationexception-on-initialize/49138/2 "2021-04-29T18:31:54Z")

</div>

Hey @tyler,

Thanks for using the dev forum!

Support for this is a little limited, but I can try and help. Is it possible you just need to add “ZOOM\_SDK\_DOTNET\_WRAP.” before the InitParam and the _CZoomSDKeDotNetWrap_?

Thanks!  
Michael

---

<div class="post-metadata">

**Author:** ![tyler](https://avatars.discourse-cdn.com/v4/letter/t/35a633/32.png) [@tyler](https://devforum.zoom.us/u/tyler)\
**Post date:** [April 30, 2021, 2:17pm UTC](https://devforum.zoom.us/t/system-accessviolationexception-on-initialize/49138/3 "2021-04-30T14:17:12Z")

</div>

I simplified my code to just this as requested

```auto
        public static async void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            ZOOM_SDK_DOTNET_WRAP.InitParam param = new ZOOM_SDK_DOTNET_WRAP.InitParam
            {
                web_domain = "https://zoom.us",
                config_opts = { optionalFeatures = 1 << 5 }, // Using Zoom Customized UI
            };

            SDKError error = ZOOM_SDK_DOTNET_WRAP.CZoomSDKeDotNetWrap.Instance.Initialize(param);
        }

```

I’m still getting the same exception. My guess is that the static instance sits outside of the memory space in MVC solutions.

I will note that I’m able to call the C++ SDK from a DLL import from inside my MVC application, so this seems to point to something in the way the memory space is setup in the C# wrapper.

---

<div class="post-metadata">

**Author:** ![tyler](https://avatars.discourse-cdn.com/v4/letter/t/35a633/32.png) [@tyler](https://devforum.zoom.us/u/tyler)\
**Post date:** [April 30, 2021, 2:30pm UTC](https://devforum.zoom.us/t/system-accessviolationexception-on-initialize/49138/4 "2021-04-30T14:30:07Z")

</div>

I tried this as well

```auto
        [DllImport(@"test.dll", EntryPoint = "get_zoom_sdk_instance", CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr GetZoomSdkInstance();

        public static async void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            IntPtr zoomSdkInstance = GetZoomSdkInstance();
            ZOOM_SDK_DOTNET_WRAP.InitParam param;
            unsafe
            {
                param = new ZOOM_SDK_DOTNET_WRAP.InitParam
                {
                    web_domain = "https://zoom.us",
                    config_opts = { optionalFeatures = 1 << 5 }, // Using Zoom Customized UI
                    res_instance = zoomSdkInstance.ToPointer(),
                };

                SDKError error = ZOOM_SDK_DOTNET_WRAP.CZoomSDKeDotNetWrap.Instance.Initialize(param);
            }
        }

```

With this C++ method

```auto
    __declspec(dllexport) SDKInterfaceWrap*__stdcall get_zoom_sdk_instance() {
        return &SDKInterfaceWrap::GetInst();
    }

```

But I got the same error on initialize. Could it be the `Initialize` method in the C# wrapper is somehow handling memory incorrectly?

---

<div class="post-metadata">

**Author:** ![tyler](https://avatars.discourse-cdn.com/v4/letter/t/35a633/32.png) [@tyler](https://devforum.zoom.us/u/tyler)\
**Post date:** [April 30, 2021, 2:36pm UTC](https://devforum.zoom.us/t/system-accessviolationexception-on-initialize/49138/5 "2021-04-30T14:36:30Z")

</div>

Sorry for the overload, but I figure more information is always better.

I confirmed the following code works inside a simple console application:

```auto
        [DllImport(@"test.dll", EntryPoint = "get_zoom_sdk_instance", CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr GetZoomSdkInstance();

        static void Main(string[] args)
        {
            IntPtr zoomSdkInstance = GetZoomSdkInstance();
            unsafe
            {
                ZOOM_SDK_DOTNET_WRAP.InitParam param = new ZOOM_SDK_DOTNET_WRAP.InitParam
                {
                    web_domain = "https://zoom.us",
                    config_opts = { optionalFeatures = 1 << 5 }, // Using Zoom Customized UI
                    res_instance = zoomSdkInstance.ToPointer(),
                };

                SDKError error = ZOOM_SDK_DOTNET_WRAP.CZoomSDKeDotNetWrap.Instance.Initialize(param);
            }
        }

```

---

<div class="post-metadata">

**Author:** ![tyler](https://avatars.discourse-cdn.com/v4/letter/t/35a633/32.png) [@tyler](https://devforum.zoom.us/u/tyler)\
**Post date:** [April 30, 2021, 3:11pm UTC](https://devforum.zoom.us/t/system-accessviolationexception-on-initialize/49138/6 "2021-04-30T15:11:51Z")

</div>

Some further investigation: looks like it’s not the `Initialize` method in the C# wrapper. Any reference to doing anything to `ZOOM_SDK_DOTNET_WRAP.CZoomSDKeDotNetWrap.Instance` throws that AccessViolationException. Looks like however the static instance is setup in the wrapper causes issues in MVC.

---

<div class="post-metadata">

**Author:** ![Michael\_Condon](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/michael_condon/32/13543_2.png) [@Michael\_Condon](https://devforum.zoom.us/u/Michael_Condon)\
**Post date:** [April 30, 2021, 7:49pm UTC](https://devforum.zoom.us/t/system-accessviolationexception-on-initialize/49138/7 "2021-04-30T19:49:17Z")

</div>

Hey @tyler,

I see. Is it possible you are not creating the static instance properly?

Thanks!  
Michael

---

<div class="post-metadata">

**Author:** ![tyler](https://avatars.discourse-cdn.com/v4/letter/t/35a633/32.png) [@tyler](https://devforum.zoom.us/u/tyler)\
**Post date:** [April 30, 2021, 8:02pm UTC](https://devforum.zoom.us/t/system-accessviolationexception-on-initialize/49138/8 "2021-04-30T20:02:12Z")

</div>

Hey @Michael_Condon, in my existing application there is no need to create the static instance as far as I’ve seen. Can you give me any example? In my WPF forms application where this is working it just starts using the static instance.

As far as I know in C# there is no need to “create a static instance” if you’re referring to instantiation. Or are you referring to something else that needs to be done?

---

<div class="post-metadata">

**Author:** ![Michael\_Condon](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/michael_condon/32/13543_2.png) [@Michael\_Condon](https://devforum.zoom.us/u/Michael_Condon)\
**Post date:** [April 30, 2021, 11:12pm UTC](https://devforum.zoom.us/t/system-accessviolationexception-on-initialize/49138/9 "2021-04-30T23:12:01Z")

</div>

Hey @tyler,

The reason you would make it static is to make the “Instance” a global singleton. So that there is always only one DotNetWrap and it is available whenever needed.

Michael

---

<div class="post-metadata">

**Author:** ![tyler](https://avatars.discourse-cdn.com/v4/letter/t/35a633/32.png) [@tyler](https://devforum.zoom.us/u/tyler)\
**Post date:** [May 3, 2021, 10:53pm UTC](https://devforum.zoom.us/t/system-accessviolationexception-on-initialize/49138/10 "2021-05-03T22:53:40Z")

</div>

Do you have some sample code on how to do this?

---

<div class="post-metadata">

**Author:** ![Michael\_Condon](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/michael_condon/32/13543_2.png) [@Michael\_Condon](https://devforum.zoom.us/u/Michael_Condon)\
**Post date:** [May 4, 2021, 7:15pm UTC](https://devforum.zoom.us/t/system-accessviolationexception-on-initialize/49138/11 "2021-05-04T19:15:13Z")

</div>

Hey @tyler,

Yes but it would be the same code that is available in the C# wrapper demo. If that code does not work, it is likely an MVC issue.

Thanks!  
Michael

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex016/uploads/zoomdeveloper/original/3X/6/1/614bdd549b610bbaa46ff934617683a02bdaa03c.png) [@system](https://devforum.zoom.us/u/system)\
**Post date:** [June 4, 2021, 5:15am UTC](https://devforum.zoom.us/t/system-accessviolationexception-on-initialize/49138/12 "2021-06-04T05:15:56Z")

</div>

Closed on June 4, 2021, 5:15am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
