0

I'm writing windows service which consists three separated threads. When user logs in (is logged when service starts) I impersonate each thread (so it operates on same access rights as currently logged on user).

(Impersonation code without error handling)

    /*Query Token */
    WTSQueryUserToken(SessionId, &hUserToken)

    /* Duplicate Token */
    DuplicateToken(
        hUserToken,
        SecurityImpersonation,
        &hDuplicateUserToken
        )

    /* Open Thread handle */
    hThreadHandle = OpenThread(
            THREAD_IMPERSONATE | 
            THREAD_QUERY_INFORMATION | 
            THREAD_SET_THREAD_TOKEN,
            TRUE,
            threadID
            );

    /* Assign the impersonation token to the thread */
    SetThreadToken(&hThreadHandle, hDuplicateUserToken)

Later in the same thread I use COM (image factory) object to retrieve thumbnails for images (using many available APIs gives similar results)

    /* com initialization */
    CoInitializeEx(NULL, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE);

    /* piece of code to retrieve a thumbnail */
    hr = pImageFactory->GetImage(size, SIIGBF_BIGGERSIZEOK, &thumbNail);

When code to retrieve thumbnail is called from non impersonated thread or normal desktop application I receive thumbnails for both images and videos. When code is called from impersonated thread the only thing I receive is one icon for all images or not implemented hresult (for IThumbnail provider).

Is there anything I should do with impersonated thread prior calling COM APIs?

dizzer
  • 106
  • 1
  • 6
  • Have you tried to put your thread in STA instead of MTA? Also a Windows service is not well suited to to 1) impersonate the logged on user and 2) use the Windows Shell services. Why don't you write an application that runs when the users logs on? – Simon Mourier May 12 '13 at 10:17
  • Thanks Simon! Using STA instead of MTA works with IThumbnail Provider. – dizzer May 13 '13 at 09:20

1 Answers1

0

As Simon Mourier suggested - using STA instead of MTA solves the problem.

CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
dizzer
  • 106
  • 1
  • 6