Skip to content

Instantly share code, notes, and snippets.

@karoltheguy
Last active February 1, 2017 19:08
Show Gist options
  • Save karoltheguy/fa8a0668864a44339cbb4c3041252769 to your computer and use it in GitHub Desktop.
Save karoltheguy/fa8a0668864a44339cbb4c3041252769 to your computer and use it in GitHub Desktop.
Start Citrix Session with reference wfica.ocx(not embedded interop)
using System;
using System.Threading;
using WFICALib;
class Program
{
public static AutoResetEvent onLogonResetEvent = null;
/// This program demo's the basic of launching and ICO session from within an application.
static void Main(string[] args)
{
ICAClientClass ica = new ICAClientClass();
onLogonResetEvent = new AutoResetEvent(false);
// Launch published Notepad if you comment this line, and uncommented
// the one above you will launch a desktop session
//ica.Application = "";
ica.InitialProgram = "#appname";
// Launch a new session
ica.Launch = true;
// Set Server address
ica.Address = "servername";
// No Password property is exposed (for security)
// but can explicitly specify it by using the ICA File "password" property
ica.Username = "username";
ica.Domain = "domain";
ica.SetProp("Password", "password here");
// Needed in some cases
ica.EncryptionLevelSession = "EncRC5-128";
// Let's have a "pretty" session
ica.DesiredColor = ICAColorDepth.Color24Bit;
// Reseach the output mode you want, depending on what your trying
// to attempt to automate. The "Client Simulation APIs" are only available under certain modes
// (i.e. things like sending keys to the session, enumerating windows, etc.)
ica.OutputMode = OutputMode.OutputModeNormal;
// Height and Width
ica.DesiredHRes = 1024;
ica.DesiredVRes = 786;
// Register for the OnLogon event
ica.OnLogon += new _IICAClientEvents_OnLogonEventHandler(ica_OnLogon);
// Launch/Connect to the session
ica.Connect();
Session test = ica.Session;
if (onLogonResetEvent.WaitOne(new TimeSpan(0, 2, 0)))
{
Console.WriteLine("Session Logged on sucessfully! And OnLogon Event was Fired!");
//Not Working for me
int AppCount = ica.EnumerateApplications();
for (int i = 0; i < AppCount; i++)
{
string appName = ica.GetEnumNameByIndex(AppCount, i);
Console.WriteLine("Application_" + i + " " + appName);
}
}
else
Console.WriteLine("OnLogon event was NOT Fired! Logging off ...");
// Do we have access to the client simulation APIs?
if (ica.Session == null)
Console.WriteLine("ICA.Session object is NULL! :(");
Console.WriteLine("\nPress any key to log off");
Console.Read();
// Logoff our session
Console.WriteLine("Logging off Session");
ica.Logoff();
}
/// &lt;summary&gt;
/// OnLogon event handler
/// &lt;/summary&gt;
static void ica_OnLogon()
{
Console.WriteLine("OnLogon event was Handled!");
onLogonResetEvent.Set();
}
}
@karoltheguy
Copy link
Author

WFICA.OCX is found in C:\Program Files\Citrix\ICA Client\

If this is running from a 64bit, find it from C:\Program Files\Citrix\ICA Client
It is also important to only compile it as x86.

@prasvenk
Copy link

prasvenk commented Feb 1, 2017

How to run the client application as a Windows service.If I do that connect call is returning error code 13.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment