[code language=”csharp” title=”C# .Net”]
/********************************************************************
* 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);
}
}
}
}
[/code]
[code language=”powershell” title=”Powershell”]
$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()
[/code]
Download ICA_Disconnect Binary
Feedback
[contact-form subject='[Siva Mulpuru%26#039;s Blog’][contact-field label=’Name’ type=’name’ required=’1’/][contact-field label=’Email’ type=’email’/][contact-field label=’Comment’ type=’textarea’ required=’1’/][/contact-form]
Leave a comment