0

So I can't find any information on this one and was wondering if anyone has encountered the same issue.

I'm using "c#" , with "Windows.Media.Capture" in a dot net core 3.1 build. When I use the build as an exe, I can communicate with the webcam and acquire images and record video.

However when I bundle the exe into a service( i'm using NSSM), I get an Unauthorized access exception on initialization.

I have changed the camera privacy settings to allow all desktop apps etc to use the camera.

I cannot find an option for services to access the camera. I have also changed the registry setting and created a group policy, but with no luck.

I believe it is a privacy setting of some form as the application works fine when run as a desktop application.

Any help would be greatly appreciated. Thanks Rob

Also here is the initialization code I'm using (straight from the Microsoft website). Just for transparency.

/// <summary>
        /// Initializes the MediaCapture, registers events, gets camera device information for mirroring and rotating, starts preview and unlocks the UI
        /// </summary>
        /// <returns></returns>
        private async Task InitializeCameraAsync()
        {
          Debug.WriteLine("InitializeCameraAsync");

          var picturesLibrary = await 
          StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures);


        // Fall back to the local app storage if the Pictures Library is not available
        _captureFolder = picturesLibrary.SaveFolder ?? ApplicationData.Current.LocalFolder;
        if (_mediaCapture == null)
        {
            // Attempt to get the back camera if one is available, but use any camera device if not
            var cameraDevice = await FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel.Back);

            if (cameraDevice == null)
            {
                Debug.WriteLine("No camera device found!");
                return;
            }

            // Create MediaCapture and its settings
            _mediaCapture = new MediaCapture();

            // Register for a notification when video recording has reached the maximum time and when something goes wrong
            // _mediaCapture.RecordLimitationExceeded += MediaCapture_RecordLimitationExceeded;
            // _mediaCapture.Failed += MediaCapture_Failed;

            var settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameraDevice.Id };
            settings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.Video;

            // Initialize MediaCapture
            try
            {
                await _mediaCapture.InitializeAsync(settings);
                _isInitialized = true;
            }
            catch (UnauthorizedAccessException)
            {
                Debug.WriteLine("The app was denied access to the camera");
                throw new Exception("The app was denied access to the camera");
            }


        }
    }
Harish
  • 789
  • 1
  • 7
  • 21
Rob
  • 1
  • 1
    Also I have tried running the service under a user account, admin account and Local system account. – Rob Apr 07 '21 at 08:38

1 Answers1

0

I believe it is a privacy setting of some form as the application works fine when run as a desktop application.

It's probably either User Interface Privilege Isolation or Session 0 Isolation. There are a few Vista-era hacks to try to get around them that may or may not work on Win10, but none of those hacks were ever recommended and would certainly be frowned on these days.

The best solution is probably to replace the Win32 service with an auto-started app. If you absolutely need a Win32 service, then you'll probably need to use a lower-level API.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • Thanks for the reply. I have it temporarily running as a startup app now. But I will have to go to a lower level in the future. – Rob Apr 09 '21 at 06:54