473,399 Members | 4,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

SetThreadDesktop keeps returning "Resource in Use"


I have a utility that monitors whether certain third party GU
applications are running and if not, it starts them up. I would lik
to get rid of this utility and instead just have a Windows Service tha
performs that same function. Because Windows Services don't run in th
interactive Windows Station and C# has nothing to do this sort o
thing, I am being forced to use unmanaged function calls from my C
code. I first obtain the access token for the current logged in use
by enumerating all processes using EnumProcesses(), callin
OpenProcess() on each pid, calling EnumProcessModules() to enumerat
the process modules, searching for explorer.exe usin
GetModuleBaseName() and getting it's token with OpenProcessToken() i
found. If not I call CloseHandle() and move on. Once found,
duplicate the token to make an impersonation token, us
ImpersonateLoggedOnUser() and Close the handle. Next I cal
OpenWindowStation() for WinSta0 the OpenDesktop() for the Defaul
desktop.

The problem occurs when I call SetThreadDesktop() with the handl
returned from OpenDesktop(). SetThreadDesktop() is generating erro
170 "resource in use". What could cause this? I mean, I'm closin
handles that I'm not using, am I not able to call this function if
have *any* handles open? If so, how does one use this properly in C
code?

FYI, I'm on WinXP, but will need this to run on win2k if it's eve
possible. I can post some source code if you want, but it's kinda jus
thrown together at the moment because I've just been experimenting wit
various ways of doing what I'm trying to do.

Thanks
Fran

--
Frank Perr
-----------------------------------------------------------------------
Frank Perry's Profile: http://www.msusenet.com/member.php?userid=72
View this thread: http://www.msusenet.com/t-187013766

Nov 17 '05 #1
1 6655

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

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Aaron | last post by:
IIS 5.0 is throwing out "The requested resource is in use." for any site that uses ASP - HTML is executing fine. I have tried re-installing scripting, latest MDAC, and all my hotfixes are up to...
40
by: Steve Juranich | last post by:
I know that this topic has the potential for blowing up in my face, but I can't help asking. I've been using Python since 1.5.1, so I'm not what you'd call a "n00b". I dutifully evangelize on the...
2
by: Happy Li | last post by:
Need help! I have a project with one crystal report. How can I compile it with csc.exe. I have tried the following command line .it compile successfully..but when I run, it display error 'Unable...
0
by: gg | last post by:
Hello, who can tell me how to use the Resource Assembly File (*.resx) in a project. For example: I define a item Name :"TestString" Vale "1234567890" in a Resource Assembly File(test.resx). How...
2
by: Matt | last post by:
I'm working on a plug-in for an application called DesktopSideBar for Windows. It emulates to some degree the Longhorn sidebar. The SDK for DesktopSideBar supports .NET plug-in development through...
1
by: John Layton | last post by:
Hi there, Does anyone know if there's a built-in way to read a ".txt" based resources file (one that's converted to a ".resources" file by "resgen.exe" at build time). I need to read/write the...
0
by: keikoo | last post by:
Hi, I need some help with this control. There's a windows form with a axwebbrowser control inside, so users can navigate to a page and it's necessary to keep the session, because, users will...
1
by: ccon67 | last post by:
In a win32 project I have several compile errors for the resource *.rc. The first fews errors look like this error RC2144 : PRIMARY LANGUAGE ID not a number error RC2135 : file not found:...
1
by: codemania | last post by:
Disappointing me extremely, with the "generate python" function within Resource Editor, I only get a segment of python code which load xrc(xml format) file, rather than real python code in which I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.