我现在是用VB6开发ActiveX DLL,通过ASP来调用的DLL里的函数
而现在碰到的问题是,想在DLL里操作注册表,没有成功!于是我把代码复制到VB的EXE工程里试了一下,发现成功了.
不同的地方在:DLL里是通过调用来驱动,而EXE工程里是通过按钮的事件来驱动.
操作注册表的代码如下:
'根键常数
Const HKEY_CLASSES_ROOT = -2147483648#
Const HKEY_CURRENT_USER = -2147483647#
Const HKEY_LOCAL_MACHINE = -2147483646#
Const HKEY_USERS = -2147483645#
'键值类型
Const REG_SZ = 1& '字符串值
Const REG_BINARY = 3& '二进制值
Const REG_DWORD = 4& 'DWORD 值
'声明有关API函数
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" _
( _
ByVal hKey As Long, _
ByVal lpSubKey As String, _
ByRef phkResult As Long _
) As Long '建立一个新的主键
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" _
( _
ByVal hKey As Long, _
ByVal lpSubKey As String, _
ByRef phkResult As Long _
) As Long '打开一个主键
Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" _
( _
ByVal hKey As Long, _
ByVal lpSubKey As String _
) As Long '删除一个主键
Private Declare Function RegCloseKey Lib "advapi32.dll" _
( _
ByVal hKey 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, _
ByVal lpData As Any, _
ByVal cbData As Long _
) As Long '创建或改变一个键值,lpData应由缺省的ByRef型改为ByVal型
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _
( _
ByVal hKey As Long, _
ByVal lpValueName As String, _
ByVal lpReserved As Long, _
ByRef lpType As Long, _
ByVal lpData As Any, _
ByRef lpcbData As Long _
) As Long '查询一个键值,lpData应由缺省的ByRef型改为ByVal型
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" _
( _
ByVal hKey As Long, _
ByVal lpValueName As String _
) As Long '删除一个键值
'主过程
Sub Main()
Dim nKeyHandle As Long, nValueType As Long, nLength As Long
Dim sValue As String
sValue = "I am a winner!"
Call RegCreateKey(HKEY_CURRENT_USER, "SuperCRM", nKeyHandle)
Call RegSetValueEx(nKeyHandle, "My Value", 0, REG_SZ, sValue, 255)
sValue = Space(255)
nLength = 255
Call RegQueryValueEx(nKeyHandle, "My Value", 0, nValueType, sValue, nLength)
MsgBox sValue
Call RegDeleteValue(nKeyHandle, "My Value")
Call RegDeleteKey(HKEY_CURRENT_USER, "New Registry Key")
Call RegCloseKey(nKeyHandle)
End Sub
'这里是按钮的事件
Private Sub Command1_Click()
Call Main
End Sub
后来在网上搜索了一下,把代码的第一部分改成:
Public Enum enumReg
HKEY_CLASSES_ROOT = &H80000000
HKEY_CURRENT_CONFIG = &H80000005
HKEY_CURRENT_USER = &H80000001
HKEY_LOCAL_MACHINE = &H80000002
HKEY_USERS = &H80000003
KEY_ALL_ACCESS = &H3F
REG_OPTION_NON_VOLATILE = 0
End Enum
还是不行,请高手门尽快帮忙解决一下.