'Visual Basic - 'GetObject' Not Declared
I've been running into an issue while attempting to update a script from VBScript to standard VB. The script simply checks for any running processes of Excel, then determines how long they've been running and shuts them down if that time has been longer than 4 minutes. Here's the script:
Imports System
Module Program
Sub Main()
Dim objWMIService As Object, objProcess, colProcess, startDate, timeDiff
objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
colProcess = objWMIService.ExecQuery("Select * from Win32_Process Where Name = " & "'EXCEL.EXE'")
For Each objProcess In colProcess
startDate = WMIDateStringToDate(objProcess.creationdate)
timeDiff = DateDiff("n", startDate, Now)
If timeDiff > 4 Then
objProcess.Terminate()
End If
Next
End Sub
Function WMIDateStringToDate(dtmInstallDate As String) As Date
WMIDateStringToDate = CDate(
Mid(dtmInstallDate, 5, 2) & "/" &
Mid(dtmInstallDate, 7, 2) & "/" &
Left(dtmInstallDate, 4) & " " &
Mid(dtmInstallDate, 9, 2) & ":" &
Mid(dtmInstallDate, 11, 2) & ":" &
Mid(dtmInstallDate, 13, 2))
End Function
End Module
Overall, the script should work. However, there's one thing that doesn't want to work - a call to GetObject. I'm coding in Visual Studio, and it gives me the error BC30451 ('GetObject' is not declared). I understand what the error means, but my question is, why wouldn't GetObject exist? Isn't it a built-in function with Visual Basic?
I attempted switching out GetObject for CreateObject, but I got the feeling that changing it to CreateObject wouldn't help my problem. Plus I got the 'Cannot create ActiveX component' error when I did, so that was another dead end.
Am I missing something like a reference to a library or that sort of idea? Is my syntax mildly off? I've attempted to read up on the documentation, but it doesn't seem like anything is different between the docs and my code except for the Set statement before the use of GetObject. But Visual Studio tells me Let and Set are obsolete anyway.
Thank you to anyone who can help me figure this out
Solution 1:[1]
After a bunch of contribution in the comments and a bit more searching through the documentation, I was finally able to come up with a solution. The program works fully as intended. I'll post the code below, in case anyone else ever needs it:
Imports System
Imports System.Management
Module Program
Sub Main()
Dim objWMIService As New ManagementObjectSearcher(
"root\CIMV2",
"SELECT * FROM Win32_Process WHERE Name = " & "'EXCEL.EXE'"), startDate, timeDiff
For Each queryObj As ManagementObject In objWMIService.Get()
Dim inParams As ManagementBaseObject =
queryObj.GetMethodParameters("Terminate")
startDate = WMIDateStringToDate(queryObj("CreationDate"))
timeDiff = DateDiff("n", startDate, Now)
If timeDiff > 4 Then
queryObj.InvokeMethod("Terminate", inParams, Nothing)
End If
Next
End Sub
Function WMIDateStringToDate(dtmInstallDate As String) As Date
WMIDateStringToDate = CDate(
Mid(dtmInstallDate, 5, 2) & "/" &
Mid(dtmInstallDate, 7, 2) & "/" &
Left(dtmInstallDate, 4) & " " &
Mid(dtmInstallDate, 9, 2) & ":" &
Mid(dtmInstallDate, 11, 2) & ":" &
Mid(dtmInstallDate, 13, 2))
End Function
End Module
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | CMBadiuk |