-1

How do I set Task or Thread privileges ?

I ask because I use Task Scheduler Managed Wrapper and when I call it in another thread (that apparently has lower privileges) I get exception.

I saw this SO question, but didn't manage to use it successfully (Thread.CurrentPrincipal.Identity as WindowsIdentity is null)

Edit:

This is how I start the thread:

await Task.Factory.StartNew(() => {
    /*...*/
}, cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);

Edit 2:

These are the exception's details:

COMException: Interface not registered (Exception from HRESULT: 0x80040155)

    at Microsoft.Win32.TaskScheduler.V2Interop.IRegisteredTask.get_Definition()
    at Microsoft.Win32.TaskScheduler.Task.get_Definition()
    at TCM.TaskShedulerStartUp.Update() in c:\...\TaskShedulerStartUp.cs:line 61
Community
  • 1
  • 1
Tar
  • 8,529
  • 9
  • 56
  • 127
  • 2
    All threads are started with the privileges of the process, so most likely you diagnosis is wrong. – H H Aug 04 '13 at 09:16
  • If your diagnosis is correct, don't change the thread a task is running on. It is a shared resource. Create a new thread that you own. – usr Aug 04 '13 at 09:53
  • @usr: see the "Edit": I own the thread that I create (am I wrong ???) – Tar Aug 04 '13 at 11:19
  • @HenkHolterman: I don't see how am I wrong - when I do this action from the "main" thread it works - moreover - if I ask the main thread to do that (using `mainThreadDispatcher.Invoke(() => {/*...*/})`), it works either. – Tar Aug 04 '13 at 11:23
  • @Tal although it is not guaranteed by the docs, in practice yes. I'd still prefer a custom taskscheduler here. – usr Aug 04 '13 at 11:31
  • First, post the exception with all details. – H H Aug 04 '13 at 13:22

1 Answers1

3

This COM error code has nothing to do with thread privileges, the linked article is quite wrong about that. An interface is registered in the HKLM\Software\Classes\Interface registry key. COM will look there when it needs to marshal the call from one thread to another, it needs to know what proxy is going to copy the function argument values. Which is certainly a very likely scenario in your program, the only thing we know is that you do start a thread.

It seems there's something wrong with the registry on your machine. Or the COM server simply isn't registered correctly. Or the COM server just doesn't support being used from a thread. Or your program is a 32-bit process on the 64-bit version of Windows and the interface key is not present in Wow6432Node. Or the server was registered incorrectly, writing the keys only in HKCU but not HKLM, which does have something to do with the user account used to run the code.

There's nothing in your question that would help us help you narrow that down. Having a problem with the task scheduler is a long-distance explanation, it losing a registry key would be a rather gross problem with your machine.

Start diagnosing this problem by running SysInternals' ProcMon utility. You'll see your program searching for the key in Interface and not finding it. Update your question with the full key name to get more help.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536