以下是一段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
VB如何访问远程服务器?
答案:2 悬赏:0 手机版
解决时间 2021-03-03 11:54
- 提问者网友:心牵心
- 2021-03-02 17:00
最佳答案
- 五星知识达人网友:猎心人
- 2021-03-02 17:13
使用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
全部回答
- 1楼网友:往事隔山水
- 2021-03-02 17:38
通过oledb连接数据源在连接字符串中不用关心ip,把下面的password,username,dsnname换成你实际的就可以
dim conora as new adodb.connection
dim strconn as string
strconn = "provider=msdasql.1;password=yourpassword;persist security info=true;user id=username;data source=dsnname"
conora.open strconn
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