Why does Group Policy block to create ISensorManger instance?

1

I have a Surface Pro 3, running Window 10 Pro, version 1803. One of the things that I do with this tablet is play around with the new API and programming features that Microsoft provides.

Recently, I was test some small toy programs using the sensor API to get a feel for them. I have a small program that enumerates all the sensors on the device, worked find on an older surface tablet, running 8.1. However when I brought the source code over to the Surface Pro, recompiled it and when to run it I got the error:

"Unable to CoCreateInstance() on SensorManger error: This program is blocked by group policy. For more information contact your system administrator"

I get this error regardless if I run as myself or as an administrator. Due to this being test code, I never bother to do anything robust error handling so this error does not cause the program to abort, so the program continues and shows a GUI with no sensor discovered obviously.

This device is a personal stand-alone device, not attached to a domain controller. Under developers, I have selected "sideload apps".

My question is, how do I troubleshoot / fix the above error?

A second, related, question with this being a stand-alone computer do I really need Group Policy, and if not is there some way to disable it

Thanks

thurizas

Posted 2019-03-11T01:49:06.897

Reputation: 113

I googled that error message, and literally every site told me the first thing to look for is a virus/malware infection. You should do that first. After that, do the search yourself and go through the other dozen or so possibilities. If you exhaust all of those, go back to your program and add debugging code. – Michael Hampton – 2019-03-11T02:16:28.327

@Biswapriyo Yes, I have the code (I wrote it - it is my first attempt at using the new sensor API). As I mentioned in my original question, the code works correctly on a Windows 8.1 system listing the available sensors. It, however fails with the reported error on a Windows 10 system – thurizas – 2019-03-11T10:42:49.313

@Biswapriyo I'm hoping you will post the solution. (or if you have I'm not seeing it for some reason) – thurizas – 2019-03-11T13:49:04.960

Answers

0

How do I troubleshoot / fix the above error?

Short answer: Run these two command as administrator (using reg add command):

REG ADD "HKLM\SOFTWARE\Policies\Microsoft\Windows\LocationAndSensors" /V "DisableSensors" /T REG_DWORD /D "0" /F 
REG ADD "HKCU\SOFTWARE\Policies\Microsoft\Windows\LocationAndSensors" /V "DisableSensors" /T REG_DWORD /D "0" /F 

Explanation: I used some C language with SensorManger interface. You can skip this part. A demo program:

#include <Windows.h>
#include <sensorsapi.h>

void func(void)
{
    HRESULT hRes = 0;
    ISensorManager *pSensorManager = NULL;

    hRes = CoInitialize(NULL);
    hRes = CoCreateInstance(&CLSID_SensorManager,
                            NULL,
                            CLSCTX_INPROC_SERVER,
                            &IID_ISensorManager,
                            &pSensorManager);

    hRes = pSensorManager->lpVtbl->Release(pSensorManager);
    CoUninitialize();
}

How did I find? This requires some reverse engineering idea. The CLSID_SensorManager GUID leads me to C:\Windows\System32\SensorsApi.dll file. I opened it in a disassembler. There is a function SensorsGroupPolicy::IsSensorsFeatureDisabled in that DLL file which checks both the two REG_DWORD values.

  • HKLM\SOFTWARE\Policies\Microsoft\Windows\LocationAndSensors\DisableSensors is checked first which is applied for all user.
  • Then HKCU\SOFTWARE\Policies\Microsoft\Windows\LocationAndSensors\DisableSensors is checked which is applied for current logged in user.
  • The setting can be disabled in Local Group Policy editor → Computer Configuration → Administrative Templates → Windows Components → Location and Sensors → Turn off sensors → Disabled. But the registry method is instantaneous.

Biswapriyo

Posted 2019-03-11T01:49:06.897

Reputation: 6 640

thanks alot. The only issue was that i kept getting a syntax error on the REG ADD commands. Finally just added the keys/ values manually. – thurizas – 2019-03-11T23:47:00.960

Interesting, the commands work fine in my PC. – Biswapriyo – 2019-03-12T04:17:20.493