【函数】
RegSetValue【操作系统】
Win9X:Yes
WinNT:Yes【声明】
RegSetValue Lib "advapi32.dll" Alias "RegSetValueA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long【说明】  设置指定项或子项的默认值 【返回值】  Long,零(ERROR_SUCCESS)表示成功。其他任何值都代表一个错误代码 【其它】
【参数表】
  hKey -----------  Long,一个已打开项的句柄,或指定一个标准项名  lpSubKey -------  String,欲对它的值进行设置的一个子项的名字。如指定vbNullString,表示设置hKey的默认值。如指定的子项不存在,则会创建它  dwType ---------  Long,必须是REG_SZ  lpData ---------  String,新值  cbData ---------  Long,指定lpData的长度,不包括空中止字符

解决方案 »

  1.   

    一个API:写注册表值
    具体可以查下MSDN
      

  2.   

    用于写注册表子项一下使RegSetValue的例子'This program needs 3 buttons
    Const REG_SZ = 1 ' Unicode nul terminated string
    Const REG_BINARY = 3 ' Free form binary
    Const HKEY_CURRENT_USER = &H80000001
    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
    Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
    Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
    Function RegQueryStringValue(ByVal hKey As Long, ByVal strValueName As String) As String
        Dim lResult As Long, lValueType As Long, strBuf As String, lDataBufSize As Long
        'retrieve nformation about the key
        lResult = RegQueryValueEx(hKey, strValueName, 0, lValueType, ByVal 0, lDataBufSize)
        If lResult = 0 Then
            If lValueType = REG_SZ Then
                'Create a buffer
                strBuf = String(lDataBufSize, Chr$(0))
                'retrieve the key's content
                lResult = RegQueryValueEx(hKey, strValueName, 0, 0, ByVal strBuf, lDataBufSize)
                If lResult = 0 Then
                    'Remove the unnecessary chr$(0)'s
                    RegQueryStringValue = Left$(strBuf, InStr(1, strBuf, Chr$(0)) - 1)
                End If
            ElseIf lValueType = REG_BINARY Then
                Dim strData As Integer
                'retrieve the key's value
                lResult = RegQueryValueEx(hKey, strValueName, 0, 0, strData, lDataBufSize)
                If lResult = 0 Then
                    RegQueryStringValue = strData
                End If
            End If
        End If
    End Function
    Function GetString(hKey As Long, strPath As String, strValue As String)
        Dim Ret
        'Open the key
        RegOpenKey hKey, strPath, Ret
        'Get the key's content
        GetString = RegQueryStringValue(Ret, strValue)
        'Close the key
        RegCloseKey Ret
    End Function
    Sub SaveString(hKey As Long, strPath As String, strValue As String, strData As String)
        Dim Ret
        'Create a new key
        RegCreateKey hKey, strPath, Ret
        'Save a string to the key
        RegSetValueEx Ret, strValue, 0, REG_SZ, ByVal strData, Len(strData)
        'close the key
        RegCloseKey Ret
    End Sub
    Sub SaveStringLong(hKey As Long, strPath As String, strValue As String, strData As String)
        Dim Ret
        'Create a new key
        RegCreateKey hKey, strPath, Ret
        'Set the key's value
        RegSetValueEx Ret, strValue, 0, REG_BINARY, CByte(strData), 4
        'close the key
        RegCloseKey Ret
    End Sub
    Sub DelSetting(hKey As Long, strPath As String, strValue As String)
        Dim Ret
        'Create a new key
        RegCreateKey hKey, strPath, Ret
        'Delete the key's value
        RegDeleteValue Ret, strValue
        'close the key
        RegCloseKey Ret
    End Sub
    Private Sub Command1_Click()
        Dim strString As String
        'Ask for a value
        strString = InputBox("Please enter a value between 0 and 255 to be saved as a binary value in the registry.", App.Title)
        If strString = "" Or Val(strString) > 255 Or Val(strString) < 0 Then
            MsgBox "Invalid value entered ...", vbExclamation + vbOKOnly, App.Title
            Exit Sub
        End If
        'Save the value to the registry
        SaveStringLong HKEY_CURRENT_USER, "KPD-Team", "BinaryValue", CByte(strString)
    End Sub
    Private Sub Command2_Click()
        'Get a string from the registry
        Ret = GetString(HKEY_CURRENT_USER, "KPD-Team", "BinaryValue")
        If Ret = "" Then MsgBox "No value found !", vbExclamation + vbOKOnly, App.Title: Exit Sub
        MsgBox "The value is " + Ret, vbOKOnly + vbInformation, App.Title
    End Sub
    Private Sub Command3_Click()
        'Delete the setting from the registry
        DelSetting HKEY_CURRENT_USER, "KPD-Team", "BinaryValue"
        MsgBox "The value was deleted ...", vbInformation + vbOKOnly, App.Title
    End Sub
    Private Sub Form_Load()
        'KPD-Team 1998
        'URL: http://www.allapi.net/
        'E-Mail: [email protected]
        Command1.Caption = "Set Value"
        Command2.Caption = "Get Value"
        Command3.Caption = "Delete Value"
    End Sub
      

  3.   

    有人说 RegSetValue是:
    RegSetValue
    The RegSetValue function sets the data for the default or unnamed value of a specified registry key. The data must be a text string. This function is provided for compatibility with Windows version 3.1. Win32-based applications should use the RegSetValueEx function, which allows you to set any number of named values of any data type. LONG RegSetValue(
      HKEY hKey,         // handle to key to set value for
      LPCTSTR lpSubKey,  // address of subkey name
      DWORD dwType,      // type of value
      LPCTSTR lpData,    // address of value data
      DWORD cbData       // size of value data
    );
     
    Parameters
    hKey 
    Handle to a currently open key or any of the following predefined reserved handle values: 
    HKEY_CLASSES_ROOT
    HKEY_CURRENT_CONFIG
    HKEY_CURRENT_USER
    HKEY_LOCAL_MACHINE
    HKEY_USERS
    Windows NT: HKEY_PERFORMANCE_DATA 
    Windows 95 and Windows 98: HKEY_DYN_DATA lpSubKey 
    Pointer to a null-terminated string containing the name of a subkey of the hKey parameter. The function sets the default value of the specified subkey. If this parameter is NULL or points to an empty string, the function sets the default value of the key identified by hKey. 
    dwType 
    Specifies the type of information to be stored. This parameter must be the REG_SZ type. To store other data types, use the RegSetValueEx function. 
    lpData 
    Pointer to a null-terminated string containing the data to set for the default value of the specified key. 
    cbData 
    Specifies the length, in bytes, of the string pointed to by the lpData parameter, not including the terminating null character. 
    Return Values
    If the function succeeds, the return value is ERROR_SUCCESS.If the function fails, the return value is a nonzero error code defined in WINERROR.H. You can use the FormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM flag to get a generic description of the error.Res
    If the key specified by the lpSubKey parameter does not exist, the RegSetValue function creates it. Value lengths are limited by available memory. Long values (more than 2048 bytes) should be stored as files with the filenames stored in the registry. This helps the registry perform efficiently. The key identified by the hKey parameter must have been opened with KEY_SET_VALUE access. To open the key, use the RegCreateKeyEx or RegOpenKeyEx function. If the ANSI version of this function is used (either by explicitly calling RegSetValueA or by not defining UNICODE before including the WINDOWS.H file), the lpData parameter must be an ANSI character string. The string is converted to Unicode before it is stored in the registry. QuickInfo
      Windows NT: Requires version 3.1 or later.
      Windows: Requires Windows 95 or later.
      Windows CE: Unsupported.
      Header: Declared in winreg.h.
      Import Library: Use advapi32.lib.
      Unicode: Implemented as Unicode and ANSI versions on Windows NT.See Also
    Registry Overview, Registry Functions, RegCreateKeyEx, RegFlushKey, RegOpenKeyEx, RegQueryValue, RegQueryValueEx, RegSetValueEx