心血来潮,突发奇想,今天动手操练 PowerBuilder 12.6 , 一通CSDN搜索过后,开始创建项目。
1. New 一个新的文件夹作工作区,这和其它语言类似。
2. 用application模板建项目。
3. 创建个 windows, 放上一些封装好的控件,变成了下面的样子。
网上精品入门贴写得都不错,就不细述过程了。接下来,准备用它调用 power basic写的dll,具体如何在 power basic下写dll前面的笔记写过,这里从略。下面是 power basic 的几个函数, mki, mkl, mks, mkd 和 VERSION, AUTHOR
FUNCTION myMKI ALIAS "myMKI" (BYVAL Param1 AS INTEGER) EXPORT AS WSTRING
DIM I AS INTEGER
DIM TString AS STRING
I=0: TString=""
' code goes here
FOR I = 2 TO 1 STEP -1
TString=TString+RIGHT$(("0"+LTRIM$(HEX$(ASC(MID$(MKI$(Param1),I,1))))),2)
NEXT I
FUNCTION = TString
END FUNCTION
FUNCTION myMKL ALIAS "myMKL" (BYVAL Param1 AS LONG) EXPORT AS WSTRING
DIM I AS INTEGER
DIM TString AS STRING
I=0: TString=""
' code goes here
FOR I = 4 TO 1 STEP -1
TString=TString+RIGHT$(("0"+LTRIM$(HEX$(ASC(MID$(MKL$(Param1),I,1))))),2)
NEXT I
FUNCTION = TString
END FUNCTION
FUNCTION myMKS ALIAS "myMKS" (BYVAL Param1 AS SINGLE) EXPORT AS WSTRING
DIM I AS INTEGER
DIM TString AS STRING
I=0: TString=""
' code goes here
FOR I = 4 TO 1 STEP -1
TString=TString+RIGHT$(("0"+LTRIM$(HEX$(ASC(MID$(MKS$(Param1),I,1))))),2)
NEXT I
FUNCTION = TString
END FUNCTION
FUNCTION myMKD ALIAS "myMKD" (BYVAL Param2 AS DOUBLE) EXPORT AS WSTRING
DIM I AS INTEGER
DIM TString AS STRING
I=0: TString=""
' code goes here
FOR I = 8 TO 1 STEP -1
TString=TString+RIGHT$(("0"+LTRIM$(HEX$(ASC(MID$(MKD$(Param2),I,1))))),2)
NEXT I
FUNCTION = TString
END FUNCTION
FUNCTION myINSTRU ALIAS "myINSTRU" (BYVAL Param1 AS BYTE PTR, BYVAL UB AS INTEGER) EXPORT AS WSTRING
DIM LParam1 AS STRING
DIM RETURNSTR AS WSTRING
DIM J AS BYTE PTR
DIM I AS INTEGER, inByte AS BYTE
RETURNSTR = "UNKNOWN"
LParam1 = ""
J = Param1
FOR I = 0 TO UB - 1
LParam1 = LParam1 + CHR$(@J)
INCR J
NEXT I
LParam1 = TRIM$(LParam1)
SELECT CASE LParam1
CASE "VERSION"
RETURNSTR = "VERSION 1.00 9AUG2023"
CASE "AUTHOR"
RETURNSTR = "Mongnewer"
END SELECT
FUNCTION = RETURNSTR
END FUNCTION
要调用函数,需先分析参数格式。
1. power builder 默认是 unicode 字符,在power basic中相对应的是 WSTRING
2. power builder 其它数字类型可直接传递, 比如 integer, long, double
3. power builder 的real 与 powerbasic 的 single 对应
power builder调用外部函数也可以加上 alias ,强行定义为 ansi 字符串。向power basic传递字节数组,比字符串要方便,因此,送出的参数采用数据和字节数组,返回时用WSTRING
inData[]按ref传送,以byte为单元,UB_inData是动态数组的上边界值。在Command1中调用它们,Command1中的代码如下。
//sle_1.text = string(1123.321)
sle_2.text = myMKI (integer(sle_1.text))
sle_3.text = myMKL (integer(sle_1.text))
sle_4.text = myMKS (real(sle_1.text))
sle_5.text = myMKD (double(sle_1.text))
byte CCMD[]; Blob BB; integer UB_CCMD
BB = Blob("VERSION", EncodingANSI!)
CCMD = GetByteArray(BB); UB_CCMD = UpperBound(CCMD)
sle_6.text = myINSTRU(CCMD, UB_CCMD)
BB = Blob("AUTHOR", EncodingANSI!)
CCMD = GetByteArray(BB); UB_CCMD = UpperBound(CCMD)
sle_7.text = myINSTRU(CCMD, UB_CCMD)
每次传递时都带有UpperBound值,在 powerbasic 那边对应的是:
FUNCTION myINSTRU ALIAS "myINSTRU" (BYVAL Param1 AS BYTE PTR, BYVAL UB AS INTEGER) EXPORT AS WSTRING
首参数是 byte ptr 指针,接下来是整数的数组边界值,export输出的是WSTRING
如果指令是相对固定的,比如不超过20个byte,那发送时指令后补空格,power basic 这边UB就按20计算也合适,因为用的是export输出,并不直接回写数据,因此不用担心由于bug闹崩溃问题。
第一节就习练到此,还算顺利吧。power builder有许多好东西待发掘,那个 blob 挺好,啥都能装进去,用时转换出来。上图功能已经齐全,包含 MKI, CVI, MKL, CVL, MKS, CVS, MKD, CVD, CRC16计算。
PowerBASIC DLL源程序和PowerBuilder示练程序链接如下:
https://download.csdn.net/download/weixin_45707491/88265102