Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Whats wrong with My Code ? error message on top

Status
Not open for further replies.

Joao Marques

Programmer
Mar 21, 2019
1
0
0
PT
System.InvalidCastException: 'Unable to cast COM object of type 'System.__ComObject' to interface type 'System.Collections.IEnumerable'.
This operation failed because the QueryInterface call on the COM component for the interface with IID '{496B0ABE-CDEE-11D3-88E8-00902754C43A}'
failed due to the following error: 'No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))'
and the COM component does not support IDispatch::Invoke calls for DISPID_NEWENUM.'



Function GetMemory(computador, utilizador, pwd, dominio)

Dim objWMIService, objSWbemLocator
objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
objWMIService = objSWbemLocator.ConnectServer(computador, "\root\CIMv2", utilizador, pwd)
Dim objOs = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem",, 48)
For Each objOs In objWMIService
GetMemory = objOs.GetPropertyValue("FreePhysicalMemory")
Next
End Function
Public Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim struser, strpassword, strcomputer, strdomain
Dim objWMIService, objProcess, objSWbemLocator

strcomputer = "myremoteserver"
strdomain = "mydonain"
struser = "myuser"
strpassword = "mypassword"

GetMemory(strcomputer, struser, strpassword, strdomain)

objWMIService = Nothing
objProcess = Nothing
objSWbemLocator = Nothing
End Sub

Your help will be appreciated
 
Erm … the error message and this line

Public Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

suggest that this isn't really VBScript, but rather is VB.NET (with Option Explicit set to OFF). I presume that someone has tried to port a vbscript example into VB.NET. But fortunately the issue isn't actually a VB.NET problem (although .NET is trying to tell you the problem). Basically you are trying to enumerate an object that can't be enumerated (that's what all that stuff about DISPID_NEWENUM means).

Change

Code:
[blue]For Each objOs In objWMIService
    GetMemory = objOs.GetPropertyValue("FreePhysicalMemory")
Next[/blue]

to

Code:
[COLOR=blue]For Each objEnum in objOS
    GetMemory = objEnum.GetPropertyValue("FreePhysicalMemory")
Next[/color]


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top