{"id":1026,"date":"2024-07-04T07:56:07","date_gmt":"2024-07-03T23:56:07","guid":{"rendered":"http:\/\/www.wyrcad.com\/?p=1026"},"modified":"2024-07-17T10:25:48","modified_gmt":"2024-07-17T02:25:48","slug":"%e6%b3%a8%e5%86%8c%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/www.wyrcad.com\/?p=1026","title":{"rendered":"\u6ce8\u518c\u8868(Registry)\u51fd\u6570\u7cfb\u5217\u4e4b\u4e00"},"content":{"rendered":"\n<p>Option Explicit<br>&#8216;\u6ce8\u518c\u8868\u64cd\u4f5c<br>Public Declare Function RegCloseKey Lib &#8220;advapi32.dll&#8221; (ByVal hkey As Long) As Long<br>Private Declare Function RegCreateKey Lib &#8220;advapi32.dll&#8221; Alias &#8220;RegCreateKeyA&#8221; (ByVal hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long<br>Public Declare Function RegOpenKey Lib &#8220;advapi32.dll&#8221; Alias &#8220;RegOpenKeyA&#8221; (ByVal hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long<br>Public Declare Function RegQueryValueEx Lib &#8220;advapi32.dll&#8221; Alias &#8220;RegQueryValueExA&#8221; (ByVal hkey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long<br>Public Declare Function RegSetValueEx Lib &#8220;advapi32.dll&#8221; Alias &#8220;RegSetValueExA&#8221; (ByVal hkey&amp;, ByVal lpValueName$, ByVal Reserved&amp;, ByVal dwType&amp;, ByVal lpData$, ByVal cbData&amp;) As Long<br>Private Declare Function RegSetValue Lib &#8220;advapi32.dll&#8221; Alias &#8220;RegSetValueA&#8221; (ByVal hkey As Long, ByVal lpSubKey As String, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long<br>Private Declare Function RegDeleteValue Lib &#8220;advapi32.dll&#8221; Alias &#8220;RegDeleteValueA&#8221; (ByVal hkey As Long, ByVal lpValueName As String) As Long<br>Public Declare Function RegOpenKeyEx Lib &#8220;advapi32&#8221; Alias &#8220;RegOpenKeyExA&#8221; (ByVal hkey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, ByRef phkResult As Long) As Long<br>Public Declare Function RegCreateKeyEx Lib &#8220;advapi32.dll&#8221; Alias &#8220;RegCreateKeyExA&#8221; (ByVal hkey&amp;, ByVal lpSubKey$, ByVal Reserved&amp;, ByVal lpClass$, ByVal dwOptions&amp;, ByVal samDesired&amp;, ByVal SecAtts&amp;, phkResult&amp;, lpdwDisp&amp;) As Long<br>Private Declare Function RegDeleteKey Lib &#8220;advapi32.dll&#8221; Alias &#8220;RegDeleteKeyA&#8221; (ByVal hkey As Long, ByVal lpSubKey As String) As Long<br>Public Declare Function SHDeleteKey Lib &#8220;shlwapi.dll&#8221; Alias &#8220;SHDeleteKeyA&#8221; (ByVal hkey As Long, ByVal pszSubKey As String) As Long<\/p>\n\n\n\n<p>Public Const HKEY_CLASSES_ROOT = &amp;H80000000<br>Public Const HKEY_CURRENT_USER = &amp;H80000001<br>Public Const HKEY_LOCAL_MACHINE = &amp;H80000002<br>Public Const HKEY_USERS = &amp;H80000003<br>Public Const HKEY_PERFORMANCE_DATA = &amp;H80000004<br>Public Const HKEY_CURRENT_CONFIG = &amp;H80000005<br>Public Const HKEY_DYN_DATA = &amp;H80000006<br>Public Const KEY_ALL_ACCESS = (&amp;H1F0000 Or &amp;H1 Or &amp;H2 Or &amp;H4 Or &amp;H8 Or &amp;H10 Or &amp;H20) And (Not &amp;H100000)<br>Public Const REG_NONE = (0)<br>Public Const REG_SZ = (1) &#8216;REG_SZ \u2014\u2014\u952e\u503c\u5b58\u50a8\u4e3a\u5b57\u7b26\u4e32\u503c<br>Public Const REG_EXPAND_SZ = (2)<br>Public Const REG_BINARY = (3) &#8216;REG_BINARY \u2014\u2014\u952e\u503c\u5b58\u50a8\u4e3a\u4e8c\u8fdb\u5236\u503c<br>Public Const REG_DWORD = (4) &#8216;REG_DWORD \u2014\u2014\u952e\u503c\u5b58\u50a8\u4e3aDWORD\u503c\uff08\u53cc\u5b57\uff09<br>Public Const REG_DWORD_BIG_ENDIAN = (5)<br>Public Const REG_LINK = (6)<br>Public Const REG_MULTI_SZ = (7)<br>Public Const REG_RESOURCE_LIST = (8)<br>Public Const REG_FULL_RESOURCE_DESCRIPTOR = (9)<br>Public Const REG_RESOURCE_REQUIREMENTS_LIST = (10)<\/p>\n\n\n\n<p>Function RegQueryStringValue(ByVal hkey As Long, ByVal sValueName As String) As String<br>On Error Resume Next<br>Dim lResult As Long, lValueType As Long, sBuf As String, lDataBufSize As Long<br>&#8216;retrieve nformation about the key<br>lResult = RegQueryValueEx(hkey, sValueName, 0, lValueType, ByVal 0, lDataBufSize)<br>If lResult = 0 Then<br>If lValueType = REG_SZ Then<br>&#8216;Create a buffer<br>sBuf = String(lDataBufSize, Chr$(0))<br>&#8216;retrieve the key&#8217;s content<br>lResult = RegQueryValueEx(hkey, sValueName, 0, 0, ByVal sBuf, lDataBufSize)<br>If lResult = 0 Then<br>&#8216;Remove the unnecessary chr$(0)&#8217;s<br>&#8216;RegQueryStringValue = Left$(sBuf, InStr(1, sBuf, Chr$(0)) \u2013 1)<br>&#8216;Avoid making mistakes when chr$(0) is zero<br>RegQueryStringValue = <a href=\"https:\/\/www.wyrcad.com\/?p=785\">GetAPIString<\/a>(sBuf)<br>End If<br>ElseIf lValueType = REG_BINARY Then<br>Dim sData As Integer<br>&#8216;retrieve the key&#8217;s value<br>lResult = RegQueryValueEx(hkey, sValueName, 0, 0, sData, lDataBufSize)<br>If lResult = 0 Then<br>RegQueryStringValue = sData<br>End If<br>End If<br>End If<br>On Error GoTo 0<br>End Function<\/p>\n\n\n\n<p>Function GetString(ByVal hkey As Long, sPath As String, ByVal sValue As String, ByVal sApp As Variant, ByVal sIniFile As Variant) &#8216;\u4ece\u6ce8\u518c\u8868\u8bfb\u53d6\u6570\u636e, \u5982\u679c\u5931\u8d25\u5219\u4ece\u521d\u59cb\u5316\u6587\u4ef6\u8bfb\u53d6<br>On Error Resume Next<br>Dim lResult1, lResult2<br>&#8216;Open the key<br>lResult1 = RegOpenKey(hkey, sPath, lRet)<br>&#8216;Get the key&#8217;s content<br>lResult2 = RegQueryStringValue(lRet, sValue)<br>GetString = lResult2<br>&#8216;Close the key<br>RegCloseKey lRet<br>On Error GoTo 0<br>&#8216;OS is diversity, determine if registry success<br>If IsMissing(sApp) Then Exit Function<br>If IsMissing(sIniFile) Then Exit Function<br>If lResult1 = 0 And Len(lResult2) > 0 And Not <a href=\"https:\/\/www.wyrcad.com\/?cat=12\">ChineseExist<\/a>(lResult2) Then Exit Function &#8216;\u542b\u6c49\u5b57\u5b57\u7b26\u4e32\u65f6\u4e0d\u4e00\u5b9a\u5bf9<br>On Error Resume Next<br>&#8216;Read ini file, when operating Registry is fail.<br>GetString = <a href=\"https:\/\/www.wyrcad.com\/?p=1031\">ReadIniFile<\/a>(sApp, sValue, sIniFile)<br>On Error GoTo 0<br>End Function<\/p>\n\n\n\n<p>Sub SaveString(ByVal hkey As Long, ByVal sPath As String, ByVal sValue As String, ByVal sData As String, ByVal sApp As Variant, ByVal sIniFile As Variant) &#8216;\u5411\u6ce8\u518c\u8868\u5199\u5165\u6570\u636e, \u5982\u679c\u5931\u8d25\u5219\u5411\u521d\u59cb\u5316\u6587\u4ef6\u5199\u5165<br>On Error Resume Next<br>Dim lResult1, lResult2<br>&#8216;Create a new key<br>lResult1 = RegCreateKey(hkey, sPath, lRet)<br>&#8216;Save a string to the key<br>lResult2 = RegSetValueEx(lRet, sValue, 0, REG_SZ, ByVal sData, Len(sData))<br>&#8216;close the key<br>RegCloseKey lRet<br>On Error GoTo 0<br>&#8216;Debug.Print lResult1<br>&#8216;Debug.Print lResult2<br>&#8216;OS is diversity, determine if registry success<br>If IsMissing(sApp) Then Exit Function<br>If IsMissing(sIniFile) Then Exit Function<br>If lResult1 = 0 And lResult2 = 0 And Not <a href=\"https:\/\/www.wyrcad.com\/?cat=12\">ChineseExist<\/a>(sData) Then Exit Sub &#8216;\u542b\u6c49\u5b57\u5b57\u7b26\u4e32\u65f6\u7ed3\u679c\u6709\u95ee\u9898<br>On Error Resume Next<br>&#8216;Write ini file, when operating Registry is fail.<br><a href=\"https:\/\/www.wyrcad.com\/?p=1031\">WriteIniFile<\/a> sApp, sValue, sData, sIniFile<br>&#8216;<a href=\"https:\/\/www.wyrcad.com\/?p=1031\">WritePrivateProfileString<\/a> sApp, sValue, sData, sIniFile<br>On Error GoTo 0<br>End Sub<\/p>\n\n\n\n<p>Sub DelSetting(ByVal hkey As Long, ByVal sPath As String, ByVal sValue As String, ByVal sApp As Variant, ByVal sIniFile As Variant) &#8216;\u5220\u9664\u6ce8\u518c\u8868\u6570\u636e, \u5982\u679c\u5931\u8d25\u5219\u5220\u9664\u521d\u59cb\u5316\u6587\u4ef6\u6570\u636e<br>On Error Resume Next<br>&#8216;Debug.Print hKey<br>&#8216;Debug.Print sPath<br>&#8216;Debug.Print sValue<br>Dim lResult1, lResult2<br>&#8216;Create a new key<br>lResult1 = RegCreateKey(hkey, sPath, lRet)<br>&#8216;Delete the key&#8217;s value<br>lResult2 = RegDeleteValue(lRet, sValue)<br>&#8216;close the key<br>RegCloseKey lRet<br>On Error GoTo 0<br>&#8216;Debug.Print lResult1<br>&#8216;Debug.Print lResult2<br>&#8216;OS is diversity, determine if registry success<br>If IsMissing(sApp) Then Exit Function<br>If IsMissing(sIniFile) Then Exit Function<br>If lResult1 = 0 And hkey = HKEY_LOCAL_MACHINE Then Exit Sub &#8216;lResult2 = 2 or 5 \u542b\u6c49\u5b57\u5b57\u7b26\u4e32\u65f6\u987b\u4e24\u5904\u5220\u9664<br>On Error Resume Next<br>&#8216;Write ini file, when operating Registry is fail.<br><a href=\"https:\/\/www.wyrcad.com\/?p=1031\">WriteIniFile<\/a> sApp, sValue, vbNullString, sIniFile<br>&#8216;<a href=\"https:\/\/www.wyrcad.com\/?p=1031\">WritePrivateProfileString<\/a> sApp, sValue, vbNullString, sIniFile<br>On Error GoTo 0<br>End Sub<\/p>\n\n\n\n<p>Public Sub DelKey(hkey As Long, sKey As String)<br>&#8216;Delete the key<br>lRet = RegDeleteKey(hkey, sKey)<br>End Sub<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Option Explicit&#8216;\u6ce8\u518c\u8868\u64cd\u4f5cPublic Declare Function RegC &hellip; <a href=\"https:\/\/www.wyrcad.com\/?p=1026\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u6ce8\u518c\u8868(Registry)\u51fd\u6570\u7cfb\u5217\u4e4b\u4e00\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[24,17],"class_list":["post-1026","post","type-post","status-publish","format-standard","hentry","category-bianchengyuandi","tag-vb","tag-biancheng"],"_links":{"self":[{"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/posts\/1026","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1026"}],"version-history":[{"count":13,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/posts\/1026\/revisions"}],"predecessor-version":[{"id":1311,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/posts\/1026\/revisions\/1311"}],"wp:attachment":[{"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}