cancel
Showing results for 
Search instead for 
Did you mean: 

Code Stage - API

BartWille
Level 3
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 & sBuffer & sBuffer & sBuffer & sBuffer sBuffer = sBuffer & sBuffer lHandle = GetForegroundWindow lTextLength = GetWindowText(lHandle, sBuffer, 100) sBuffer = UCase(Left(sBuffer, lTextLength))
2 REPLIES 2

MarkusKrahn
Level 3
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 & sBuffer & sBuffer & sBuffer & sBuffer sBuffer = sBuffer & 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

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.

I thought to post my solution here, so everyone who comes across this thread might benefit:

Global Code:

'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


Code Step: Get Active Window
  Inputs: -
  Outputs: Active_Window (Number)

Active_Window = GetForegroundWindow()


Code Step: Get Active Window Title
  Inputs: hWnd (Number) <-- This is the Active_Window from the previous step
  Outputs: Window_Title (Text), Window_Title_Length (Number)

'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()


Hope this helps 🙂



------------------------------
Ivo Goudzwaard
------------------------------