Hi,
I had popup a UI using win32 api creaprocessasuser. It works as I use admin user login. but it give two exception
1. unhandled exception (in event handler)as a normal user try to launch pop UI.
Description: The process was terminated due to an unhandled exception.
Exception Info: System.ObjectDisposedException
at System.Windows.Forms.Control.CreateHandle()
at System.Windows.Forms.Form.CreateHandle()
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.ContainerControl.FocusActiveControlInternal()
at System.Windows.Forms.Form.SetVisibleCore(Boolean)
at System.Windows.Forms.Control.set_Visible(Boolean)
at System.Windows.Forms.Application+ThreadContext.RunMessageLoopInner(Int32, System.Windows.Forms.ApplicationContext)
at System.Windows.Forms.Application+ThreadContext.RunMessageLoop(Int32, System.Windows.Forms.ApplicationContext)
at System.Windows.Forms.Application.Run(System.Windows.Forms.Form)
at WinFormPrintDriverApp.Program.Main(System.String[])
Faulting application name: myapplication.exe, version: 1.0.0.0, time stamp: 0xab4b49da
Faulting module name: KERNELBASE.dll, version: 6.3.9600.19671, time stamp: 0x5e67335c
Exception code: 0xe0434352
Fault offset: 0x00013d28
Faulting process id: 0x1d84
Faulting application start time: 0x01d77a36c724e6f6
Faulting application path: c:\testpath\myapplication.exe
Faulting module path: C:\Windows\SYSTEM32\KERNELBASE.dll
Report Id: 0ae714c2-e62a-11eb-8106-0050568ffd89
Faulting package full name:
Faulting package-relative application ID:
what might be issue? Any help appreciated to resolve this issue.
What I have tried:
my code:
1 public static bool StartProcessAsCurrentUser(string appPath, string cmdLine, string currentUserName, string workDir = null, bool visible = true)
2 {
3 var hUserToken = IntPtr.Zero;
4 var startInfo = new STARTUPINFO();
5 var procInfo = new PROCESS_INFORMATION();
6 var pEnv = IntPtr.Zero;
7 int iResultOfCreateProcessAsUser;
8
9 startInfo.cb = Marshal.SizeOf(typeof(STARTUPINFO));
10
11 try
12 {
13 if (!GetSessionUserToken(ref hUserToken, currentUserName))
14 {
15 throw new Exception("StartProcessAsCurrentUser: GetSessionUserToken failed.");
16 }
17 uint dwCreationFlags = CREATE_UNICODE_ENVIRONMENT | (uint)(visible ? CREATE_NEW_CONSOLE : CREATE_NO_WINDOW);
18 startInfo.wShowWindow = (short)(visible ? SW.SW_SHOW : SW.SW_HIDE);
19 startInfo.lpDesktop = "winsta0\\default";
20
21 if (!CreateEnvironmentBlock(ref pEnv, hUserToken, false))
22 {
23 throw new Exception("StartProcessAsCurrentUser: CreateEnvironmentBlock failed.");
24 }
25
26 if (!CreateProcessAsUser(hUserToken,
27 appPath,
28 cmdLine,
29 IntPtr.Zero,
30 IntPtr.Zero,
31 false,
32 dwCreationFlags,
33 pEnv,
34 workDir,
35 ref startInfo,
36 out procInfo))
37 {
38 iResultOfCreateProcessAsUser = Marshal.GetLastWin32Error();
39 throw new Exception("StartProcessAsCurrentUser: CreateProcessAsUser failed. Error Code -" + iResultOfCreateProcessAsUser);
40 }
41 iResultOfCreateProcessAsUser = Marshal.GetLastWin32Error();
42 }
43 catch(Exception ex)
44 {
45 log.Info("StartProcessAsCurrentUser exeption msg:"+ex.Message +" stacktrace:"+ex.StackTrace);
46 }
47 finally
48 {
49 CloseHandle(hUserToken);
50 if (pEnv != IntPtr.Zero)
51 {
52 DestroyEnvironmentBlock(pEnv);
53 }
54 CloseHandle(procInfo.hThread);
55 CloseHandle(procInfo.hProcess);
56 }
57 return true;
58 }
59 }
60
61 private static bool GetSessionUserToken(ref IntPtr phUserToken, string currentUserName)
62 {
63 var bResult = false;
64 var hImpersonationToken = IntPtr.Zero;
65 var activeSessionId = INVALID_SESSION_ID;
66 try
67 {
68 ITerminalServicesManager manager = new TerminalServicesManager();
69 using (ITerminalServer server = manager.GetLocalServer())
70 {
71 server.Open();
72 foreach (ITerminalServicesSession session in server.GetSessions())
73 {
74 if (session.UserName.ToUpper() == currentUserName.ToUpper())
75 {
76 activeSessionId = (uint)session.SessionId;
77 break;
78 }
79 }
80 }
81
82
83 if (activeSessionId == INVALID_SESSION_ID)
84 {
85 activeSessionId = WTSGetActiveConsoleSessionId();
86 }
87 log.Info("Method GetSessionUserToken: activeSessionId:" + activeSessionId);
88 if (WTSQueryUserToken(activeSessionId, ref hImpersonationToken) != 0)
89 {
90
91 bResult = DuplicateTokenEx(hImpersonationToken, 0, IntPtr.Zero,
92 (int)SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, (int)TOKEN_TYPE.TokenPrimary, ref phUserToken);
93 CloseHandle(hImpersonationToken);
94 }
95 log.Info("GetSessionUserToken ends.");
96 }
97 catch(Exception ex)
98 {
99 log.Info("GetSessionUserToken exception:." + ex.Message);
100 }
101
102 return bResult;
103 }