Logging off

  • Thread starter Thread starter Frank Rizzo
  • Start date Start date
F

Frank Rizzo

I have the following situation:

1. Application X1 runs under a regular user account (this user is also
the currently logged on user).

2. Application X1 kicks off Application X2 using an Administrator
account and then quits itself.

3. Application X2 does a couple of things, then restarts Application X1
using the regular User account, then Application X2 quits itself.

4. Application X1 then needs to log off the system. It calls calls Win
API calls to enable log-off priviliges, then it calls ExitWindowsEx.
The api function kills the running app (e.g. X1), but the log-off itself
never happens.

The log-off works perfectly fine from the application running under the
regular user account, if it is not kicked off by an application running
under another account.

What am I missing here to make the log-off work?
Regards
 
Hi Frank,

Can you show some code about how you lauch application from another
application with differen account and how you call API to enable log-off
priviliges? Also, if you first lauch APP X2, and then run APP X1 from it
and call ExitWindowsEx, will this make it successful?

Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Luke said:
Hi Frank,

Can you show some code about how you lauch application from another
application with differen account and how you call API to enable log-off
priviliges? Also, if you first lauch APP X2, and then run APP X1 from it
and call ExitWindowsEx, will this make it successful?
First of all, sorry for not replying in ascii - the code looks
unreadable in it.
Ok, here is how I kick off an executable. This routine is used by both
applications. It is fairly simply. The method separates the executable
from its arguments, applies user name and password and kicks off the
application. Note that Process.StartInfo.Domain is not set because we
are dealing with local accounts only.

public static Process RunExecutable(string exe, string userName,
string password)
{
string fileName = string.Empty;
string arguments = string.Empty;
bool useCredentials = false;
const string EXE_INDICATOR = ".exe";

// break up the exe parameter into filename and arguments
// basically look for data after .exe string

int iPos = exe.ToLower().IndexOf(EXE_INDICATOR);
if (iPos > 0)
{
fileName = exe.Substring(0, iPos + EXE_INDICATOR.Length);

if (exe.Length > iPos + EXE_INDICATOR.Length + 1)
arguments = exe.Substring(iPos +
EXE_INDICATOR.Length + 1);
}
else
fileName = exe;

Process prc = new Process();

prc.StartInfo.FileName = fileName.Trim();
prc.StartInfo.Arguments = arguments.Trim();

if (userName != string.Empty)
{
prc.StartInfo.UserName = userName;
useCredentials = true;
}

if (password != string.Empty)
{
SecureString ssPassword = new SecureString();
foreach (char c in password.ToCharArray())
ssPassword.AppendChar(c);

prc.StartInfo.Password = ssPassword;
useCredentials = true;
}

if (useCredentials)
{
// when using credentials, MS requires setting the
working directory as described in the following article
//
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref6/html/P_System_Diagnostics_ProcessStartInfo_Password.htm
// So will set the working directory to the current
directory.

// Derive the working directory from the location of the
entry assembly
string fullPath =
System.Reflection.Assembly.GetEntryAssembly().Location;
FileInfo fi = new FileInfo(fullPath);

prc.StartInfo.WorkingDirectory = fi.DirectoryName;

// can't use Shell Execute when using credentials
prc.StartInfo.UseShellExecute = false;
}

prc.Start();
return prc;
}

Next, for logging off I use the WindowsController class from
http://www.mentalis.org/soft/class.qpx?id=7
It is very well documented and it's easy to use. I call
WindowsController.ExitWindows(RestartOptions.LogOff, true);

Regards,
Robert
 
Hi Robert,

The code lokks fine, what is the OS version, windows server 2003 or Vista?
If you use two local administrator accounts, will it also generate the
problem?

Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Any update on this issue?

Sincerely,

Luke Zhang

Microsoft Online Community Support
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hello,

I just check the source code of Windows Controller class, and I found
following code:

protected static void ExitWindows(int how , bool force) {
EnableToken("SeShutdownPrivilege");
if (force)
how = how | EWX_FORCE;
if (ExitWindowsEx(how, 0) == 0)
throw new PrivilegeException(FormatError(Marshal.GetLastWin32Error()));
}
/// <summary>
/// Tries to enable the specified privilege.
/// </summary>
/// <param name="privilege">The privilege to enable.</param>
/// <exception cref="PrivilegeException">There was an error while
requesting a required privilege.</exception>
/// <remarks>Thanks to Michael S. Muegel for notifying us about a bug in
this code.</remarks>
protected static void EnableToken(string privilege ) {
if (Environment.OSVersion.Platform != PlatformID.Win32NT ||
!CheckEntryPoint("advapi32.dll", "AdjustTokenPrivileges"))
return;
IntPtr tokenHandle = IntPtr.Zero;
LUID privilegeLUID = new LUID();
TOKEN_PRIVILEGES newPrivileges = new TOKEN_PRIVILEGES();
TOKEN_PRIVILEGES tokenPrivileges ;
if (OpenProcessToken(Process.GetCurrentProcess().Handle,
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref tokenHandle) == 0)
throw new PrivilegeException(FormatError(Marshal.GetLastWin32Error()));
if (LookupPrivilegeValue("", privilege, ref privilegeLUID) == 0)
throw new PrivilegeException(FormatError(Marshal.GetLastWin32Error()));
tokenPrivileges.PrivilegeCount = 1;
tokenPrivileges.Privileges.Attributes = SE_PRIVILEGE_ENABLED;
tokenPrivileges.Privileges.pLuid = privilegeLUID;
int size = 4;
if (AdjustTokenPrivileges(tokenHandle, 0, ref tokenPrivileges, 4 + (12 *
tokenPrivileges.PrivilegeCount), ref newPrivileges, ref size) == 0)
throw new PrivilegeException(FormatError(Marshal.GetLastWin32Error()));
}


I suggest you may and this class in your project and set break point in the
method of EnableToken(string privilege ). I suspect the problem occur here.
So, may run the application in debug mode, or add and some code to record
log information in the function, to check if there is any problem when
running the application in the real environment, for example, are all the
functions call successful in the method of EnableToken(string privilege )?

Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top