Created
February 1, 2017 15:28
-
-
Save Mattie/912fc76b090e7ec4facf66d930e6e0ac to your computer and use it in GitHub Desktop.
Taken from https://www.citrix.com/blogs/2010/03/02/fun-with-the-ica-client-object-ico-and-net-console-applications/ The following code example illustrates the use of Client Simulation Keyboard APIs to type within our Notepad application session, take a screen shot and save it to the %temp% directory, and finally enumerate and output the top leve…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// This program demo's the basic of launching and ICO session from within an application. | |
/// </summary> | |
class Program | |
{ | |
/// <summary> | |
/// Auto Reset Event used to block execution until we receive the OnLogon event | |
/// </summary> | |
public static AutoResetEvent onLogonResetEvent = null; | |
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 = "#Notepad"; | |
// Launch a new session | |
ica.Launch = true; | |
// Set Server address | |
ica.Address = "10.8.X.X"; | |
// No Password property is exposed (for security) | |
// but can explicitly specify it by using the ICA File "password" property | |
ica.Username = "johncat"; | |
ica.Domain = "xxxx"; | |
ica.SetProp("Password", "xxxx"); | |
// 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 logon event | |
ica.OnLogon += new _IICAClientEvents_OnLogonEventHandler(ica_OnLogon); | |
// Launch/Connect to the session | |
ica.Connect(); | |
if (onLogonResetEvent.WaitOne(new TimeSpan(0, 2, 0))) | |
Console.WriteLine("Session Logged on sucessfully! And OnLogon Event was Fired!"); | |
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! :("); | |
// Give notepad a few seconds to setup | |
// (or detect the window as seen below) | |
// Obvious Tips: Sleep is usually bad in automation, race conditions ... now you know | |
Thread.Sleep(5000); | |
//////////////////////////////////////////////////////////////////// | |
// Keyboard Simulation | |
// Let's script some key input with the Keyboard interface | |
Keyboard kbrd = ica.Session.Keyboard; | |
// "Greatness has been achieved!" - Indeed! | |
kbrd.SendKeyDown((int)Keys.G); | |
kbrd.SendKeyDown((int)Keys.R); | |
kbrd.SendKeyDown((int)Keys.E); | |
kbrd.SendKeyDown((int)Keys.A); | |
kbrd.SendKeyDown((int)Keys.T); | |
kbrd.SendKeyDown((int)Keys.N); | |
kbrd.SendKeyDown((int)Keys.E); | |
kbrd.SendKeyDown((int)Keys.S); | |
kbrd.SendKeyDown((int)Keys.S); | |
kbrd.SendKeyDown((int)Keys.Space); | |
kbrd.SendKeyDown((int)Keys.H); | |
kbrd.SendKeyDown((int)Keys.A); | |
kbrd.SendKeyDown((int)Keys.S); | |
kbrd.SendKeyDown((int)Keys.Space); | |
kbrd.SendKeyDown((int)Keys.B); | |
kbrd.SendKeyDown((int)Keys.E); | |
kbrd.SendKeyDown((int)Keys.E); | |
kbrd.SendKeyDown((int)Keys.N); | |
kbrd.SendKeyDown((int)Keys.Space); | |
kbrd.SendKeyDown((int)Keys.A); | |
kbrd.SendKeyDown((int)Keys.C); | |
kbrd.SendKeyDown((int)Keys.H); | |
kbrd.SendKeyDown((int)Keys.I); | |
kbrd.SendKeyDown((int)Keys.E); | |
kbrd.SendKeyDown((int)Keys.V); | |
kbrd.SendKeyDown((int)Keys.E); | |
kbrd.SendKeyDown((int)Keys.D); | |
// Exclamation ! | |
kbrd.SendKeyDown((int)Keys.ShiftKey); | |
kbrd.SendKeyDown((int)Keys.D1); | |
//////////////////////////////////////////////////////////////////// | |
// ScreenShots | |
// Let's take a screen shot of our session | |
ScreenShot ss = ica.Session.CreateFullScreenShot(); | |
ss.Filename = Path.GetTempPath() + Path.GetRandomFileName() + ".bmp"; | |
Console.WriteLine("Saving Screen Shot to: " + ss.Filename); | |
ss.Save(); | |
/////////////////////////////////////////////////////////////////// | |
// Session Windows Enumeration | |
// Enumerate top level windows and output them | |
Windows allTopLevelWindows = ica.Session.TopLevelWindows; | |
if (allTopLevelWindows.Count > 0) | |
{ | |
Console.WriteLine("\nDisplaying all Top Level Windows:"); | |
foreach (window w in allTopLevelWindows) | |
{ | |
Console.Write("\n"); | |
Console.WriteLine("Window ID: " + w.WindowID); | |
Console.WriteLine("Window Caption: " + w.Caption); | |
Console.WriteLine("Window Position X: " + w.PositionX); | |
Console.WriteLine("Window Position Y: " + w.PositionY); | |
} | |
} | |
Console.WriteLine("\nPress any key to log off"); | |
Console.Read(); | |
// Logoff our session | |
Console.WriteLine("Logging off Session"); | |
ica.Logoff(); | |
} | |
/// <summary> | |
/// OnLogon Event Handler | |
/// </summary> | |
static void ica_OnLogon() | |
{ | |
Console.WriteLine("OnLogon event was Handled!"); | |
onLogonResetEvent.Set(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment