************************************************************* DEFINE CLASS wwAPI AS Custom ************************************************************* *** Author: Rick Strahl *** (c) West Wind Technologies, 1997 *** Contact: (541) 386-2087 / rstrahl@west-wind.com *** Function: Encapsulates several Windows API functions ************************************************************* *** Custom Properties nLastError=0 cErrorMsg = "" FUNCTION Init ************************************************************************ * wwAPI :: Init ********************************* *** Function: DECLARES commonly used DECLAREs so they're not redefined *** on each call to the methods. ************************************************************************ *** Open Registry Key DECLARE INTEGER RegOpenKey ; IN Win32API ; INTEGER nHKey,; STRING cSubKey,; INTEGER @nHandle *** Create a new Key DECLARE Integer RegCreateKey ; IN Win32API ; INTEGER nHKey,; STRING cSubKey,; INTEGER @nHandle *** Close an open Key DECLARE Integer RegCloseKey ; IN Win32API ; INTEGER nHKey ENDFUNC * Init FUNCTION ReadRegistryString ************************************************************************ * wwAPI :: ReadRegistryString ********************************* *** Function: Reads a string value from the registry. *** Pass: tnHKEY - HKEY value (in CGIServ.h) *** tcSubkey - The Registry subkey value *** tcEntry - The actual Key to retrieve *** tlInteger - Optional - Return an DWORD value *** tnMaxStringSize - optional - Max size for a string (512) *** Return: Registry String or .NULL. on not found ************************************************************************ LPARAMETERS tnHKey, tcSubkey, tcEntry, tlInteger,tnMaxStringSize LOCAL lnRegHandle, lnResult, lnSize, lcDataBuffer, tnType IF EMPTY(tnMaxStringSize) tnMaxStringSize= MAX_INI_BUFFERSIZE ENDIF IF EMPTY(tnHKEY) tnHKEY = HKEY_LOCAL_MACHINE ENDIF IF VARTYPE(tnHKey) = "C" DO CASE CASE tnHKey = "HKLM" tnHKey = HKEY_LOCAL_MACHINE CASE tnHkey = "HKCU" tnHKey = HKEY_CURRENT_USER CASE tnHkey = "HKCR" tnHKey = HKEY_CLASSES_ROOT OTHERWISE tnHKey = 0 ENDCASE ENDIF lnRegHandle=0 *** Open the registry key lnResult=RegOpenKey(tnHKey,tcSubKey,@lnRegHandle) IF lnResult#ERROR_SUCCESS *** Not Found RETURN .NULL. ENDIF *** Return buffer to receive value IF !tlInteger *** Need to define here specifically for Return Type *** for lpdData parameter or VFP will choke. *** Here it's STRING. DECLARE INTEGER RegQueryValueEx ; IN Win32API ; INTEGER nHKey,; STRING lpszValueName,; INTEGER dwReserved,; INTEGER @lpdwType,; STRING @lpbData,; INTEGER @lpcbData lcDataBuffer=space(tnMaxStringSize) lnSize=LEN(lcDataBuffer) lnType=REG_DWORD lnResult=RegQueryValueEx(lnRegHandle,tcEntry,0,@lnType,; @lcDataBuffer,@lnSize) ELSE *** Need to define here specifically for Return Type *** for lpdData parameter or VFP will choke. *** Here's it's an INTEGER DECLARE INTEGER RegQueryValueEx ; IN Win32API AS RegQueryInt; INTEGER nHKey,; STRING lpszValueName,; INTEGER dwReserved,; Integer @lpdwType,; INTEGER @lpbData,; INTEGER @lpcbData lcDataBuffer=0 lnSize=4 lnType=REG_DWORD lnResult=RegQueryInt(lnRegHandle,tcEntry,0,@lnType,; @lcDataBuffer,@lnSize) IF lnResult = ERROR_SUCCESS RETURN lcDataBuffer ELSE RETURN -1 ENDIF ENDIF =RegCloseKey(lnRegHandle) IF lnResult#ERROR_SUCCESS *** Not Found RETURN .NULL. ENDIF IF lnSize < 2 RETURN "" ENDIF *** Return string and strip out NULLs RETURN SUBSTR(lcDataBuffer,1,lnSize-1) ENDFUNC * ReadRegistryString ************************************************************************ * Registry :: WriteRegistryString ********************************* *** Function: Writes a string value to the registry. *** If the value doesn't exist it's created. If the key *** doesn't exist it is also created, but this will only *** succeed if it's the last key on the hive. *** Pass: tnHKEY - HKEY value (in WCONNECT.h) *** tcSubkey - The Registry subkey value *** tcEntry - The actual Key to write to *** tcValue - Value to write or .NULL. to delete key *** tlCreate - Create if it doesn't exist *** Assume: Use with extreme caution!!! Blowing your registry can *** hose your system! *** Return: .T. or .NULL. on error ************************************************************************ FUNCTION WriteRegistryString LPARAMETERS tnHKey, tcSubkey, tcEntry, tcValue,tlCreate LOCAL lnRegHandle, lnResult, lnSize, lcDataBuffer, tnType IF EMPTY(tnHKEY) tnHKEY = HKEY_LOCAL_MACHINE ENDIF IF VARTYPE(tnHKey) = "C" DO CASE CASE tnHKey = "HKLM" tnHKey = HKEY_LOCAL_MACHINE CASE tnHkey = "HKCU" tnHKey = HKEY_CURRENT_USER CASE tnHkey = "HKCR" tnHKey = HKEY_CLASSES_ROOT OTHERWISE tnHKey = 0 ENDCASE ENDIF lnRegHandle=0 lnResult=RegOpenKey(tnHKey,tcSubKey,@lnRegHandle) IF lnResult#ERROR_SUCCESS IF !tlCreate RETURN .F. ELSE lnResult=RegCreateKey(tnHKey,tcSubKey,@lnRegHandle) IF lnResult#ERROR_SUCCESS RETURN .F. ENDIF ENDIF ENDIF *** Need to define here specifically for Return Type! *** Here lpbData is STRING. *** Check for .NULL. which means delete key IF !ISNULL(tcValue) IF VARTYPE(tcValue) = "N" DECLARE INTEGER RegSetValueEx ; IN Win32API ; INTEGER nHKey,; STRING lpszEntry,; INTEGER dwReserved,; INTEGER fdwType,; INTEGER@ lpbData,; INTEGER cbData lnResult=RegSetValueEx(lnRegHandle,tcEntry,0,REG_DWORD,; @tcValue,4) ELSE DECLARE INTEGER RegSetValueEx ; IN Win32API ; INTEGER nHKey,; STRING lpszEntry,; INTEGER dwReserved,; INTEGER fdwType,; STRING lpbData,; INTEGER cbData *** Nope - write new value lnSize=LEN(tcValue) if lnSize=0 tcValue=CHR(0) ENDIF lnResult=RegSetValueEx(lnRegHandle,tcEntry,0,REG_SZ,; tcValue,lnSize) ENDIF ELSE *** Delete a value from a key DECLARE INTEGER RegDeleteValue ; IN Win32API ; INTEGER nHKEY,; STRING cEntry *** DELETE THE KEY lnResult=RegDeleteValue(lnRegHandle,tcEntry) ENDIF =RegCloseKey(lnRegHandle) IF lnResult#ERROR_SUCCESS RETURN .F. ENDIF RETURN .T. ENDPROC * WriteRegistryString FUNCTION EnumKey ************************************************************************ * wwAPI :: EnumRegistryKey ********************************* *** Function: Returns a registry key name based on an index *** Allows enumeration of keys in a FOR loop. If key *** is empty end of list is reached. *** Pass: tnHKey - HKEY_ root key *** tcSubkey - Subkey string *** tnIndex - Index of key name to get (0 based) *** Return: "" on error - Key name otherwise ************************************************************************ LPARAMETERS tnHKey, tcSubKey, tnIndex LOCAL lcSubKey, lcReturn, lnResult, lcDataBuffer lnRegHandle=0 *** Open the registry key lnResult=RegOpenKey(tnHKey,tcSubKey,@lnRegHandle) IF lnResult#ERROR_SUCCESS *** Not Found RETURN .NULL. ENDIF DECLARE Integer RegEnumKey ; IN WIN32API ; INTEGER nHKey, ; INTEGER nIndex, ; STRING @cSubkey, ; INTEGER nSize lcDataBuffer=SPACE(MAX_INI_BUFFERSIZE) lnSize=MAX_INI_BUFFERSIZE lnResult=RegENumKey(lnRegHandle, tnIndex, @lcDataBuffer, lnSize) =RegCloseKey(lnRegHandle) IF lnResult#ERROR_SUCCESS *** Not Found RETURN .NULL. ENDIF RETURN TRIM(CHRTRAN(lcDataBuffer,CHR(0),"")) ENDFUNC * EnumRegistryKey ENDDEFINE <pre> You also need a few constants: <pre> *** Registry roots #DEFINE HKEY_CLASSES_ROOT -2147483648 && (( HKEY ) 0x80000000 ) #DEFINE HKEY_CURRENT_USER -2147483647 && (( HKEY ) 0x80000001 ) #DEFINE HKEY_LOCAL_MACHINE -2147483646 && (( HKEY ) 0x80000002 ) #DEFINE HKEY_USERS -2147483645 && (( HKEY ) 0x80000003 ) *** Registry Value types #DEFINE REG_NONE 0 && Undefined Type (default) #DEFINE REG_SZ 1 && Regular Null Terminated String #DEFINE REG_BINARY 3 && ??? (unimplemented) #DEFINE REG_DWORD 4 && Long Integer value #DEFINE MULTI_SZ 7 && Multiple Null Term Strings (not implemented)Hope this helps,