1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
2 <!-- saved from url=(0062)http://support.microsoft.com/support/kb/articles/Q173/6/87.ASP -->
3 <HTML><HEAD><TITLE>Q173687 - HOWTO: Access the Application Desktop from a Service
</TITLE>
4 <META http-equiv=CONTENT-Type
content=
"text/html; charset=iso-8859-1">
5 <META http-equiv=PICS-Label
6 content='(PICS-
1.1 "http://www.rsac.org/ratingsv01.html" l gen true comment
"RSACi North America Server" by
"Inet@microsoft.com" for
"http://support.microsoft.com" on
"1998.02.17T12:28-0800" r (n
0 s
0 v
0 l
0))'
>
7 <META content=EN-US name=ms.locale
>
8 <META content=
"Support; KB Article" name=Category
>
9 <META content=Support name=Premium
>
10 <META content=
"Support; KB; win32sdk; kbOSWinNT; kbOSWinSearch; " name=KBArea
>
11 <META content=Q173687 name=KBID
>
12 <META content=
"HOWTO: Access the Application Desktop from a Service"
15 content='The configuration of a Windows NT service determines how it may access the
application desktop. The application desktop is named
"default" and belongs
to the
"WinSta0" Window station object. The application desktop is
associated with the in'
17 <META content=win32sdk name=Product
>
18 <META content=
"September 10, 1997" name=KBCreate
>
19 <META content=
"October 21, 2000" name=KBModify
>
20 <META content=
"September 11, 1997" name=EditDate
>
21 <META content=
"" name=Question
>
22 <META content=winnt:
3.51,
4.0 name=Versions
>
23 <META content=
"" name=Component
>
24 <META content=
"kbOSWinNT kbOSWinSearch" name=Technology
>
25 <META content=
"" name=Links
>
27 content=
"kbKernBase kbOSWin2000 kbSecurity kbService kbDSupport kbGrpKernBase"
29 <META content=
"" name=Platform
>
30 <META content=
"" name=Hardware
>
31 <META content=
"" name=SolutionType
>
32 <META content=kbhowto name=IssueType
>
33 <META content=prodapp name=BoilerPlate
>
34 <META content=
"" name=ProducedView
><LINK href=
"desktop_files/style.css"
35 type=text/css rel=STYLESHEET
>
36 <META content=
"MSHTML 5.50.4207.2601" name=GENERATOR
></HEAD>
37 <BODY bgColor=#ffffff leftMargin=
0 topMargin=
0 rightMargin=
0 MARGINWIDTH=
"0"
38 MARGINHEIGHT=
"0"><!-- Do not redirect KB Articles to the XMLStore at this time --><!--TOOLBAR_START--><A
39 name=TOP
><A name=TOP
></A><!-- Start: ToolBar V2.0--><!--TOOLBAR_EXEMPT-->
40 <SCRIPT language=JavaScript
src=
"desktop_files/toolbar.js"></SCRIPT>
42 <SCRIPT language=JavaScript
src=
"desktop_files/global.js"></SCRIPT>
44 <SCRIPT language=JavaScript
src=
"desktop_files/EN-US_local.js"></SCRIPT>
45 <!-- Start: ToolBar for down-level browsers--><SPAN id=TBDownLevelDiv
>
46 <TABLE cellSpacing=
0 cellPadding=
0 width=
"100%" bgColor=#ffffff border=
0>
49 <TD vAlign=top
width=
"100%" height=
60 rowSpan=
2><A
50 href=
"http://support.microsoft.com/isapi/gosupport.asp?target=/directory/default.asp"><IMG
51 alt=
"Microsoft Product Support Services" src=
"desktop_files/pss_EN-US.gif"
53 <TD vAlign=top align=right height=
20><IMG height=
20 alt=
"" src=
"" width=
18
54 border=
0></TD><!--Start Global Toolbar Section-->
55 <TD vAlign=center noWrap align=right bgColor=#
000000 colSpan=
2
56 height=
20><FONT face=
"Verdana, Arial" color=#ffffff
57 size=
1><B> <A style=
"COLOR: #ffffff; TEXT-DECORATION: none"
59 href=
"http://support.microsoft.com/isapi/gomscom.asp?target=/products/"><FONT
60 color=#ffffff
>All Products
</FONT></A> <FONT
61 color=#ffffff
>|
</FONT> <A
62 style=
"COLOR: #ffffff; TEXT-DECORATION: none" target=_top
63 href=
"http://support.microsoft.com/isapi/gosupport.asp?target=/directory/default.asp?fr=0&sd=gn"><FONT
64 color=#ffffff
>Support
</FONT></A> <FONT color=#ffffff
>|
</FONT>
65 <A style=
"COLOR: #ffffff; TEXT-DECORATION: none" target=_top
66 href=
"http://support.microsoft.com/isapi/gosearch.asp?target=/"><FONT
67 color=#ffffff
>Search
</FONT></A> <FONT color=#ffffff
>|
</FONT>
68 <A style=
"COLOR: #ffffff; TEXT-DECORATION: none" target=_top
69 href=
"http://support.microsoft.com/isapi/gomscom.asp?target=/"><FONT
70 color=#ffffff
>microsoft.com Home
</FONT></A> </B></FONT></TD><!--End Global Toolbar Section--></TR>
72 <TD vAlign=top width=
19 height=
40><IMG height=
40
73 src=
"desktop_files/1ptrans.gif" width=
19 border=
0></TD>
74 <TD vAlign=top noWrap align=right colSpan=
2 height=
40><A target=_top
75 href=
"http://support.microsoft.com/isapi/gomscom.asp?target=/"><IMG
76 height=
40 alt=microsoft.com
src=
"" width=
112 border=
0></A></TD></TR><!-- Start: Black Line Spacer -->
78 <TD noWrap bgColor=#
000000 colSpan=
4 height=
1></TD></TR><!-- End: Black Line Spacer -->
79 <TR><!-- Start: Local menus -->
80 <TD vAlign=center noWrap bgColor=#
000000 colSpan=
4 height=
20><FONT
81 face=
"Verdana, Arial" color=#ffffff size=
1><B> <A
82 title=
"Support Home" style=
"COLOR: #ffffff; TEXT-DECORATION: none"
84 href=
"http://support.microsoft.com/isapi/gosupport.asp?target=/directory/default.asp"><FONT
85 color=#ffffff
>Support Home
</FONT></A> <FONT
86 color=#ffffff
>|
</FONT> <A title=
"Self Support"
87 style=
"COLOR: #ffffff; TEXT-DECORATION: none" target=_top
88 href=
"http://support.microsoft.com/isapi/gosupport.asp?target=/directory/self.asp?sd=gn"><FONT
89 color=#ffffff
>Self Support
</FONT></A> <FONT
90 color=#ffffff
>|
</FONT> <A title=
"Assisted Support"
91 style=
"COLOR: #ffffff; TEXT-DECORATION: none" target=_top
92 href=
"http://support.microsoft.com/isapi/gosupport.asp?target=/directory/getitem.asp?item=AD&sd=gn"><FONT
93 color=#ffffff
>Assisted Support
</FONT></A> <FONT
94 color=#ffffff
>|
</FONT> <A
95 title=
"Comprehensive support customized by customer type"
96 style=
"COLOR: #ffffff; TEXT-DECORATION: none" target=_top
97 href=
"http://support.microsoft.com/isapi/gosupport.asp?target=/directory/customer.asp&sd=gn"><FONT
98 color=#ffffff
>Custom Support
</FONT></A> <FONT
99 color=#ffffff
>|
</FONT> </B></FONT></TD><!-- End: Local menus --></TR><!-- Start: Black Line Spacer -->
101 <TD noWrap bgColor=#
000000 colSpan=
4 height=
1></TD></TR><!-- End: Black Line Spacer --></TBODY></TABLE></SPAN><!-- End: ToolBar For down-level browsers-->
102 <SCRIPT language=JavaScript
>
103 <!--// Hide from old browsers
104 var ToolBar_Supported = ToolBar_Supported;
105 if (ToolBar_Supported != null && ToolBar_Supported == true)
107 TBDownLevelDiv.style.display = "none";
112 <!-- End: ToolBar V2.0--><!--TOOLBAR_END--><!--TOOLBAR_END-->
113 <TABLE cellSpacing=
0 cellPadding=
0 width=
"100%" border=
0>
118 <TABLE cellSpacing=
0 cellPadding=
0 width=
"95%" border=
0>
121 <TD vAlign=top
><FONT face=
"Verdana, Arial, Helvetica" size=
2>
126 <H2>HOWTO: Access the Application Desktop from a
127 Service
</H2></TD></TR></TBODY></TABLE>
129 The information in this article applies to:
<BR>
131 <LI>Microsoft Win32 Application Programming Interface (API), used
134 <LI>The Microsoft Windows NT operating system, versions
3.51,
136 <LI>The Microsoft Windows
2000 operating system
</LI></UL></LI></UL>
140 <P>The configuration of a Windows NT service determines how it may
141 access the application desktop. The application desktop is named
142 "default" and belongs to the
"WinSta0" Window station object. The
143 application desktop is associated with the interactively logged-on
144 user. A process that has access to the application desktop can
145 display message boxes, windows, and dialog boxes that can be seen
146 visually by the interactively logged-on user. In addition, a process
147 with access to the application desktop can send messages to other
148 processes running on the desktop. The application desktop is not
149 destroyed when the interactively logged-on user logs off.
150 <BR><BR>NOTE: Running interactive services under the LocalSystem
151 account is a VERY dangerous practice. This is especially true of the
152 command processor and batch files. A user who wants to control the
153 system can just hit CTRL+C to get an interactive system command
155 <H2>MORE INFORMATION
</H2>
156 <P>A service that is configured in the LocalSystem account and is
157 interacting with the desktop (the service type includes the
158 SERVICE_INTERACTIVE_PROCESS flag) has access to the application
159 desktop.
<BR><BR>A service that is configured in the LocalSystem
160 account and is not interacting with the desktop does not have access
161 to the application desktop by default. If the service needs to
162 display information through a message box, you can do this by
163 specifying one of the following two flag types:
<PRE class=FIXEDTEXT
> MB_DEFAULT_DESKTOP_ONLY - The message box will appear on the application
164 desktop, for example,
"winsta0\default".
165 MB_SERVICE_NOTIFICATION - The message box will appear on the currently
166 active desktop.
</PRE>In addition, a
167 service configured in the LocalSystem account can reconfigure its
168 thread to access the application desktop. This is demonstrated in
169 the following sample code. The sample code obtains handles to the
170 "WinSta0" window station and the
"default" desktop. Then it re-
171 associates the current thread to the application desktop. Then the
172 sample will display a message box and, when access to the
173 interactive desktop is no longer needed, it resets the thread to the
174 original window station and desktop. This must be done for every
175 thread that wants access to the application desktop.
176 <H3>Sample Code
</H3><PRE class=CODESAMP
> BOOL ThreadInteract(void)
180 HWINSTA hwinstaCurrent;
184 // Save the current Window station
186 hwinstaCurrent = GetProcessWindowStation();
187 if (hwinstaCurrent == NULL)
191 // Save the current desktop
193 hdeskCurrent = GetThreadDesktop(GetCurrentThreadId());
194 if (hdeskCurrent == NULL)
198 // Obtain a handle to WinSta0 - service must be running
199 // in the LocalSystem account
201 hwinsta = OpenWindowStation(
"winsta0", FALSE,
202 WINSTA_ACCESSCLIPBOARD |
203 WINSTA_ACCESSGLOBALATOMS |
204 WINSTA_CREATEDESKTOP |
205 WINSTA_ENUMDESKTOPS |
208 WINSTA_READATTRIBUTES |
210 WINSTA_WRITEATTRIBUTES);
215 // Set the windowstation to be winsta0
217 if (!SetProcessWindowStation(hwinsta))
221 // Get the default desktop on winsta0
223 hdesk = OpenDesktop(
"default",
0, FALSE,
225 DESKTOP_CREATEWINDOW |
227 DESKTOP_HOOKCONTROL |
228 DESKTOP_JOURNALPLAYBACK |
229 DESKTOP_JOURNALRECORD |
230 DESKTOP_READOBJECTS |
231 DESKTOP_SWITCHDESKTOP |
232 DESKTOP_WRITEOBJECTS);
237 // Set the desktop to be
"default"
239 if (!SetThreadDesktop(hdesk))
245 MessageBox(NULL,
"MB_OK",
"test_interact", MB_OK);
248 // Reset the Window station and desktop
250 if (!SetProcessWindowStation(hwinstaCurrent))
253 if (!SetThreadDesktop(hdeskCurrent))
257 // Close the windowstation and desktop handles
259 if (!CloseWindowStation(hwinsta))
262 if (!CloseDesktop(hdesk))
266 }
</PRE>A service configured for a user account can access the
267 application desktop through the message box types mentioned above.
268 The above code will fail for a service configured for a user account
269 due to security reasons but it will work for a service that
270 impersonates the interactively logged-on user. For example,
271 impersonation can be done through a named pipe connection from a
272 process running on the application desktop. The service would have
273 to impersonate the named pipe client.
275 <P>Additional query words:
</P></FONT><FONT
276 face=
"Verdana, Arial, Helvetica" size=
1>
277 <P>Keywords : kbKernBase kbOSWin2000 kbSecurity kbService kbDSupport
278 kbGrpKernBase
<BR>Issue type : kbhowto
<BR>Technology : kbOSWinNT
280 </P><!--CONVLEGACY DELIMITER--></FONT></TD></TR></TBODY></TABLE>
282 <TABLE cellSpacing=
0 cellPadding=
0 width=
"100%" border=
0>
286 <CENTER><BR><FONT face=
"Verdana, Arial, Helvetica" size=
1>Last
287 Reviewed: October
21,
2000<BR><A
288 href=
"http://support.microsoft.com/support/misc/cpyright.asp">©
2001
289 Microsoft Corporation. All rights reserved. Terms of
290 Use.
</A><BR></FONT></CENTER></TD></TR></TBODY></TABLE></P></CENTER></TD>
291 <TD vAlign=top width=
1 bgColor=#
003399><IMG height=
"100%"
292 src=
"G:\KIN\PAS\desktop_files\1ptrans(1).gif" width=
1 border=
0></TD>
293 <TD vAlign=top width=
135>
295 <TABLE cellSpacing=
0 cellPadding=
0 width=
"90%" border=
0>
298 <TD vAlign=top
><FONT face=
"Verdana, Arial, Helvetica" size=
1><BR>
299 <P>Article ID: Q173687
</P>
300 <P><B>Last Reviewed:
</B><BR>October
21,
2000 </P>
301 <P><A title=
"Send this article to a friend"
302 href=
"mailto:?subject=Microsoft Knowledge Base Article - Q173687&body=This article pointer was forwarded to you from the Microsoft Online Support site. http://support.microsoft.com/support/kb/articles/Q173/6/87.ASP"><IMG
303 height=
10 src=
"desktop_files/icoEmail.gif" width=
18 align=absMiddle
304 border=
0>Send to a friend
</A><BR><BR>
305 <P>Provided by
<BR><A target=_top
306 href=
"http://support.microsoft.com/directory/default.asp">Microsoft
307 Product Support Services
</A>
311 <FORM name=VOTED
onsubmit=
"return votingsubmit();"
312 action=/support/contentvoting/voting_idc.asp method=post
><INPUT
313 type=hidden value=Q173687 name=KBID
> <INPUT type=hidden
314 value=
"Support; KB; win32sdk" name=PROD
> <INPUT type=hidden
315 value=
"Support; KB; win32sdk" name=KBAREA
> <INPUT type=hidden
316 value=support.microsoft.com name=SRV
> <INPUT type=hidden
317 value=/support/kb/articles/Q173/
6/
87.ASP name=URL
> Did the
318 information in this article help answer your question?
319 <P><INPUT type=radio value=
1 name=VOTE
>Yes
<BR><INPUT type=radio
320 value=
2 name=VOTE
>No
<BR><INPUT type=radio value=
3 name=VOTE
>Did not
322 <P>Please provide additional comments about this
323 information.
<BR>(
255 character max)
<BR><BR><TEXTAREA id=COMMENTS
style=
"FONT-WEIGHT: normal; FONT-SIZE: xx-small; LINE-HEIGHT: normal; FONT-STYLE: normal; FONT-VARIANT: normal" name=COMMENTS rows=
5 wrap=virtual cols=
17 MAXLENGTH=
"255"></TEXTAREA>
325 <CENTER><INPUT type=image height=
32 alt=Submit width=
88
326 src=
"desktop_files/votesubmit.gif" border=
0> </CENTER>
327 <SCRIPT language=JavaScript
>
329 function votingsubmit()
331 if((document.VOTED.VOTE[0].checked == false) && (document.VOTED.VOTE[1].checked == false) && (document.VOTED.VOTE[2].checked == false)){
332 alert("Please choose a voting option.");
334 if(document.VOTED.COMMENTS.value.length > 255){
335 alert("Your comment has exceeded the 255 character limit.\nPlease reduce the number of characters in your comment.");
336 document.forms[0].COMMENTS.focus();
343 <P></P></FONT></TD></TR></TBODY></TABLE></CENTER></TD></TR></TBODY></TABLE></BODY></HTML>