PowerBuilder FAQ

Main Event   History   FAQ   Tutorial  Links   OpenSource   Group   About me

在WebForms上获得客户端信息

1 在webforms target导入.net assembly

 

 

 

 

 

2 代码如下

 

 

 

 

 

 

 

 

 

3 部署web target ,最终效果如下

 

 

 

 

 

 

 

 

 

 

 

 

 

 

C/C++ Datatype Conversion

PowerBuilder Datatype           C/C++ Datatype
             Blob                                  PBBlob
             Boolean                             int 
             Character                          char
             Date                                  PBDate
             DateTime                          PBDateTime
             Decimal                             PBDecimal 
             Double                              double 
             Integer                              int
             Real                                  float
             String                               PBString 
             Time                                 PBTime
             UnSignedInteger               unsigned int

C/C++ Datatype                    PowerBuilder Datatype
             BOOL                                Boolean 
             WORD                               UnSignedInteger
             DWORD                            UnSignedLong
             HANDLE                           UnSignedLong
             HWND                              UnSignedLong
             LPSTR                               String Ref
             LPBYTE                             String Ref
             LPINT                               Long Ref
             char                                 Blob{1} 
             int                                    Integer
             unsigned int                    UnSignedInt
             long                                 Long
             ULONG/unsigned long    UnSignedLong
             double                             Double
             char *                               String Ref
 

取得用户的登陆ID

//Declare an external function as:
Function boolean GetUserNameA( ref string userID, ref ulong len ) library "ADVAPI32.DLL"

In Powerscript
string  login_name
string  ls_temp
ulong   lul_value
boolean lb_rc

lul_value = 255
ls_temp = Space( 255 )
lb_rc = GetUserNameA( ls_temp, lul_value )
login_name = Trim( ls_temp

映射一个网络驱动器

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

在EXE文件中注册OCX组件

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

Powerbuilder中的内存操作大搜集

我们知道pb中不支持指针,但我们在使用WIN32 API和调用一些dll中的外部函数时候,经常会与其打些交道,所以这里将相关的一些技巧收集整理起来。

1、根据字符串地址得到字符串

  完全通过pb自带的函数String就可以实现,函数的语法为String ( data, { format } ),当我们将变量地址作为Data参数,字符串“Address”作为format参数,函数的返回值就是我们需要的字符串。这是种未公开(呵呵,pb的帮助中找不到),但被广泛使用的方法。

  例:string ls_tmp
  ls_tmp =string(hStrData,"Address")

2、得到pb中某个字符串变量的地址

  这次,单纯依靠pb自身是行不通了,需要请来Win Api函数帮忙了:

主人公:Function long lstrcpy(ref string Destination, ref string Source) library "kernel32.dll"

原型:

The lstrcpy function copies a string to a buffer.
LPTSTR lstrcpy(
    LPTSTR lpString1, // address of buffer
    LPCTSTR lpString2  // address of string to copy
   );
Return Values:If the function succeeds, the return value is a pointer to the buffer.


看我怎么大显身手:

定义实例变量:String is_dst

  string ls_src
  long ll_address
  ls_src= "test me"
  ls_dst =space(255)  
  ll_address=lstrcpy(ls_dst,ls_src)

麻烦是麻烦点,不过终于知道你藏身在ll_address那里了。

如何获得一个系统环境变量

Retrieve an environment variable

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

Common XP environment variables:


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.

获取已安装的打印机

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

在PB应用中访问url

Inet linet_base
GetContextService("Internet", linet_base)
linet_base.HyperlinkToURL("http://...")
If IsValid(linet_base) Then Destroy linet_base

获取系统时间格式

RegistryGet("HKEY_CURRENT_USER\Control Panel\International","sShortDate", ls_shortdate
messageBox ("ShortDate", ls_shortdate)
 

获取一个图片的尺寸

// Width and height of a picture inside a BMP file
int job
blob b
ulong width, height

job = FileOpen( "filename.bmp", StreamMode!, Read! )
FileRead( job, b )
width  = Long( Integer( BlobMid( b, 19, 2 ) ), &
   Integer( BlobMid( b, 21, 2) ) )
height = Long( Integer( BlobMid( b, 23, 2 ) ), &
   Integer( BlobMid( b, 25, 2) ) )
FileClose( job )