造成不能得到(Food! Done)的原因是: API函数在传一个字符串到VB时,这个字符串是以NULL结尾的, 比如,你在取到第一个字符串Good!时,它实际上是Good! 。(注意,它后面的空格,因为NULL字符是不能显示的。)所以就算后面还有什么字符也不能显示出来了。 =============== 以上纯属个人意见,如果有误导,。。嘿嘿,概不负责!下面是我的一段代码,你可以参考一下:Option Explicit 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 RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult 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 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, lpData As Any, ByVal cbData 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 lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As String) As LongConst HKEY_LOCAL_MACHINE = &H80000002 Const MY_SUBKEY = "SOFTWARE\科筑打印监控_客户端" Private Const REG_NONE = 0 Private Const REG_SZ = 1 Private Const ERROR_SUCCESS = 0& Dim status As LongPrivate Sub Command1_Click() SetKeyValue HKEY_LOCAL_MACHINE, MY_SUBKEY, "host", Trim(Text1.Text) End SubPublic Function GetKeyValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String) As String Dim llKeyID As Long, llBufferSize As Long, lsKeyValue As String
GetKeyValue = Empty status = ERROR_SUCCESS
status = RegOpenKey(plKey, psKey, llKeyID) If status = ERROR_SUCCESS Then status = RegQueryValueEx(llKeyID, psSubKey, 0&, REG_SZ, 0&, llBufferSize) If llBufferSize < 2 Then status = RegCloseKey(llKeyID) GetKeyValue = "NoValue" Else lsKeyValue = String(llBufferSize + 1, " ") status = RegQueryValueEx(llKeyID, psSubKey, 0&, REG_SZ, ByVal lsKeyValue, llBufferSize) If status = ERROR_SUCCESS Then GetKeyValue = Left$(lsKeyValue, llBufferSize - 1) End If status = RegCloseKey(llKeyID) End If Else GetKeyValue = "NoSubKey" End If End FunctionPublic Function CreateKey(ByVal plKey As Long, ByVal psKey As String) As Long Dim llKeyID As Long
status = ERROR_SUCCESS status = RegCreateKey(plKey, psKey, llKeyID)
If status = ERROR_SUCCESS Then CreateKey = llKeyID End If End FunctionPublic Sub SetKeyValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String, ByVal psKeyValue As String) Dim llKeyID As Long
status = ERROR_SUCCESS
status = RegOpenKey(plKey, psKey, llKeyID) If status = ERROR_SUCCESS Then If LenB(psKeyValue) = 0 Then status = RegSetValueEx(llKeyID, psSubKey, 0&, REG_SZ, 0&, 0&) Else status = RegSetValueEx(llKeyID, psSubKey, 0&, REG_SZ, ByVal psKeyValue, lstrlen(psKeyValue) + 1) End If status = RegCloseKey(llKeyID) End If End Sub Private Sub Form_Load() Text1.Text = GetIPAddress() If Text1.Text = "127.0.0.1" Then frmGetIP.Caption = "You are of Line" Else frmGetIP.Caption = "You are on Line" End If End Sub Private Sub Command2_Click() Dim s As String
s = GetKeyValue(HKEY_LOCAL_MACHINE, MY_SUBKEY, "host") If Right(s, 1) = vbNullChar Then s = Left(s, Len(s) - 1) Text2.Text = s & "|" End Sub
Private Sub Form_Load() Text1.Text = GetIPAddress() If Text1.Text = "127.0.0.1" Then frmGetIP.Caption = "You are of Line" Else frmGetIP.Caption = "You are on Line" End If End Sub上面一段去掉!
我试了一下,没有问题啊
先按command1 再按command2,command2按下后,text1:"software",text2:"Good! Done" 我看老兄你还是给分吧
API函数在传一个字符串到VB时,这个字符串是以NULL结尾的,
比如,你在取到第一个字符串Good!时,它实际上是Good! 。(注意,它后面的空格,因为NULL字符是不能显示的。)所以就算后面还有什么字符也不能显示出来了。
===============
以上纯属个人意见,如果有误导,。。嘿嘿,概不负责!下面是我的一段代码,你可以参考一下:Option Explicit
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 RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult 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 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, lpData As Any, ByVal cbData 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 lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As String) As LongConst HKEY_LOCAL_MACHINE = &H80000002
Const MY_SUBKEY = "SOFTWARE\科筑打印监控_客户端"
Private Const REG_NONE = 0
Private Const REG_SZ = 1
Private Const ERROR_SUCCESS = 0&
Dim status As LongPrivate Sub Command1_Click()
SetKeyValue HKEY_LOCAL_MACHINE, MY_SUBKEY, "host", Trim(Text1.Text)
End SubPublic Function GetKeyValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String) As String
Dim llKeyID As Long, llBufferSize As Long, lsKeyValue As String
GetKeyValue = Empty
status = ERROR_SUCCESS
status = RegOpenKey(plKey, psKey, llKeyID)
If status = ERROR_SUCCESS Then
status = RegQueryValueEx(llKeyID, psSubKey, 0&, REG_SZ, 0&, llBufferSize)
If llBufferSize < 2 Then
status = RegCloseKey(llKeyID)
GetKeyValue = "NoValue"
Else
lsKeyValue = String(llBufferSize + 1, " ")
status = RegQueryValueEx(llKeyID, psSubKey, 0&, REG_SZ, ByVal lsKeyValue, llBufferSize)
If status = ERROR_SUCCESS Then
GetKeyValue = Left$(lsKeyValue, llBufferSize - 1)
End If
status = RegCloseKey(llKeyID)
End If
Else
GetKeyValue = "NoSubKey"
End If
End FunctionPublic Function CreateKey(ByVal plKey As Long, ByVal psKey As String) As Long
Dim llKeyID As Long
status = ERROR_SUCCESS
status = RegCreateKey(plKey, psKey, llKeyID)
If status = ERROR_SUCCESS Then
CreateKey = llKeyID
End If
End FunctionPublic Sub SetKeyValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String, ByVal psKeyValue As String)
Dim llKeyID As Long
status = ERROR_SUCCESS
status = RegOpenKey(plKey, psKey, llKeyID)
If status = ERROR_SUCCESS Then
If LenB(psKeyValue) = 0 Then
status = RegSetValueEx(llKeyID, psSubKey, 0&, REG_SZ, 0&, 0&)
Else
status = RegSetValueEx(llKeyID, psSubKey, 0&, REG_SZ, ByVal psKeyValue, lstrlen(psKeyValue) + 1)
End If
status = RegCloseKey(llKeyID)
End If
End Sub
Private Sub Form_Load()
Text1.Text = GetIPAddress()
If Text1.Text = "127.0.0.1" Then
frmGetIP.Caption = "You are of Line"
Else
frmGetIP.Caption = "You are on Line"
End If
End Sub
Private Sub Command2_Click()
Dim s As String
s = GetKeyValue(HKEY_LOCAL_MACHINE, MY_SUBKEY, "host")
If Right(s, 1) = vbNullChar Then s = Left(s, Len(s) - 1)
Text2.Text = s & "|"
End Sub
Text1.Text = GetIPAddress()
If Text1.Text = "127.0.0.1" Then
frmGetIP.Caption = "You are of Line"
Else
frmGetIP.Caption = "You are on Line"
End If
End Sub上面一段去掉!