VB如何访问远程服务器? 50
以下是一段VB代码,可惜只能访问本机,求教如何才能访问远程服务器。'本地电脑strComputer="."'连接到本地电脑SetobjWMIService=GetObje...
以下是一段VB代码,可惜只能访问本机,求教如何才能访问远程服务器。
'本地电脑
strComputer = "."
'连接到本地电脑
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
'总计物理内存
Set colItems = objWMIService.InstancesOf("Win32_LogicalMemoryConfiguration")
For Each colItem In colItems
dwTotalMem = CLng(colItem.TotalPhysicalMemory / 1024)
MyFile.WriteLine("总计物理内存: " )
Next 展开
'本地电脑
strComputer = "."
'连接到本地电脑
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
'总计物理内存
Set colItems = objWMIService.InstancesOf("Win32_LogicalMemoryConfiguration")
For Each colItem In colItems
dwTotalMem = CLng(colItem.TotalPhysicalMemory / 1024)
MyFile.WriteLine("总计物理内存: " )
Next 展开
2个回答
展开全部
使用GetObject建立远程连接有点奔溃,还是WbemScripting.SWbemLocator.ConnectServer来的简单,另外Win32_LogicalMemoryConfiguration类支持XP和2003,不支持Win7的。加QQ:66750739可以给你一个CIM库的查询工具
以下是完整源代码:
Option Explicit
Private objSWbemLocator As SWbemLocator
Private objSWbemServices As SWbemServices
Private objSWbemObjectSet As SWbemObjectSet
Private objSWbemObject As SWbemObject
Private strComputer As String, strNameSpace As String
Private strClass As String
Private strUser As String
Private strPass As String
Private GetText As String
Private Sub Form_Load()
On Error Resume Next
Dim 单根内存大小 As Long
Dim 总内存大小 As Long
SetForm
strComputer = "." '计算机名或IP地址,本机必须为“.”
strNameSpace = "root\cimv2" '指定命名空间为root\cimv2
strUser = "" '指定计算机用户名,当前用户连接请留空
strPass = "" '指定计算机密码,由于XP SP2以后系统策略设置,远程计算机连接时必须需要密码(不允许空密码连接)
strClass = "Win32_MemoryDevice" '指定类为Win32_Service
Set objSWbemServices = Nothing '断开连接
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator") '建立1个WBEM对象的引用指针
If strComputer = "." And strUser = "" Then
Set objSWbemServices = objSWbemLocator.ConnectServer(strComputer, strNameSpace) '连接到本地、命名空间的WMI,返回一个对 SWbemServices 对象的引用
DoEvents
If Err Then
GetText = Err.Description
Err.Clear
GoTo ErrExit
End If
ElseIf strPass = "" Then
Set objSWbemServices = objSWbemLocator.ConnectServer(strComputer, strNameSpace, strUser) '指定用户名连接到指定计算机,基本会返回拒绝访问。
DoEvents
If Err Then
GetText = "连接远程计算机“" & strComputer & "”时出错," & Err.Description
Err.Clear
GoTo ErrExit
End If
Else
Set objSWbemServices = objSWbemLocator.ConnectServer(strComputer, strNameSpace, strUser, strPass) '指定用户名、密码连接到指定计算机
DoEvents
If Err Then
GetText = "连接远程计算机“" & strComputer & "”时出错," & Err.Description
Err.Clear
GoTo ErrExit
End If
End If
Set objSWbemObjectSet = objSWbemServices.ExecQuery("SELECT * FROM " & strClass) '通过WQL查询,返回指定类的所有
'Debug.Print objSWbemObject.GetObjectText_
GetText = "内存插槽数量:" & objSWbemObjectSet.Count & vbCrLf
For Each objSWbemObject In objSWbemObjectSet
GetText = GetText & "------------------------------" & vbCrLf
GetText = GetText & "插槽编号:" & objSWbemObject.DeviceID & vbCrLf
单根内存大小 = objSWbemObject.EndingAddress - objSWbemObject.StartingAddress
If 单根内存大小 < 1 Then
GetText = GetText & "未检查到内存!" & vbCrLf
Else
GetText = GetText & "开始地址:" & objSWbemObject.StartingAddress & vbCrLf
GetText = GetText & "结束地址:" & objSWbemObject.EndingAddress & vbCrLf
GetText = GetText & "内存大小:" & (单根内存大小 + 1) \ 1048576 & "GB(" & 单根内存大小 + 1 & "字节数)" & vbCrLf
总内存大小 = 总内存大小 + 单根内存大小 + 1
End If
Next
GetText = GetText & "==============================" & vbCrLf
GetText = GetText & "总内存大小:" & 总内存大小 \ 1048576 & "GB(" & 总内存大小 & "字节数)" & vbCrLf
ErrExit:
Debug.Print GetText
'Text1.Text = GetText
End Sub
Sub SetForm()
Me.Caption = "内存查询工具 By:克隆狼": Me.BorderStyle = 1
Me.Height = 5940: Me.Width = 3490
'Text1.Height = Me.ScaleHeight - 50: Text1.Width = Me.ScaleWidth - 40: Text1.Top = 20: Text1.Left = 20
End Sub
Option Explicit
Private objSWbemLocator As SWbemLocator
Private objSWbemServices As SWbemServices
Private objSWbemObjectSet As SWbemObjectSet
Private objSWbemObject As SWbemObject
Private strComputer As String, strNameSpace As String
Private strClass As String
Private strUser As String
Private strPass As String
Private GetText As String
Private Sub Form_Load()
On Error Resume Next
Dim 单根内存大小 As Long
Dim 总内存大小 As Long
SetForm
strComputer = "." '计算机名或IP地址,本机必须为“.”
strNameSpace = "root\cimv2" '指定命名空间为root\cimv2
strUser = "" '指定计算机用户名,当前用户连接请留空
strPass = "" '指定计算机密码,由于XP SP2以后系统策略设置,远程计算机连接时必须需要密码(不允许空密码连接)
strClass = "Win32_MemoryDevice" '指定类为Win32_Service
Set objSWbemServices = Nothing '断开连接
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator") '建立1个WBEM对象的引用指针
If strComputer = "." And strUser = "" Then
Set objSWbemServices = objSWbemLocator.ConnectServer(strComputer, strNameSpace) '连接到本地、命名空间的WMI,返回一个对 SWbemServices 对象的引用
DoEvents
If Err Then
GetText = Err.Description
Err.Clear
GoTo ErrExit
End If
ElseIf strPass = "" Then
Set objSWbemServices = objSWbemLocator.ConnectServer(strComputer, strNameSpace, strUser) '指定用户名连接到指定计算机,基本会返回拒绝访问。
DoEvents
If Err Then
GetText = "连接远程计算机“" & strComputer & "”时出错," & Err.Description
Err.Clear
GoTo ErrExit
End If
Else
Set objSWbemServices = objSWbemLocator.ConnectServer(strComputer, strNameSpace, strUser, strPass) '指定用户名、密码连接到指定计算机
DoEvents
If Err Then
GetText = "连接远程计算机“" & strComputer & "”时出错," & Err.Description
Err.Clear
GoTo ErrExit
End If
End If
Set objSWbemObjectSet = objSWbemServices.ExecQuery("SELECT * FROM " & strClass) '通过WQL查询,返回指定类的所有
'Debug.Print objSWbemObject.GetObjectText_
GetText = "内存插槽数量:" & objSWbemObjectSet.Count & vbCrLf
For Each objSWbemObject In objSWbemObjectSet
GetText = GetText & "------------------------------" & vbCrLf
GetText = GetText & "插槽编号:" & objSWbemObject.DeviceID & vbCrLf
单根内存大小 = objSWbemObject.EndingAddress - objSWbemObject.StartingAddress
If 单根内存大小 < 1 Then
GetText = GetText & "未检查到内存!" & vbCrLf
Else
GetText = GetText & "开始地址:" & objSWbemObject.StartingAddress & vbCrLf
GetText = GetText & "结束地址:" & objSWbemObject.EndingAddress & vbCrLf
GetText = GetText & "内存大小:" & (单根内存大小 + 1) \ 1048576 & "GB(" & 单根内存大小 + 1 & "字节数)" & vbCrLf
总内存大小 = 总内存大小 + 单根内存大小 + 1
End If
Next
GetText = GetText & "==============================" & vbCrLf
GetText = GetText & "总内存大小:" & 总内存大小 \ 1048576 & "GB(" & 总内存大小 & "字节数)" & vbCrLf
ErrExit:
Debug.Print GetText
'Text1.Text = GetText
End Sub
Sub SetForm()
Me.Caption = "内存查询工具 By:克隆狼": Me.BorderStyle = 1
Me.Height = 5940: Me.Width = 3490
'Text1.Height = Me.ScaleHeight - 50: Text1.Width = Me.ScaleWidth - 40: Text1.Top = 20: Text1.Left = 20
End Sub
以下是完整源代码:
Option Explicit
Private objSWbemLocator As SWbemLocator
Private objSWbemServices As SWbemServices
Private objSWbemObjectSet As SWbemObjectSet
Private objSWbemObject As SWbemObject
Private strComputer As String, strNameSpace As String
Private strClass As String
Private strUser As String
Private strPass As String
Private GetText As String
Private Sub Form_Load()
On Error Resume Next
Dim 单根内存大小 As Long
Dim 总内存大小 As Long
SetForm
strComputer = "." '计算机名或IP地址,本机必须为“.”
strNameSpace = "root\cimv2" '指定命名空间为root\cimv2
strUser = "" '指定计算机用户名,当前用户连接请留空
strPass = "" '指定计算机密码,由于XP SP2以后系统策略设置,远程计算机连接时必须需要密码(不允许空密码连接)
strClass = "Win32_MemoryDevice" '指定类为Win32_Service
Set objSWbemServices = Nothing '断开连接
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator") '建立1个WBEM对象的引用指针
If strComputer = "." And strUser = "" Then
Set objSWbemServices = objSWbemLocator.ConnectServer(strComputer, strNameSpace) '连接到本地、命名空间的WMI,返回一个对 SWbemServices 对象的引用
DoEvents
If Err Then
GetText = Err.Description
Err.Clear
GoTo ErrExit
End If
ElseIf strPass = "" Then
Set objSWbemServices = objSWbemLocator.ConnectServer(strComputer, strNameSpace, strUser) '指定用户名连接到指定计算机,基本会返回拒绝访问。
DoEvents
If Err Then
GetText = "连接远程计算机“" & strComputer & "”时出错," & Err.Description
Err.Clear
GoTo ErrExit
End If
Else
Set objSWbemServices = objSWbemLocator.ConnectServer(strComputer, strNameSpace, strUser, strPass) '指定用户名、密码连接到指定计算机
DoEvents
If Err Then
GetText = "连接远程计算机“" & strComputer & "”时出错," & Err.Description
Err.Clear
GoTo ErrExit
End If
End If
Set objSWbemObjectSet = objSWbemServices.ExecQuery("SELECT * FROM " & strClass) '通过WQL查询,返回指定类的所有
'Debug.Print objSWbemObject.GetObjectText_
GetText = "内存插槽数量:" & objSWbemObjectSet.Count & vbCrLf
For Each objSWbemObject In objSWbemObjectSet
GetText = GetText & "------------------------------" & vbCrLf
GetText = GetText & "插槽编号:" & objSWbemObject.DeviceID & vbCrLf
单根内存大小 = objSWbemObject.EndingAddress - objSWbemObject.StartingAddress
If 单根内存大小 < 1 Then
GetText = GetText & "未检查到内存!" & vbCrLf
Else
GetText = GetText & "开始地址:" & objSWbemObject.StartingAddress & vbCrLf
GetText = GetText & "结束地址:" & objSWbemObject.EndingAddress & vbCrLf
GetText = GetText & "内存大小:" & (单根内存大小 + 1) \ 1048576 & "GB(" & 单根内存大小 + 1 & "字节数)" & vbCrLf
总内存大小 = 总内存大小 + 单根内存大小 + 1
End If
Next
GetText = GetText & "==============================" & vbCrLf
GetText = GetText & "总内存大小:" & 总内存大小 \ 1048576 & "GB(" & 总内存大小 & "字节数)" & vbCrLf
ErrExit:
Debug.Print GetText
'Text1.Text = GetText
End Sub
Sub SetForm()
Me.Caption = "内存查询工具 By:克隆狼": Me.BorderStyle = 1
Me.Height = 5940: Me.Width = 3490
'Text1.Height = Me.ScaleHeight - 50: Text1.Width = Me.ScaleWidth - 40: Text1.Top = 20: Text1.Left = 20
End Sub
Option Explicit
Private objSWbemLocator As SWbemLocator
Private objSWbemServices As SWbemServices
Private objSWbemObjectSet As SWbemObjectSet
Private objSWbemObject As SWbemObject
Private strComputer As String, strNameSpace As String
Private strClass As String
Private strUser As String
Private strPass As String
Private GetText As String
Private Sub Form_Load()
On Error Resume Next
Dim 单根内存大小 As Long
Dim 总内存大小 As Long
SetForm
strComputer = "." '计算机名或IP地址,本机必须为“.”
strNameSpace = "root\cimv2" '指定命名空间为root\cimv2
strUser = "" '指定计算机用户名,当前用户连接请留空
strPass = "" '指定计算机密码,由于XP SP2以后系统策略设置,远程计算机连接时必须需要密码(不允许空密码连接)
strClass = "Win32_MemoryDevice" '指定类为Win32_Service
Set objSWbemServices = Nothing '断开连接
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator") '建立1个WBEM对象的引用指针
If strComputer = "." And strUser = "" Then
Set objSWbemServices = objSWbemLocator.ConnectServer(strComputer, strNameSpace) '连接到本地、命名空间的WMI,返回一个对 SWbemServices 对象的引用
DoEvents
If Err Then
GetText = Err.Description
Err.Clear
GoTo ErrExit
End If
ElseIf strPass = "" Then
Set objSWbemServices = objSWbemLocator.ConnectServer(strComputer, strNameSpace, strUser) '指定用户名连接到指定计算机,基本会返回拒绝访问。
DoEvents
If Err Then
GetText = "连接远程计算机“" & strComputer & "”时出错," & Err.Description
Err.Clear
GoTo ErrExit
End If
Else
Set objSWbemServices = objSWbemLocator.ConnectServer(strComputer, strNameSpace, strUser, strPass) '指定用户名、密码连接到指定计算机
DoEvents
If Err Then
GetText = "连接远程计算机“" & strComputer & "”时出错," & Err.Description
Err.Clear
GoTo ErrExit
End If
End If
Set objSWbemObjectSet = objSWbemServices.ExecQuery("SELECT * FROM " & strClass) '通过WQL查询,返回指定类的所有
'Debug.Print objSWbemObject.GetObjectText_
GetText = "内存插槽数量:" & objSWbemObjectSet.Count & vbCrLf
For Each objSWbemObject In objSWbemObjectSet
GetText = GetText & "------------------------------" & vbCrLf
GetText = GetText & "插槽编号:" & objSWbemObject.DeviceID & vbCrLf
单根内存大小 = objSWbemObject.EndingAddress - objSWbemObject.StartingAddress
If 单根内存大小 < 1 Then
GetText = GetText & "未检查到内存!" & vbCrLf
Else
GetText = GetText & "开始地址:" & objSWbemObject.StartingAddress & vbCrLf
GetText = GetText & "结束地址:" & objSWbemObject.EndingAddress & vbCrLf
GetText = GetText & "内存大小:" & (单根内存大小 + 1) \ 1048576 & "GB(" & 单根内存大小 + 1 & "字节数)" & vbCrLf
总内存大小 = 总内存大小 + 单根内存大小 + 1
End If
Next
GetText = GetText & "==============================" & vbCrLf
GetText = GetText & "总内存大小:" & 总内存大小 \ 1048576 & "GB(" & 总内存大小 & "字节数)" & vbCrLf
ErrExit:
Debug.Print GetText
'Text1.Text = GetText
End Sub
Sub SetForm()
Me.Caption = "内存查询工具 By:克隆狼": Me.BorderStyle = 1
Me.Height = 5940: Me.Width = 3490
'Text1.Height = Me.ScaleHeight - 50: Text1.Width = Me.ScaleWidth - 40: Text1.Top = 20: Text1.Left = 20
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-22
展开全部
给每个值就可以联接局域网了
cn.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=" & SuserName & ";PassWord=" & SuserMm & ";Initial Catalog=" & SdataName & ";Data Source=" & ServeName & ""
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询