PowerBuilder Tips & Tricks
Advanced PowerBuilder Tips & Tricks Programming, that I copied from books, website, blog, literature, and other resources.
Showing posts with label Win32 API. Show all posts
Showing posts with label Win32 API. Show all posts

In this example, we populate a listbox with the printers name


/* Get Printer List */
string printers[]
int rtn, i, nbPrinters
rtn = RegistryKeys &
("HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Print\Printers", &
printers)
nbPrinters = UpperBound(printers)
FOR i = 1 TO nbPrinters
lb_1.addItem(printers[i])
NEXT

 

With this simple tips you can hide the application on Windows Taskbar even if its running
1. Declare Win32 API module on Declare -> Local External Functions:

FUNCTION boolean SetWindowLongA (ulong hWnd, int nIndex, long newValue) Library "user32.dll"


2. Add below codes on Open event on main window
IF NOT SetWindowLongA(Handle(This), -20,128) THEN
messageBox ('Error','Cannot Hide!')
END IF


In order the code to take effect you should compile your application into EXE first

 

Hiding Desktop and Taskbar

Posted In: . By popo

With this tips you can easily hide the desktop and taskbar from the user

1. Declare win32 API module on Declare -> Local External FUnctions

Function Long FindWindowEx (Long hwnd1, Long hwnd2, String lpsz1, String lpsz2) Library "user32" Alias For "FindWindowExA"
Function Long ShowWindow (Long hwnd, Long nCmdShow) Library "user32" Alias For "ShowWindow"


2. Declare constants below on Declare -> Instance Variables
Constant Long SW_HIDE = 0
Constant Long SW_NORMAL = 1
Constant Long SW_SHOWMINIMIZED = 2
Constant Long SW_SHOWMAXIMIZED = 3
Constant Long SW_SHOWNOACTIVATE = 4
Constant Long SW_SHOW = 5
Constant Long SW_MINIMIZE = 6
Constant Long SW_SHOWMINNOACTIVE = 7
Constant Long SW_SHOWNA = 8
Constant Long SW_RESTORE = 9
Constant Long SW_SHOWDEFAULT = 10


3. Type the example codes below to hide the desktop and taskbar
String ls_ShellViewWnd = "Progman"
String ls_ShellTaskBarWnd = "Shell_TrayWnd"
String ls_Null
Long ll_HTaskBar, ll_HDeskTop

setNull (ls_Null)
//Hide TaskBar
ll_HTaskBar = FindWindowEx ( 0, 0, ls_ShellTaskBarWnd, ls_Null)
ShowWindow ( ll_HTaskBar, SW_HIDE )
//Hide Desktop
ll_HDeskTop = FindWindowEx ( 0, 0, ls_ShellViewWnd, ls_Null )
ShowWindow ( ll_HDeskTop, SW_HIDE )


To show up the desktop and taskbar again, use codes below
//Show Task Bar
ll_HTaskBar = FindWindowEx ( 0, 0, ls_ShellTaskBarWnd, ls_Null)
ShowWindow ( ll_HTaskBar, SW_SHOW )
//Show Desktop
ll_HDeskTop = FindWindowEx ( 0, 0, ls_ShellViewWnd, ls_Null )
ShowWindow ( ll_HDeskTop, SW_SHOW )

 

In this tips, I would like to discuss how to make an application run only once at a time. When user try to run your application, first try to see if it is already running. If it is already running bring it to top and return. There are some sleek SDK functions you can make use of to implement this. The following are the steps to create this feature:

  1. Create a "Custom Class UserObject"
    Custom Class PowerBuilder UserObject
  2. Set the AutoInstantiate to TRUE
  3. Declare the Win32 API modul at the UserObject on Declare -> Local External Functions
    Function Long GetLastError () Library 'kernel32.dll'
    Function ULong CreateMutex (ULong lpsa, Boolean fInitialOwner, String lpszMutexName) Library 'kernel32.dll' Alias for CreateMutexA
  4. Create a UserObject Function like this:
    Create UserObject Function - isRunning
    String ls_name
    If Handle(GetApplication()) > 0 Then
    ls_name = GetApplication().AppName + Char(0)
    CreateMutex(0, True, ls_name)
    If GetLastError() = 183 Then Return True
    End If
    Return False
  5. Save the UserObject as uo_mutex (for example)
