<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic RE: Hey Bart, in Product Forum</title>
    <link>https://community.blueprism.com/t5/Product-Forum/Code-Stage-API/m-p/64642#M17386</link>
    <description>&lt;P&gt;Sorry to dig up this old thread, but I came across the same requirement as Bart some time ago, and I solved it using BP 6.6 and a StringBuilder object.&lt;/P&gt;
&lt;P&gt;I thought to post my solution here, so everyone who comes across this thread might benefit:&lt;/P&gt;
&lt;P&gt;Global Code:&lt;/P&gt;
&lt;PRE class="language-markup"&gt;'Gets the current active (foreground) window
Declare Function GetForegroundWindow Lib "user32.dll" As IntPtr

'Gets the currrent active window's title
Declare Function GetWindowTextA Lib "user32.dll" _
	(ByVal hWnd As IntPtr, _
	ByVal lpString As StringBuilder, _
	ByVal cch As Integer) As Integer&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;Code Step: Get Active Window&lt;BR /&gt;&amp;nbsp; Inputs: -&lt;BR /&gt;&amp;nbsp; Outputs: Active_Window (Number)&lt;/P&gt;
&lt;PRE class="language-markup"&gt;Active_Window = GetForegroundWindow()&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;Code Step: Get Active Window Title&lt;BR /&gt;&amp;nbsp; Inputs: hWnd (Number) &amp;lt;-- This is the Active_Window from the previous step&lt;BR /&gt;&amp;nbsp; Outputs: Window_Title (Text), Window_Title_Length (Number)&lt;/P&gt;
&lt;PRE class="language-markup"&gt;'Declare a sting builder object in order to build the title string
Dim caption As New System.Text.StringBuilder(256)

'Get the window title length by calling the imported function
Window_Title_Length = GetWindowTextA(hWnd, caption, caption.Capacity)

'Retrieve the window title from the string builder
Window_Title = caption.ToString()&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;Hope this helps &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;BR /&gt;&lt;BR /&gt;------------------------------&lt;BR /&gt;Ivo Goudzwaard&lt;BR /&gt;------------------------------&lt;BR /&gt;</description>
    <pubDate>Tue, 21 Apr 2020 13:23:00 GMT</pubDate>
    <dc:creator>IvoGoudzwaard</dc:creator>
    <dc:date>2020-04-21T13:23:00Z</dc:date>
    <item>
      <title>Code Stage - API</title>
      <link>https://community.blueprism.com/t5/Product-Forum/Code-Stage-API/m-p/64640#M17384</link>
      <description>Goal: check whether the title of the window in the foreground corresponds with a given string.

The VBO Utility - General - Window Exists action checks more than just the foreground window. So I tried by creating a Code Stage.

Note: the below code works in the VBA Editor. It fails in Blue Prism. In Blue Prism the handle is properly returned, but the lTextLength returns always 0.

No error messages are returned by Blue Prism.

