The Registry is a database in Windows that stores system hardware information, application information, and user information. It provides the best place for applications to save parameter settings. In Windows operating system, when we run the “regedit.exe file, we can see from the open registry editing window that the Registry consists of two parts: the first layer of each item on the left is called the primary key of the Registry, double-click the keys extended by each primary key latency. the right side of the window is the key value of each subkey, each sub-Key has its own default value and an empty string with the default value ).
To read and write the registry data in a VFP Application, you must use the WIN32API (Windows 32-bit application interface) function to call the WIN32API function just like other VFP functions, the DECLARE command must be used to register and DECLARE each called function. The format is DECLARE [cFunctionType] FunctionName IN LibraryName [AS AliasName].
[CParamType1 [@] ParamName1, cParamType2 [@] ParamName2,...]
The meaning of each parameter is: ① cFunctionType is the type of the return value of the function, but one of SHORT, INTEGER, LONG, SINGLE, DOUBLE, and STRING. If the function does not return a value, cFunctionType is omitted; ② FunctionName indicates the name of the WIN32API function to be called. Pay attention to the case. Otherwise, the VFP will be in. DLL library LibraryName). If the function name is the same AS that of the VFP function, you can use the AS clause to obtain a valid VFP function name. ③ LibraryName specifies the external Windows. DLL name. For example, if WIN32API is used, VFP is stored in KERNEL32.DLL, GDI32.DLL, USER32.DLL, and flat. search for FunctionName in DLL and ADVAPI32.DLL; ④ cParamType specifies the type of each parameter, which can be INTEGER, LONG, SINGLE, DOUBLE, or STRING, in VFP Function parameters can be passed in two ways: pass by value and reference by name. The former only transmits the value of variable or constant to the function, therefore, the value of the variable cannot be modified within the function. The latter transmits the address of the variable that cannot be used as a constant to the function. Therefore, the value of the variable can be modified within the function, it is applicable to situations where the value of the variable needs to be modified within the function or the return value needs to be placed in the variable. In VFP, if "@" is added before the variable name, it indicates passing parameters by reference. If "@" is not added, it indicates passing parameters by value. The parameters in this example are passed by reference or by value. You can see from the DECLARE command and function call statement.
The following example adds the value of TimesForUse under the HKEY_CURRENT_USER \ SOFTWARE \ LYTAPP sub-key in the registry to 1 until 12 times of running:
* Each primary key corresponds to an INTEGER: # DEFINE HKEY_CLASSES_ROOT bitset (2147483648) &- # DEFINE HKEY_CURRENT_USER bitset (0, 31) + 1 &-2147483647 # DEFINE HKEY_LOCAL_MACHINE bitset (2147483646) + 2 &- # DEFINE HKEY_USER bitset (2147483645) + 3 &- # DEFINE HKEY_CURRENT_CONFIG bitset (2147483643) + 5 &- # DEFINE HKEY_DYN_DATA bitset (2147483642) + 6 &- * Key value data type: 1-string, 3-binary, 4-integer # DEFINE REG_SZ 1 # DEFINE REG_BINARY 3 # DEFINE REG_DWORD 4 * If the following API functions return 0, the operation is successful. DECLARE Integer RegOpenKey IN Win32API; Integer nHKey, String @ cSubKey, Integer @ nResult DECLARE Integer RegCreateKey IN Win32API; Integer nHKey, String @ cSubKey, Integer @ nResult DECLARE Integer RegSetValueEx IN Win32API; Integer hKey, String lpszValueName, Integer dwReserved ,; Integer fdwType, String lpbData, Integer cbData DECLARE Integer RegQueryValueEx IN Win32API; Integer nHKey, String lpszValueName, Integer dwReserved ,; Integer @ lpdwType, string @ lpbData, Integer @ lpcbData * DECLARE Integer RegDeleteKey IN Win32API; * Integer nHKey, String @ cSubKey * DECLARE Integer RegDeleteValue IN Win32API; * Integer nHKey, String cSubKey * DECLARE Integer RegCloseKey IN Win32API; * Integer nHKey Csubkey = 'Software \ lytapp' Nresult = 0 If regopenkey (HKEY_CURRENT_USER, @ csubkey, @ nresult) #0 RegCreateKey (HKEY_CURRENT_USER, csubkey, @ nresult) Endif * A long integer pointing to the subkey "HKEY_CURRENT_USER \ Software \ lytapp" is stored in the nresult. Lpdwtype = 0 Lpbdata = space (256) Lpcbdata = len (lpbdata) If RegQueryValueEx (nresult, 'timesforuse', 0, @ lpdwtype, @ lpbdata, @ lpcbdata) = 0 Do case Case lpdwtype = REG_SZ Lpbdata = left (lpbdata, lpcbdata-1) If val (lpbdata) <12 Lpbdata = alltrim (str (val (lpbdata) + 1 )) Messagebox ('The program can only be used 12 times before it is registered! '+ CHR (13) +' you are now the nth '+; Lpbdata +. ') Lpbdata = lpbdata + chr (0) Cbdata = len (lpbdata) RegSetValueEx (nresult, 'timesforuse', 0, REG_SZ, lpbdata, cbdata) Else Messagebox ("the program has expired! "+ CHR (13) +" contact the author for registration. ") Endif Case lpdwtype = REG_BINARY Messagebox ('the key value is binary data. ') Case lpdwtype = REG_DWORD Messagebox ('the key value is a long integer, please use NN = 0' + CHR (13) + 'for I = 1 to 4' + CHR (13) +; 'Nn = NN + VAL (SUBSTR (lpbdata, I, 1) * 16 ^ (2 * I-2) '+ CHR (13) +; 'Endfor reads this value. ') Otherwise Messagebox ('unknown data! ') Endcase Else Lpbdata = '1' Messagebox ('The program can only be used 12 times before it is registered! '+ CHR (13) +' you are using this program now '+ lpbdata +. ') Lpbdata = lpbdata + chr (0) RegSetValueEx (nresult, 'timesforuse', 0, REG_SZ, lpbdata, 2) Endif |
* Warning: It is recommended that you do not modify the content when you are not familiar with the Registry. Otherwise, the system may crash. If you want to modify the registry, we recommend that you back up the registry before modification.