Search code examples
c++windowswinapischeduled-tasks

C++ Logon task schedule Error: No Mapping between account names and security ids was done


I am trying to write a windows Logon trigger task using C++ on Windows 7.

I am following this microsoft tutorial.

But I am facing problem in saving the task to root folder. Here:

//  ------------------------------------------------------
    //  Save the task in the root folder.
    IRegisteredTask *pRegisteredTask = NULL;

    hr = pRootFolder->RegisterTaskDefinition(
            _bstr_t( wszTaskName ),
            pTask,
            TASK_CREATE_OR_UPDATE, 
            _variant_t(L"Builtin\\Administrators"), 
            _variant_t(), 
            TASK_LOGON_GROUP,
            _variant_t(L""),
            &pRegisteredTask);

Where the hr is getting error : No Mapping between account names and security ids was done

I also tried replacing _variant_t(L"Builtin\\Administrators") with _variant_t(L"S-1-5-32-544") to NULL out language hard coding issue, still No luck.

How can I make it work?


Solution

  • I suspect the demo code you have is XP-era, and hasn't been updated to match the Vista/Win7 rules.

    I updated the sample to set the LUA settings after setting the logon trigger, and it seems to work:

        hr = pLogonTrigger->put_UserId(_bstr_t(L"DOMAIN\username"));
        if (FAILED(hr))
        {
            printf("\nCannot add user ID to logon trigger: %x", hr);
            CoUninitialize();
            return 1;
        }
    
    
        //*** NEW**** Set the LUA settings
        CComPtr<IPrincipal>         pPrincipal;
    
        hr = pTask->get_Principal(&pPrincipal);
        if (SUCCEEDED(hr))
        {
            hr = pPrincipal->put_RunLevel(TASK_RUNLEVEL_LUA);
        }
        if (SUCCEEDED(hr))
        {
            hr = pPrincipal->put_GroupId(_bstr_t(L"Builtin\\Administrators"));
        }
        if (FAILED(hr))
        {
            printf("\nCannot set runlevel/groupid: %x", hr);
            CoUninitialize();
            return 1;
        }
    

    If you need it to run on XP, then it's likely that the get_Principal call will fail, so let that failure through.