473,756 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SetThreadDeskto p 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 EnumProcessModu les() to enumerat
the process modules, searching for explorer.exe usin
GetModuleBaseNa me() and getting it's token with OpenProcessToke n() i
found. If not I call CloseHandle() and move on. Once found,
duplicate the token to make an impersonation token, us
ImpersonateLogg edOnUser() and Close the handle. Next I cal
OpenWindowStati on() for WinSta0 the OpenDesktop() for the Defaul
desktop.

The problem occurs when I call SetThreadDeskto p() with the handl
returned from OpenDesktop(). SetThreadDeskto p() 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 6712

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

Nov 17 '05 #2

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

Similar topics

6
7853
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 date. I have also tried re-syncing the IWAM account but that didn't work either... Running the webs in HIGH (isolated) did not help either. I disabled ISAPI caching, and re-enabled it .. nothing ... rebooted the server, restarted IIS several times ...
40
3043
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 goodness of Python whenever I talk with fellow developers, but I always hit a snag when it comes to discussing the finer points of the execution model (specifically, exceptions). Without fail, when I start talking with some of the "old-timers"...
2
7867
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 to find the report in the manifest resources'. csc.exe /target:exe /resource:source\abc.rpt,abcsystem.abc.rpt /lib:"C:\Program Files\Common Files\Crystal Decisions\1.1\Managed"...
0
1332
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 to load the string vale? Thanks
2
1793
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 COM Interop and a provided reference DLL. In the SDK are two included samples... One, called RecycleBin is written entirely in unmanaged C++ as a normal DLL. The second, StickerPanel, is written in C#. My first exercise was porting StickerPanel...
1
2374
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 ".txt" file directly. Thanks.in advance.
0
3100
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 access many times to this site and don't want to log in each time. The process is the following: the form is shown and user navigate through the webbrowser control, the form can be closed and opened many times but the session of the page referenced...
1
3098
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: MAIN_MENU error RC2164 : unexpected value in RCDATA To overcome all of these errors, I include <windows.hat the top of "resource.h". But verytime the resource editor is touch, a new resource.h is generated and I have to add that line again...I'm...
1
1848
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 could append my own code. Does that mean RE(Resource Editor) is of little use? I hope not, maybe the way I use it is wrong? Tell me please.
0
9431
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9255
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10014
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9819
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9689
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7226
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5119
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3780
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2647
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.