在PB程序中实现ODBC数据源的添加
原著:鹿寒
思路:主要是利用PB对注册表操作函数RegistryGet()和RegistrySet(),这两个函数以及其他注册表函数在PB的帮助里都有详细说明。
再就是利用PB的ProfileString()函数读取INI文件中预先定义好的参数。
写注册表的源程序:
string ls_sys[],ls_driver[],st_driver,st_path
integer li_rtn
ls_me="新星科技股份有限公司(New Star, Inc.)"
ls_version="2.0"
ls_sys[1]="HKEY_LOCAL_MACHINE\Software\ODBC\ODBCINST.INI\Sybase SQL Anywhere 5.0"
ls_sys[2]="HKEY_CURRENT_USER\Software\"+ls_me+"\仓储管理系统\"+ls_version
ls_sys[3]="HKEY_CURRENT_USER\Software\ODBC\ODBC.INI\ODBC Data Sources"
ls_sys[4]="HKEY_CURRENT_USER\Software\ODBC\ODBC.INI"
li_rtn = RegistryGet(ls_sys[1], "Driver", RegString!, st_driver)
IF li_rtn = -1 THEN
messagebox("错误","未安装Sybase SQL Anywhere 5.0 !",information!,ok!)
RETURN
END IF
li_rtn = RegistryGet(ls_sys[2], "InstPath", RegString!, st_path)
IF li_rtn = -1 THEN
messagebox("错误","读取安装路径出错 !",information!,ok!)
RETURN
END IF
string envfile,ls_source[],ls_drivepath,ls_temp
integer i
envfile="regedit.ini"
ls_source[1]="cl"
ls_source[2] =ProfileString(envfile,"INI","AutoStop"," ")
ls_source[3] =ProfileString(envfile,"INI","DatabaseFile"," ")
ls_source[4] =ProfileString(envfile,"INI","DatabaseName"," ")
ls_source[5] =ProfileString(envfile,"INI","Description"," ")
ls_source[6] =ProfileString(envfile,"INI","Driver"," ")
ls_source[7] =ProfileString(envfile,"INI","PWD"," ")
ls_source[8] =ProfileString(envfile,"INI","Start"," ")
ls_source[9] =ProfileString(envfile,"INI","UID"," ")
li_rtn=RegistrySet(ls_sys[3], ls_source[1] , RegString!, "Sybase SQL Anywhere 5.0")
IF li_rtn = -1 THEN
messagebox("错误","读取系统文件或写注册表出错 !",information!,ok!)
RETURN
END IF
ls_sys[4]=ls_sys[4] + "\" + ls_source[1]
li_rtn=RegistrySet(ls_sys[4], "AutoStop" , RegString!, ls_source[2])
IF li_rtn = -1 THEN
messagebox("错误","读取系统文件或写注册表出错 !",information!,ok!)
RETURN
END IF
li_rtn=RegistrySet(ls_sys[4], "DatabaseFile" , RegString!, st_path+"\"+ls_source[3])
IF li_rtn = -1 THEN
messagebox("错误","读取系统文件或写注册表出错 !",information!,ok!)
RETURN
END IF
li_rtn=RegistrySet(ls_sys[4], "DatabaseName" , RegString!, ls_source[4])
IF li_rtn = -1 THEN
messagebox("错误","读取系统文件或写注册表出错 !",information!,ok!)
RETURN
END IF
li_rtn=RegistrySet(ls_sys[4], "Description" , RegString!, ls_source[5])
IF li_rtn = -1 THEN
messagebox("错误","读取系统文件或写注册表出错?!",information!,ok!)
RETURN
END IF
li_rtn=RegistrySet(ls_sys[4], "Driver" , RegString!, st_driver)
IF li_rtn = -1 THEN
messagebox("错误","读取系统文件或写注册表出错 !",information!,ok!)
RETURN
END IF
li_rtn=RegistrySet(ls_sys[4], "PWD" , RegString!, ls_source[7])
IF li_rtn = -1 THEN
messagebox("错误","读取系统文件或写注册表出错 !",information!,ok!)
RETURN
END IF
ls_temp=st_driver
i=pos(ls_temp,"\")
do while i>0
ls_drivepath=ls_drivepath+left(ls_temp,i)
ls_temp=right(ls_temp,len(ls_temp)-i)
i=pos(ls_temp,"\")
loop
li_rtn=RegistrySet(ls_sys[4], "Start" , RegString!, ls_drivepath+ls_source[8])
IF li_rtn = -1 THEN
messagebox("错误","读取系统文件或写注册表出错 !",information!,ok!)
RETURN
END IF
li_rtn=RegistrySet(ls_sys[4], "UID" , RegString!, ls_source[9])
IF li_rtn = -1 THEN
messagebox("错误","读取系统文件或写注册表出错 !",information!,ok!)
RETURN
END IF
regedit.ini文件格式如下:
[INI]
AutoStop=yes
DatabaseFile=Psdemodb.db
DatabaseName=仓储管理系统
Description=cl
Driver=
PWD=sql
Start=dbeng50.exe
UID=dba
备注:程序中ls_sys[2]变量代表的注册表键值是我通过安装程序制作工具自动生成的,每个人使用的制作安装程序的工具不同,这个键值也许不尽相同。其实用意只是在于取得st_path这个变量,它保存应用程序安装后所在路径,其实也可以用API函数得到,至于使用API函数怎么取得当前程序所在路径就不讲了。