For clarity, here is what the function I'm using currently looks like:
private void LaunchApp()
{
GetDesktopWindow();
IntPtr hwinstaSave = GetProcessWindowStation();
UInt32 ThreadID = GetCurrentThreadId();
IntPtr hDeskSave = GetThreadDesktop(ThreadID);
UInt32 [] procIDs = new UInt32[2048];
UInt32 [] cbNeeded = new UInt32[1];
UInt32 procarrsize = UInt32.Parse(procIDs.Length.ToString());
Int32 error;
RevertToSelf();
if (!EnumProcesses(procIDs, procarrsize, cbNeeded))
{
error = Marshal.GetLastWin32Error();
return;
}
IntPtr tokenhandle = IntPtr.Zero;
IntPtr prochandle = IntPtr.Zero;
bool pidfound = false;
foreach (UInt32 pid in procIDs)
{
if ((pid != 0) && (pid != 4))
{
prochandle = OpenProcess(0x0400 | 0x0010, false, pid);
if (!prochandle.Equals(null))
{
IntPtr[] mod = new IntPtr[2048];
if (EnumProcessModules(prochandle, mod
UInt32.Parse(mod.Length.ToString()), cbNeeded))
{
foreach(IntPtr modnum in mod)
{
if (modnum.ToInt32() > 0)
{
StringBuilder exename = new StringBuilder(256);
if (GetModuleBaseName(prochandle, modnum, exename
exename.Capacity) != 0)
{
if (String.Compare(exename.ToString().ToLower()
"explorer.exe") == 0)
{
if (OpenProcessToken(prochandle, 0x0002, ref tokenhandle) !
0)
{
pidfound = true;
break;
}
else
{
error = Marshal.GetLastWin32Error();
CloseHandle(prochandle);
return;
}
}
}
else
{
error = Marshal.GetLastWin32Error();
continue;
}
}
}
if (pidfound)
{
CloseHandle(prochandle);
break;
}
}
else
{
CloseHandle(prochandle);
LogError(Marshal.GetLastWin32Error());
continue;
}
}
CloseHandle(prochandle);
}
}
IntPtr duptok = IntPtr.Zero;
if (!DuplicateTokenEx(tokenhandle, 0x0004 | 0x0008 |0x0020
IntPtr.Zero, 2, 2, ref duptok))
{
error = Marshal.GetLastWin32Error();
CloseHandle(prochandle);
return;
}
if (ImpersonateLoggedOnUser(duptok) != 0)
{
CloseHandle(prochandle);
}
else
{
error = Marshal.GetLastWin32Error();
CloseHandle(prochandle);
return;
}
IntPtr hWinStaUser = IntPtr.Zero;
hWinStaUser = OpenWindowStation("WinSta0", false, MAXIMUM_ALLOWED);
if (!SetProcessWindowStation(hWinStaUser))
{
RevertToSelf();
LogError(Marshal.GetLastWin32Error());
return;
}
IntPtr hDeskUser = OpenDesktop("Default", 0, false, MAXIMUM_ALLOWED);
RevertToSelf();
// The error occurs on the next line.
if (!SetThreadDesktop(hDeskUser))
{
RevertToSelf();
LogError(Marshal.GetLastWin32Error());
return;
}
...
...
--
Frank Perr
-----------------------------------------------------------------------
Frank Perry's Profile:
http://www.msusenet.com/member.php?userid=72
View this thread:
http://www.msusenet.com/t-187013766