永发信息网

VB如何访问远程服务器?

答案:2  悬赏:0  手机版
解决时间 2021-03-03 11:54
以下是一段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
最佳答案
使用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
全部回答
通过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
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
为什么说物理中的压力是由弹性形变产生的??
蜂蜜咬掉要是用什么样
谭咏麟的魔鬼之女是翻唱Sharon O'Neill
红米手机通话质量差是怎么回事
寻龙诀舒淇说了一句上了床不主动联系你的男人
17k和岳麓那个对新人写手有好处,个人文笔还
重感情是什么意思?
【赔了夫人又折兵】赔了夫人又折兵是什么意思
《楚乔传》汁湘是谁演的 奸杀汁湘的人是谁
钢铁雄心3 基础设施怎么修复
波导是什么意思
青春期发育的特点是DA. 身高和体重突增B. 脑
蝎子会不会咬人
离心机管底加速度为160gn是什么意思?怎样转
瑞安到洞头怎么走啊 要直达的那种
推荐资讯
想知道:临沧市 从临沧机场到凤庆县中医院怎么
就是我们平常取快递的那些小快递店怎么开,我
若风豪宅在哪的最新相关信息
垦丁怎么到阿里山
将一张圆形纸对折三次后展开,不能得到(  
阿富汗猎犬有几种颜色 详细??
孔子提出“畏天命,获罪于天,无所祷也”。这
一个伟大僧人西行取经的传奇故事,一条由信念
参赛作品共有125幅,一等奖6幅,二等奖占参赛
春燕学生用品地址有知道的么?有点事想过去
伟江家电在哪里啊,我有事要去这个地方
实践和认识的关系能不能说两者的关系属于第一
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?