Below is how to use the object. Put this code at the beginning of open event from your Application object:
uo_mutex lou_mutex
If lou_mutex.uf_isrunning() Then
MessageBox ('Warning', 'Application is already running', StopSign!)
HALT CLOSE
End If
//...
// Your next line code
//...

 

C/C++ Datatype Conversion

Posted In: . By popo

If you want to use the Win32 API within PowerBuilder, you have to know the prototype modul. The following list is not an exhaustive list of all C and C++ to PowerBuilder Datatype but most of the common Datatype that I've used over the years of interfacing PowerBuilder and C/C++ programs. There are two lists, the first is PowerBuilder to C++ Datatype, useful for the C Proxy Generator. Then there is a list of C/C++ to PowerBuilder Datatype useful for people writing DLL's and the like:

PowerBuilder DatatypeC/C++ Datatype
BlobPBBlob
Booleanint
Characterchar
DatePBDate
DateTimePBDateTime
DecimalPBDecimal
Doubledouble
Integerint
Realfloat
StringPBString
TimePBTime
UnSignedIntegerunsigned int

C/C++ DatatypePowerBuilder Datatype
BOOLBoolean
WORDUnSignedInteger
DWORDUnSignedLong
HANDLEUnSignedLong
HWNDUnSignedLong
LPSTRString Ref
LPBYTEString Ref
LPINTLong Ref
charBlob {1}
intInteger
unsigned intUnsignedInt
longLong
ULONG/unsigned longUnsignedLong
doubleDouble
char *String Ref

 

Having problems with OCX's which work fine in development and EXE on your machine but when you ship the EXE to a users machine and install your program the OCX does not work?

The problem is that the way OCX's are designed to work is that every OCX should attempt on its own to register with the system during its constructor event. This is done by the container calling a function that is in EVERY OCX called DLLRegisterServer.

The problem is that PowerBuilder does not call this function. Even if you run the REGSRV utility, the OCX will fail to register itself. To correct this problem, in object where you are using the OCX you need to goto the constructor event and call the OCX DLLRegesterServer function.

Declare a local external function in the container object

Function long DllRegisterServer() Library "ocxname.OCX"

In the constructor event

LONG ll_RC
ll_RC = DllRegisterServer()

 

Using the Get Volume API tip previously posted will return the names of the drives connected to your machine. Unfortunately for network drives this returns the name of the physical drive not the name of the share you have mapped to. If you have many shares mapped to a single physical network drive you will not be able to distinguish between the drives.

To improve this situation you can use an API call to return the name of the network share. This is more meaningful and easier for the user. The API call you will need to use is WNetGetConnectionA. To use the API call declare the local external function as follows:

function ulong WNetGetConnectionA( string szLocalPath, &
ref string szNameBuffer, ref ulong lpBufferSize) Library "mpr.dll"

Then in your script you can call the API as follows:
Ulong lul_Max
Long ll_RC
String ls_Drive, ls_Volume

// hard coded for the f: drive
ls_Drive = 'f:'
lul_Max = 2000

// pre allocate string to stop GPF
ls_Volume = Space( 2000 )

// call the API function, the share name
// will be in the ls_Volume variable
ll_RC = WNetGetConnectionA( ls_Drive, ls_Volume, lul_Max )
ls_Volume = Trim( ls_Volume )

 

Get Volume Information

Posted In: . By popo

In order to show a list of Drives We needed to get the name of the Drive. The following API call gets the volume name as well as the serial number.

Create an NVO add the following local external function:

function long GetVolumeInformationA( ref string ls_RootPath, &
ref string ls_VolName, long ll_VolLen, ref string ls_volserial, &
long ll_maxcomplen, long ll_systemflags, &
ref string ls_SystemName,&
long ll_SystemLen ) Library 'kernel32'

Then add a function called GetVolumeName which accepts a string and returns a string and add the following code:
// Call the API function to get the volume label from a drive letter
Long ll_Max, ll_Flags, ll_Ret, ll_FileSys
Long ll_Vol

String ls_Vol
String ls_Drv, ls_FileSys, ls_Flags, ls_Serial


ls_Drv = as_Vol
ls_Vol = Space(32)
ls_FileSys = Space(32)
ls_Serial = Space(32)
ll_Vol = Len( ls_Vol )
ll_FileSys = Len( ls_Filesys )

