1
Reply

WTSLogoffSession to logoff remote users on Windows 7??

Christian Brayshaw

Christian Brayshaw

Dec 21 2009 9:05 AM
8k
Guys,

I have an application that I wrote and maintain last year which manages users logged on to a terminal server or client machine. These include XP, 2003, 2008, Vista, 2008 R2 and Windows 7.

Currently I can logoff users remotely on all but 1 operating system - Windows 7. For some reason, I cannot find why, I cannot logoff users remotely using this API.

I run my application as a Domain Admin, so access rights shouldn't be an issue.
API Call:

 // Logoff session
        [DllImport("wtsapi32.dll", SetLastError = true)]
        static extern bool WTSLogoffSession(IntPtr hServer, int SessionId, bool bWait);
Method used:

 public bool UserLogoff(string servername, int sessionID)
        {
            hWTSServerHandle = IntPtr.Zero;

            try
            {
                hWTSServerHandle = WTSOpenServer(servername);

                if (Convert.ToInt32(hWTSServerHandle.ToString()) > 0)
                {
                    WTSLogoffSession(hWTSServerHandle, sessionID, true);
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("UserLogoff: Exception occured!\n" + e.ToString());
                return false;
            }
            finally
            {
                WTSCloseServer(hWTSServerHandle);
            }

            return true;
        }
Any ideas?

Answers (1)