How To Write a DWORD to the Registry
This article was previously published under Q258262
This article has been archived. It is offered "as is" and will no longer be updated.
Sometimes it is necessary to write information to the registry. This article shows how to write REG_DWORD values to a key created in the registry for this article. If the value you are writing does not already exist, you need to add it.
IMPORTANT: This article contains information about modifying the registry. Before you modify the registry, make sure to back it up and make sure that you understand how to restore the registry if a problem occurs. For information about how to back up, restore, and edit the registry, click the following article number to view the article in the Microsoft Knowledge Base:
256986 Description of the Microsoft Windows Registry
WARNING: If you use Registry Editor incorrectly, you may cause serious problems that may require you to reinstall your operating system. Microsoft cannot guarantee that you can solve problems that result from using Registry Editor incorrectly. Use Registry Editor at your own risk.
The information about the registry editor in this article is for Microsoft Windows NT and Microsoft Windows 2000. If you are running Microsoft Windows 9X, you need to use Regedit.exe instead of Regedt32.exe.
NOTE: This article uses a registry key that is created just for testing purposes and should be deleted when you are done testing.
- Start Registry Editor (Regedt32.exe).
- Locate the following key in the registry:
- On the Edit menu, click Add Key, and then add the following registry Key:
- On the Edit menu, click Add Value, and then add the following registry value:
Value Name: MyDWORD Data Type: REG_DWORD Value: 0
- Quit the Registry Editor.
- From Visual FoxPro, create a program and enter the following code:
#DEFINE HKEY_CLASSES_ROOT -2147483648#DEFINE HKEY_CURRENT_USER -2147483647#DEFINE HKEY_LOCAL_MACHINE -2147483646#DEFINE HKEY_USERS -2147483645LOCAL nKey, cSubKey, cValue, nValueToWrite, lSuccessnKey = HKEY_LOCAL_MACHINEnValueToWrite = 1cSubKey = "Software\VfpREGTest"cValue = "MyDWORD"lSuccess = WriteRegDWORD(nKey, cSubKey, cValue, nValueToWrite)IF (lSuccess) THEN =MESSAGEBOX("Function Successful.")ELSE =MESSAGEBOX("Function Not Successful.")ENDIFFUNCTION WriteRegDWORD * This function writes a REG_DWORD to the registry. It will return .T. * if successful and .F. if it isn't successful. PARAMETERS nKey, cSubKey, cValue, nValueToWrite * nKey The root key to open. It can be any of the constants defined below. *#DEFINE HKEY_CLASSES_ROOT -2147483648 *#DEFINE HKEY_CURRENT_USER -2147483647 *#DEFINE HKEY_LOCAL_MACHINE -2147483646 *#DEFINE HKEY_USERS -2147483645 *cSubKey The SubKey to open. *cValue The value that is going to be written. *nValueToWrite The value to write to the registry * Constants that are needed for Registry functions #DEFINE REG_DWORD 4 * WIN 32 API functions that are used DECLARE Integer RegOpenKey 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 RegCloseKey IN Win32API Integer nHKey * Local variables used LOCAL nErrCode && Error Code returned from Registry functions LOCAL nKeyHandle && Handle to Key that is opened in the Registry LOCAL lpdwValueType && Type of Value that we are looking for. LOCAL lpbValue && The data stored in the value LOCAL lpcbValueSize && Size of the variable LOCAL lpdwReserved && Reserved Must be 0 * Initialize the variables nKeyHandle = 0 lpdwReserved = 0 lpdwValueType = REG_DWORD lpcbValueSize = 4 && DWORD is 4 bytes lpbValue = LongToStr(nValueToWrite) nErrCode = RegOpenKey(nKey, cSubKey, @nKeyHandle) * If the error code isn't 0, then the key doesn't exist or can't be opened. IF (nErrCode # 0) THEN RETURN .F. ENDIF nErrCode=RegSetValueEx(nKeyHandle, cValue, lpdwReserved, lpdwValueType, lpbValue, lpcbValueSize) =RegCloseKey(nKeyHandle) IF (nErrCode # 0) THEN RETURN .F. ENDIFRETURN .T.FUNCTION LongToStr* This function converts a long to a string PARAMETERS nLongVal LOCAL nLoopVar, strReturn strReturn = "" FOR nLoopVar = 24 TO 0 STEP -8 strReturn = CHR(INT(nLongVal/(2^nLoopVar))) + strReturn nLongVal = MOD(nLongVal, (2^nLoopVar)) NEXTRETURN strReturn* End of Code
- Run the code created in step 1. A message box appears announcing the success or failure of the function. You can use regedt32.exe to verify that the value was written to the registry.
- You can replace nKey, cSubKey, cValue, and nValueToWrite with your information to be written to the registry.
For additional information obtaining values from the registry, click the article number below to view the article in the Microsoft Knowledge Base:
244675 How ToUse Windows Script Host to Read, Write, Delete Registry
Article ID: 258262 - Last Review: 12/05/2015 19:21:07 - Revision: 2.2
Microsoft Visual FoxPro 3.0 Standard Edition, Microsoft Visual FoxPro 3.0b Standard Edition, Microsoft Visual FoxPro 5.0 Standard Edition, Microsoft Visual FoxPro 5.0a, Microsoft Visual FoxPro 6.0 Professional Edition
- kbnosurvey kbarchive kbapi kbcodesnippet kbhowto kbregistry KB258262