WAIT WINDOW registry(1,3,"SOFTWARE\Microsoft\visualfoxpro",,"abc","19821207",1)
***FOR VFP ***以下读写注册表的自定义函数:
*------------------------------------------------
*操作注册表
*语法:
*创建子键:registry(0,主键序号,子键名,新建子键名)
*写注册表:registry(1,主键序号,子键名,,注册项,项目值)
*读注册表:registry(2,主键序号,子键名,,注册项)
*删除子键:registry(3,主键序号,子键名)
FUNCTION REGISTRY
PARAMETERS NREG_CODE,;&&操作代码,0-创建子键,1-写注册表,2-读注册表,3-删除注册表项
NREG_MAINKEYNO,; &&注册表主键序号
CREG_SKEY,; &&子键
CREG_SKEY_NEW,; &&需创建的子键
CREG_SKEYNAME,; &&注册表项
CREG_KEYVALUE,; &&注册表项的值
NREG_VALUETYPE &&注册表项的值的类型,1-数据串,2-Unicode 串,3-二进制,4-32-位数值
ON ERROR RETURN .F.
LOCAL NDISPOSITION,;
LNRESULT,; &&要取得值的注册表子项的地址
NRESULT,;
RETURNVALUE,; &&返回的注册表值
NKEYLENGTH,; &&要写的值的长度
BUFFER,; &&返回注册表项值
BUFFERSIZE,; &&返回值的长度
LNERROR,; &&返回注册表函数的错误代码
NERROR,; &&返回的错误代码
SKEY1,SKEY2
IF !BETWEEN(NREG_MAINKEYNO,1,4)
RETURN .F.
ENDIF
IF NREG_CODE=1 AND !BETWEEN(NREG_VALUETYPE,1,4)
RETURN .F.
ENDIF
NREG_MAINKEYNO=NREG_MAINKEYNO-2147483649
*注册表键值访问权限
#DEFINE KEY_ALL_ACCESS 983103 &&所有权限
DECLARE INTEGER RegOpenKeyEx IN Win32API ;
INTEGER nKey,;
STRING @cSubKey, ;
INTEGER nReserved,;
INTEGER nAccessMask,;
INTEGER @nResult
DECLARE INTEGER RegQueryValueEx IN Win32API;
INTEGER nKey, ;
STRING cValueName, ;
INTEGER nReserved,;
INTEGER @nType, ;
STRING @cBuffer, ;
INTEGER @nBufferSize
DECLARE RegCreateKeyEx IN WIN32API;
INTEGER hKey,; &&一个打开项的句柄,或者一个标准项名
STRING LPSUBKEY,; &&欲创建的新子项的名字
INTEGER RESERVED,; &&设为零
STRING LPCLASS,; &&项的类名
INTEGER DWOPTIONS,; &&零,这个项不正式保存下来,系统重新启动后会消失
INTEGER SAMDESIRED,; &&访问权限
INTEGER LPSECURITYATTRIBUTES,;&&对这个项的安全特性进行描述的一个结构,零
INTEGER @PHKRESULT,; &&指定用于装载新子项句柄的一个变量
INTEGER @LPDWDISPOSITION &&返回是新建子项,还是打开子项(如果新建的子项存在)
DECLARE RegDeleteKey IN WIN32API;
INTEGER hKey,;
STRING lpSubKey
DECLARE INTEGER RegCloseKey IN Win32API INTEGER nKey
DECLARE INTEGER RegSetValueEx IN Win32API;
INTEGER hKey,;
STRING lpValueName,;
INTEGER RESERVED,;
INTEGER dwType,;
STRING lpData,;
INTEGER cbData
DO CASE
CASE NREG_CODE=0 &&创建子建
NRESULT= 0
NDISPOSITION = 0
NERROR = REGOPENKEYEX(NREG_MAINKEYNO,CREG_SKEY,0, KEY_ALL_ACCESS, @NRESULT)
IF NERROR#0
MESSAGEBOX('Can`t open the sub_key!',0 48,'INF')
RETURN .F.
ENDIF
NERROR=REGCREATEKEYEX(NRESULT,CREG_SKEY_NEW,0,'nReg_ValueType',0,KEY_ALL_ACCESS,0,@LNRESULT,@NDISPOSITION)
=REGCLOSEKEY(NRESULT)
RETURN NERROR
***********************************
CASE NREG_CODE=1 &&写注册表
NERROR=0
NERROR =REGOPENKEYEX(NREG_MAINKEYNO,CREG_SKEY, 0,KEY_ALL_ACCESS, @LNRESULT)
IF NERROR#0
MESSAGEBOX('Can`t write the sub_key!',0 48,'INF')
RETURN .F.
ENDIF
NKEYLENGTH=LEN(CREG_KEYVALUE)
NERROR=REGSETVALUEEX(LNRESULT,CREG_SKEYNAME,0,NREG_VALUETYPE, @CREG_KEYVALUE, @NKEYLENGTH)
=REGCLOSEKEY(LNRESULT)
RETURN NERROR=0
***********************************
CASE NREG_CODE=2 &&读注册表
RETURNVALUE = ""
LNRESULT = 0
BUFFER = SPACE(128)
BUFFERSIZE = 128
LNERROR = REGOPENKEYEX(NREG_MAINKEYNO,CREG_SKEY, 0, KEY_ALL_ACCESS, @LNRESULT)
IF LNERROR = 0
LNTYPE = 0
LNERROR = REGQUERYVALUEEX(LNRESULT, CREG_SKEYNAME, 0, @LNTYPE, @BUFFER, @BUFFERSIZE)
IF LNERROR = 0 AND BUFFER <> CHR(0) &&如果错误码为 0, 则正常读取值.
RETURNVALUE = LEFT(BUFFER, BUFFERSIZE - 1) &&取得返回值
ENDIF
ENDIF
=REGCLOSEKEY(LNRESULT)
RETURN RETURNVALUE
***********************************
CASE NREG_CODE=3 &&删除注册表项
SKEY1=LEFT(CREG_SKEY,AT('\',CREG_SKEY,OCCURS('\',CREG_SKEY))-1)
SKEY2=SUBST(CREG_SKEY,AT('\',CREG_SKEY,OCCURS('\',CREG_SKEY)) 1)
NERROR =REGOPENKEYEX(NREG_MAINKEYNO,SKEY1, 0,KEY_ALL_ACCESS, @LNRESULT)
IF NERROR#0
MESSAGEBOX('Can`t delete the sub_key!',0 48,'INF')
RETURN .F.
ELSE
NERROR=REGDELETEKEY(LNRESULT,SKEY2)
ENDIF
ENDCASE
ENDFUNC
读写注册表的自定义函数
最新推荐文章于 2024-10-31 14:44:51 发布