Disconnect ICA session from inside

/********************************************************************
* Citrix Server SDK WFAPI Component 
* Author: Siva Mulpuru
* The following code invokes the WFDisconnectSession in WFAPI.dll
* Works on XenDesktop and Xenapp ICA session
**********************************************************************/

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace ICA_Disconnect
{
    class Program
    {
        const int CurrentSession = -1;
        static readonly IntPtr CurrentServer = IntPtr.Zero;
        [DllImport("WFAPI.dll", EntryPoint = "WFDisconnectSession", SetLastError = true)]
        private static extern bool WFDisconnectSession(IntPtr hServer,
            int iSessionId, int waitForDisconnect);
        static void Main(string[] args)
        {
            try
            {
                if (WFDisconnectSession(CurrentServer, CurrentSession, 0))
                    Console.WriteLine("ICA Session disconnected @ {0}", DateTime.Now.ToString("MM/dd/yyyy HH:mm"));

                else
                {
                    string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;
                    Console.WriteLine("WFDisconnectSession failed; Win32 Error: {0}", errorMessage);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("\nException Caught: ---\n{0}", ex.Message);
                
            }
        }
       
    }
}
$signature = @'

const int CurrentSession = -1;
static readonly IntPtr CurrentServer = IntPtr.Zero;

[DllImport("WFAPI.dll", EntryPoint = "WFDisconnectSession")]
private static extern bool WFDisconnectSession(IntPtr hServer, 
    int iSessionId, int bwait);

public static bool ICADisconnect() {
    bool ok = WFDisconnectSession(CurrentServer, CurrentSession, 0);
    if (!ok) return false;
    return true;
}

'@

Add-Type -MemberDefinition $signature -Name CustomName -Namespace CustomNamespace -PassThru
[CustomNamespace.CustomName]::ICADisconnect()

Download ICA_Disconnect Binary

ICA_Disconnect.exe

Feedback

One thought on “Disconnect ICA session from inside

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s