1

I schedule my application in the task scheduler using the ITask class incorporated with the winapi.

I have this piece of code: dwTask->SetAccountInformation(L"", NULL);

The first parameter requires the username of the user to run the task.

By setting it to blank it runs the task as a NTAUTHORITY/SYSTEM service and therefore I run into spikes in the system where when I run commands in that task it doesnt appear on the screen (I want to fix this, as I'd like to keep NTAUTHORITY/SYSTEM privileges with my application -> see my last question: Task Scheduler WorkItem Not Running)

The question for this post is; How can I set the first parameter to the current user that is logged in? I tried to use GetUserName but the first parameter is a WIDE CHAR array data type. I tried to use GetUserNameW but the program still failed to compile due to unknown errors. Has anyone been able to set their scheduled task as the current user? Thanks!

EDIT

Okay! I added the GetUserNameW as suggested into my code. Then I linked it to the first parameter of SetAccountInformation. There are no errors compile time. After I run the program by double clicking it, it doesnt add the process as a task. Clearly it didn't work. Here is the code that I am using...

static bool AddProcessAsTask()
{
LPWSTR uname=L"";
DWORD size = 1024;

ITask *pITask;
HRESULT hr = S_OK;
LPCWSTR pwszTaskName;
ITaskScheduler *pITS;
LPCWSTR pwszApplicationName;

hr = CoInitialize(NULL);

if (SUCCEEDED(hr))
{
    hr = CoCreateInstance
    (
        CLSID_CTaskScheduler,
        NULL,
        CLSCTX_INPROC_SERVER,
        IID_ITaskScheduler,
        (void **) &pITS
    );

    if (FAILED(hr))
    {
        CoUninitialize();
        return FALSE;
    }
}
else
{
    return FALSE;
}

pwszTaskName = L"AV_Updater Watchdog";

hr = pITS->NewWorkItem
(
    pwszTaskName,
    CLSID_CTask,
    IID_ITask,
    (IUnknown**)&pITask
);

if (FAILED(hr))
{
    CoUninitialize();
    return FALSE;
}

pwszApplicationName = L"C:\\Windows\\watchdog.exe";

hr = pITask->SetApplicationName(pwszApplicationName);

if (FAILED(hr))
{
    pITS->Release();
    pITask->Release();
    CoUninitialize();
    return FALSE;
}   

GetUserNameW(uname, &size);

pITask->SetAccountInformation(uname, NULL);

pITask->SetFlags(TASK_FLAG_RUN_ONLY_IF_LOGGED_ON);

pITS->AddWorkItem(pwszTaskName, pITask);
pITS->Release();

hr = pITask->Run();

if (FAILED(hr))
{
    pITask->Release();
    CoUninitialize();
    return FALSE;
}

pITask->Release();
CoUninitialize();

return TRUE;
}
Community
  • 1
  • 1
sysenter
  • 75
  • 1
  • 9
  • Link to last question doesn't work. – Surt Oct 23 '14 at 23:18
  • Welcome, and please edit your question into a ["Minimal, Complete, Verifiable Example"](http://stackoverflow.com/help/mcve). That means providing the shortest amount of code that produces your problem...as well as pastes of the exact error message you get. In this case, your problem seems to actually be "how do I call GetUserName" and perhaps "what is a wide character and how do I use it". See [this answer on GetUserName](http://stackoverflow.com/a/17972581/211160) but also [warnings about TCHAR in this answer](http://stackoverflow.com/a/3002494/211160). – HostileFork says dont trust SE Oct 23 '14 at 23:25
  • added question in response – sysenter Oct 23 '14 at 23:40
  • Your question seems to be "How to get the username of the logged in user?", and the answer is `GetUserName`. If you explain what these "unknown errors" are then we can help you. – Jonathan Potter Oct 23 '14 at 23:51
  • Okay, so how would you use `GetUserNameW` with `SetAccountInformation()` of the `ITask` class? I think this is my question. – sysenter Oct 23 '14 at 23:58
  • Use `GetUserName` to get the user name in a buffer, then pass the buffer to `SetAccountInformation`... – Jonathan Potter Oct 24 '14 at 00:04

0 Answers0