I tried to run Microsoft Word using windows service, Windows API.
The following code throw an error
uint sessionId = WTSGetActiveConsoleSessionId();
var result = WTSQueryUserToken((UInt32)sessionId, out p);
bool result = DuplicateTokenEx(p, TOKEN_ALL_ACCESS, IntPtr.Zero, TokenImpersonation, 1, out duplicate);
result = CreateProcessAsUser(duplicate, processExe, null, IntPtr.Zero, IntPtr.Zero, false, (UInt32)CreateProcessFlags.CREATE_NEW_CONSOLE, IntPtr.Zero, null, ref info, out procInfo);
Microsoft Word opened but can not access any folders and show an error:
What's wrong with my code?
WTSQueryUserToken
already returns a primary token. Also you are not doing any error checking. As word is a GUI application you probably also shouldn't passCREATE_NEW_CONSOLE
flag