For clarity, here is what the function I'm using currently looks like:
private void LaunchApp()
{
GetDesktopWindo w();
IntPtr hwinstaSave = GetProcessWindo wStation();
UInt32 ThreadID = GetCurrentThrea dId();
IntPtr hDeskSave = GetThreadDeskto p(ThreadID);
UInt32 [] procIDs = new UInt32[2048];
UInt32 [] cbNeeded = new UInt32[1];
UInt32 procarrsize = UInt32.Parse(pr ocIDs.Length.To String());
Int32 error;
RevertToSelf();
if (!EnumProcesses (procIDs, procarrsize, cbNeeded))
{
error = Marshal.GetLast Win32Error();
return;
}
IntPtr tokenhandle = IntPtr.Zero;
IntPtr prochandle = IntPtr.Zero;
bool pidfound = false;
foreach (UInt32 pid in procIDs)
{
if ((pid != 0) && (pid != 4))
{
prochandle = OpenProcess(0x0 400 | 0x0010, false, pid);
if (!prochandle.Eq uals(null))
{
IntPtr[] mod = new IntPtr[2048];
if (EnumProcessMod ules(prochandle , mod
UInt32.Parse(mo d.Length.ToStri ng()), cbNeeded))
{
foreach(IntPtr modnum in mod)
{
if (modnum.ToInt32 () > 0)
{
StringBuilder exename = new StringBuilder(2 56);
if (GetModuleBaseN ame(prochandle, modnum, exename
exename.Capacit y) != 0)
{
if (String.Compare (exename.ToStri ng().ToLower()
"explorer.e xe") == 0)
{
if (OpenProcessTok en(prochandle, 0x0002, ref tokenhandle) !
0)
{
pidfound = true;
break;
}
else
{
error = Marshal.GetLast Win32Error();
CloseHandle(pro chandle);
return;
}
}
}
else
{
error = Marshal.GetLast Win32Error();
continue;
}
}
}
if (pidfound)
{
CloseHandle(pro chandle);
break;
}
}
else
{
CloseHandle(pro chandle);
LogError(Marsha l.GetLastWin32E rror());
continue;
}
}
CloseHandle(pro chandle);
}
}
IntPtr duptok = IntPtr.Zero;
if (!DuplicateToke nEx(tokenhandle , 0x0004 | 0x0008 |0x0020
IntPtr.Zero, 2, 2, ref duptok))
{
error = Marshal.GetLast Win32Error();
CloseHandle(pro chandle);
return;
}
if (ImpersonateLog gedOnUser(dupto k) != 0)
{
CloseHandle(pro chandle);
}
else
{
error = Marshal.GetLast Win32Error();
CloseHandle(pro chandle);
return;
}
IntPtr hWinStaUser = IntPtr.Zero;
hWinStaUser = OpenWindowStati on("WinSta0", false, MAXIMUM_ALLOWED );
if (!SetProcessWin dowStation(hWin StaUser))
{
RevertToSelf();
LogError(Marsha l.GetLastWin32E rror());
return;
}
IntPtr hDeskUser = OpenDesktop("De fault", 0, false, MAXIMUM_ALLOWED );
RevertToSelf();
// The error occurs on the next line.
if (!SetThreadDesk top(hDeskUser))
{
RevertToSelf();
LogError(Marsha l.GetLastWin32E rror());
return;
}
...
...
--
Frank Perr
-----------------------------------------------------------------------
Frank Perry's Profile:
http://www.msusenet.com/member.php?userid=72
View this thread:
http://www.msusenet.com/t-187013766