ll_Ret = GetVolumeInformationA( ls_Drv, ls_Vol, ll_Vol, &
ls_Serial, ll_Max, ll_Flags, ls_FileSys, ll_FileSys )

IF (ll_Ret = 0) THEN
ls_Vol = ''
ELSE
ls_Vol = Trim( ls_Vol )
END IF

RETURN (ls_Vol)

Call the function passing the drive you want the volume name for. For example C:\

 

Mapping a Network Drive

Posted In: . By popo

This tip show how to use the Window API for mapping a network drive from PowerBuilder.
Function Declaration:

FUNCTION ulong WNetUseConnectionA (ulong hwndOwner, &
REF s_netresource lpNetResource, string lpPassword,
string lpUsername, ulong dwFlags, REF string lpAccessName, &
REF ulong lpBufferSize, REF ulong lpResult) library "mpr.dll"

Structure Definition:
$PBExportHeader$s_netresource.srs
global type s_netresource from structure
unsignedlong dwScope
unsignedlong dwType
unsignedlong dwDisplayType
unsignedlong dwUsage
string lpLocalName
string lpRemoteName
string lpComment
string lpProvider
end type

Mapping Code:
CONSTANT ulong NO_ERROR = 0
CONSTANT ulong CONNECT_REDIRECT = 128
CONSTANT ulong RESOURCETYPE_DISK = 1

s_netresource lstr_netresource

String ls_null
String ls_buffer
String ls_MappedDrive

uLong ll_bufferlen
uLong ll_null
uLong ll_ErrInfo
uLong ll_success

SetNull(ll_null)
SetNull(ls_null)

ls_buffer = Space(32)
ll_bufferlen = Len(ls_buffer)

lstr_netresource.dwType = RESOURCETYPE_DISK
lstr_netresource.lpLocalName = ls_null
lstr_netresource.lpRemoteName = "UNC resource name here"
lstr_netresource.lpProvider = ls_null

ll_ErrInfo = WNetUseConnectionA(ll_null, lstr_netresource, &
'password', 'username', &
CONNECT_REDIRECT, ls_buffer, ll_bufferlen, ll_success)

IF ll_ErrInfo = NO_ERROR THEN
MessageBox("Drive Mapped", "Drive Letter is " + ls_buffer)
Return 1
ELSE
MessageBox("Mapping Falied", "Error is " + String(ll_ErrInfo))
Return -1
END IF

 

Getting Active Directory

Posted In: . By popo

With this simple trick you can get the current active directory. Declare the Win32 API modul on Declare -> Local External Functions

Function boolean GetCurrentDirectoryA (long nBufferLength, ref string lpBuffer) Library "kernel32.dll"

Write this code:
string ls_temp
ulong lul_value
boolean lb_rc

lul_value = 255
ls_temp = space(255)
lb_rc = GetCurrentDirectoryA (lul_value, ls_temp)
If lb_rc Then MessageBox('Current Directory', ls_temp, information!)

 

Getting Computer Name

Posted In: . By popo

You can get the computer name from within the application. Declare the Win32 API modul on Declare -> Local External Functions

Function boolean GetComputerNameA (ref string lpBuffer, ref ulong nSize) Library "kernel32.dll"

Now write this code:
string ls_temp
ulong lul_value
boolean lb_rc

lul_value = 255
ls_temp = space(255)
lb_rc = GetComputerNameA (ls_temp, lul_value)
messageBox ('Computer Name', ls_temp, information!)

 

Sending Key Press

Posted In: , . By popo

With this tips, you can make a control likely pressing (a) key(s). First declare the Win32 API modul on Declare -> Local External Functions

Subroutine keybd_event( int bVk, int bScan, int dwFlags, int dwExtraInfo) Library "user32.dll"

Next is an example to simulate pressing "A" key
integer li_vkey
li_vkey = 65 // Character A
sle_1.setfocus() // the desired control to view
keybd_event( li_vkey, 1, 0, 0)

Another example to simulate "Backspace" key
integer li_vkey
integer li_pos
li_pos = len(sle_1.Text) + 1
sle_1.selectText(li_pos, 0)// Cursor position on last text
li_vkey = asc ("~b") // backspace
keybd_event( li_vkey, 1, 0, 0)