Tuesday, January 15, 2013

Scan Window titles

[local function declaration]
FUNCTION LONG GetDesktopWindow() LIBRARY "user32"
FUNCTION LONG GetWindow( long hWnd, long uCmd ) LIBRARY "user32"
FUNCTION LONG GetClassNameA &
   ( long hWnd, Ref String lpClassName, long nMaxCount ) &
      LIBRARY "user32"
FUNCTION LONG GetWindowTextA &
   ( long hWnd, Ref String lpString, long nMaxCount ) &
      LIBRARY "user32"
FUNCTION BOOLEAN IsWindowVisible( long hWnd ) LIBRARY "user32"

[powerscript]
long ll_Desktop,ll_Child
 
string ls_ClassName, ls_WindowName, ls_ieopened
 
Constant long GW_HWNDFIRST = 0
Constant long GW_HWNDLAST = 1
Constant long GW_HWNDNEXT = 2
Constant long GW_HWNDPREV = 3
Constant long GW_OWNER  = 4
Constant long GW_CHILD  = 5
Constant long GW_MAX   = 5
 
Constant long MAX_WIDTH = 255
 
ll_Desktop = GetDesktopWindow()
 
ll_Child = GetWindow( ll_Desktop, GW_CHILD )
 
ls_ieopened = ""
DO WHILE (ll_Child > 0)
   ls_ClassName  = Space(MAX_WIDTH)
   ls_WindowName = Space(MAX_WIDTH)
   // Get window classname
   GetClassNameA( ll_Child, ls_ClassName, MAX_WIDTH )
   // Get window text
   GetWindowTextA( ll_Child, ls_WindowName, MAX_WIDTH )
 
   IF ls_classname = "IEFrame" THEN
      ls_ieopened += ls_ClassName + "(" + ls_WindowName + ") "
   END IF
   ll_Child = GetWindow( ll_Child, GW_HWNDNEXT )
LOOP 
 
Messagebox("" , ls_ieopened)

Detect if running in PB or executable

IF Handle(GetApplication()) = 0 THEN
   MessageBox("Info", "Running in PB environment")
ELSE
   MessageBox("Info", "Running in standalone executable")
END I

Terminate an another application

[local external function declaration]
FUNCTION ulong FindWindowA(ulong classname, String windowname) & 
  LIBRARY "user32.dll"
FUNCTION boolean PostMessageA&
  (ulong hwndle,UINT wmsg,ulong wParam,ulong lParam) & 
  LIBRARY "user32.dll"
  
  
[powerscript]
CONSTANT uint WM_QUIT = 18 // hex 0x0012
ulong     lul_handle
string    ls_app

ls_app = "Calculator"
//  ls_app = "Calculatrice"  in french windows!

lul_handle = FindWindowA(0, ls_app)

IF lul_handle > 0 THEN 
   PostMessageA(lul_handle, WM_QUIT, 0, 0);
ELSE
   MessageBox("Oups", ls_app + " is not running!")
END IF

Evaluate an expression

datastore lds_temp

string ls_result
string ls_expession

string ls_err
// a dummy minimal datastore definition
string ls_dsdef = &
'release 6; datawindow() table(column=(type=char(255) name=a dbname="a") )'

lds_temp = CREATE datastore
lds_temp.Create(ls_dsdef, ls_err)
lds_temp.InsertRow(0)

IF Len(ls_err) > 0 THEN
   MessageBox("ds create", ls_err)
ELSE
   ls_expression = "(2 + 7)  / 3"
   ls_result = &
     lds_temp.Describe ( 'evaluate( " ' + ls_expression + ' " ,1)' )
   MessageBox("ds evaluate", ls_result)
END IF
DESTROY lds_temp

Retrieve an environment variable

// PB6 
ContextKeyword lcxk_base
string ls_Path
string ls_values[]

this.GetContextService("Keyword", lcxk_base)
lcxk_base.GetContextKeywords("path", ls_values)
IF Upperbound(ls_values) > 0 THEN
   ls_Path = ls_values[1]
ELSE
   ls_Path = "*UNDEFINED*"
END IF
 
 
============================================ 
ALLUSERSPROFILE      location of the All Users Profile.
APPDATA              location where applications store data by default.
CD                   current directory string.
CLIENTNAME           client's NETBIOS name when connected to terminal server session.
CMDCMDLINE           command line used to start the current cmd.exe.
CMDEXTVERSION        version number of the current Command Processor Extensions.
CommonProgramFiles   path to the Common Files folder.
COMPUTERNAME         name of the computer.
COMSPEC              path to the command shell executable.
DATE                 current date.
ERRORLEVEL           error code of the most recently used command.
HOMEDRIVE            drive letter is connected to the user's home directory.
HOMEPATH             full path of the user's home directory.
HOMESHARE            network path to the user's shared home directory.
LOGONSEVER           name of the domain controller that validated the current logon session.
NUMBER_OF_PROCESSORS   number of processors installed on the computer.
OS                   name of the operating system. 
                     (Windows XP and Windows 2000 list the operating system as Windows_NT.)
Path                 search path for executable files.
PATHEXT              file extensions that the operating system considers to be executable.
PROCESSOR_ARCHITECTURE   processor's chip architecture.
PROCESSOR_IDENTFIER  description of the processor.
PROCESSOR_LEVEL      model number of the computer's processor.
PROCESSOR_REVISION   revision number of the processor.
ProgramFiles         path to the Program Files folder.
PROMPT               command-prompt settings for the current interpreter.
RANDOM               random decimal number between 0 and 32767.
SESSIONNAME          connection and session names when connected to terminal server session.
SYSTEMDRIVE          drive containing the Windows root directory.
SYSTEMROOT           location of the Windows root directory.
TEMP and TMP         default temporary directories for applications that are available to 
                     users who are currently logged on.
TIME                 current time.
USERDOMAIN           name of the domain that contains the user's account.
USERNAME             name of the user currently logged on.
USERPROFILE          location of the profile for the current user.
WINDIR               location of the OS directory.

Get the executable name

[local external function declaration]
FUNCTION ulong GetModuleFileName (ulong hinstModule, ref string lpszPath, ulong cchPath )  &
   LIBRARY "KERNEL32.DLL" &
   ALIAS FOR "GetModuleFileNameA;ansi"  // ;ansi  required for PB10 or better

[powerscript]
ClassDefinition  lcd

String ls_fullpath 
ulong lul_handle, lul_length = 512

IF handle(getapplication()) = 0 THEN
    // running from the IDE
    lcd=getapplication().classdefinition
    ls_fullpath = lcd.libraryname
ELSE
    // running from EXE
    lul_handle = handle( getapplication() )
    ls_fullpath=space(lul_length) 
    GetModuleFilename( lul_handle, ls_fullpath, lul_length )
END IF

MessageBox("", ls_fullpath)

Wednesday, January 2, 2013

Open the Webapge in PowerBuilder

integer li_rc
inet lcx_inet
li_rc = getcontextservice('Internet',lcx_inet)
If (li_rc <> 1) THEN
    messagebox('Clicked','getcontextservice failed')
ELSE
    lcx_inet.hyperlinktourl('c:\temp\error.xml')
END IF