Initialise - Global Code:
Private Declare Function GetForegroundWindow Lib "user32.dll" _
	() As Long

Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" _
	(ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Action: Get Window Title
Dim lHandle As Long
Dim lTextLength As Long

sBuffer = "          "
sBuffer = sBuffer &amp;amp; sBuffer &amp;amp; sBuffer &amp;amp; sBuffer &amp;amp; sBuffer
sBuffer = sBuffer &amp;amp; sBuffer

lHandle = GetForegroundWindow
lTextLength = GetWindowText(lHandle, sBuffer, 100)
sBuffer = UCase(Left(sBuffer, lTextLength))</description>
      <pubDate>Wed, 08 Mar 2017 22:07:00 GMT</pubDate>
      <guid>https://community.blueprism.com/t5/Product-Forum/Code-Stage-API/m-p/64640#M17384</guid>
      <dc:creator>BartWille</dc:creator>
      <dc:date>2017-03-08T22:07:00Z</dc:date>
    </item>
    <item>
      <title>Hey Bart,</title>
      <link>https://community.blueprism.com/t5/Product-Forum/Code-Stage-API/m-p/64641#M17385</link>
      <description>Hey Bart,
To get an idea on what might cause this, Marshal.GetLastWin32Error might help.
EDIT:
I assume sBuffer is an out Text item?
In your stage:
Dim iLasterr As Integer
Dim lHandle As Long
Dim lTextLength As Long
sBuffer = "" ""
sBuffer = sBuffer &amp;amp; sBuffer &amp;amp; sBuffer &amp;amp; sBuffer &amp;amp; sBuffer
sBuffer = sBuffer &amp;amp; sBuffer
lHandle = GetForegroundWindow
lTextLength = GetWindowText(lHandle, sBuffer, 100)
sBuffer = UCase(Left(sBuffer, lTextLength))
iLasterr = Marshal.GetLastWin32Error()
'put iLasterr in a data item and then look up and see if the win32 error code can point you to the cause
-----------
BR
Markus</description>
      <pubDate>Thu, 09 Mar 2017 13:34:00 GMT</pubDate>
      <guid>https://community.blueprism.com/t5/Product-Forum/Code-Stage-API/m-p/64641#M17385</guid>
      <dc:creator>MarkusKrahn</dc:creator>
      <dc:date>2017-03-09T13:34:00Z</dc:date>
    </item>
    <item>
      <title>RE: Hey Bart,</title>
      <link>https://community.blueprism.com/t5/Product-Forum/Code-Stage-API/m-p/64642#M17386</link>
      <description>&lt;P&gt;Sorry to dig up this old thread, but I came across the same requirement as Bart some time ago, and I solved it using BP 6.6 and a StringBuilder object.&lt;/P&gt;
&lt;P&gt;I thought to post my solution here, so everyone who comes across this thread might benefit:&lt;/P&gt;
&lt;P&gt;Global Code:&lt;/P&gt;
&lt;PRE class="language-markup"&gt;'Gets the current active (foreground) window
Declare Function GetForegroundWindow Lib "user32.dll" As IntPtr

'Gets the currrent active window's title
Declare Function GetWindowTextA Lib "user32.dll" _
	(ByVal hWnd As IntPtr, _
	ByVal lpString As StringBuilder, _
	ByVal cch As Integer) As Integer&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;Code Step: Get Active Window&lt;BR /&gt;&amp;nbsp; Inputs: -&lt;BR /&gt;&amp;nbsp; Outputs: Active_Window (Number)&lt;/P&gt;
&lt;PRE class="language-markup"&gt;Active_Window = GetForegroundWindow()&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;Code Step: Get Active Window Title&lt;BR /&gt;&amp;nbsp; Inputs: hWnd (Number) &amp;lt;-- This is the Active_Window from the previous step&lt;BR /&gt;&amp;nbsp; Outputs: Window_Title (Text), Window_Title_Length (Number)&lt;/P&gt;
&lt;PRE class="language-markup"&gt;'Declare a sting builder object in order to build the title string
Dim caption As New System.Text.StringBuilder(256)

'Get the window title length by calling the imported function
Window_Title_Length = GetWindowTextA(hWnd, caption, caption.Capacity)

'Retrieve the window title from the string builder
Window_Title = caption.ToString()&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;Hope this helps &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;BR /&gt;&lt;BR /&gt;------------------------------&lt;BR /&gt;Ivo Goudzwaard&lt;BR /&gt;------------------------------&lt;BR /&gt;</description>
      <pubDate>Tue, 21 Apr 2020 13:23:00 GMT</pubDate>
      <guid>https://community.blueprism.com/t5/Product-Forum/Code-Stage-API/m-p/64642#M17386</guid>
      <dc:creator>IvoGoudzwaard</dc:creator>
      <dc:date>2020-04-21T13:23:00Z</dc:date>
    </item>
  </channel>
</rss>

