How to start a process using WMI ("winmgmts:Win32_Process") with hidden window!?

louis1-6497 0 Reputation points
2024-09-11T09:29:40.4133333+00:00

I try to start processes from MsAccess VBA using WMI

Set process = GetObject("winmgmts:Win32_Process")

Result = process.Create(CmdStr, Null, Null, ProcessID)

That works however this opens a window. .... And I do not want a window, I would like the task without or with a hidden window.

In the example given by microsoft (see link below at the bottom of the page)

'https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/create-method-in-class-win32-process'

Set objConfig = objStartup.SpawnInstance_

objConfig.ShowWindow = SW_NORMAL"  

is used to determ the window type.

However using that option does lead to a ^security issue schutdown^ a few lines later when starting the process.

So I need a solution to start a process using WMI without window. Note that I can nu use Shell(), since the Shell() function does not return the correct PID (WMI does).

Below some details what is happening using Microsofts Example code.

the microsoft example code does not work. Actual windows security block it / closes MsAccess.

To be more precise

Set objWMIService = GetObject("winmgmts:" _

    & "{impersonationLevel=impersonate}!\\" _

    & strComputer & "\root\cimv2")

Is accepted ... however adding

Set objStartup = objWMIService.Get("Win32_ProcessStartup")

Does lead to "security close" at line

intReturn = objProcess.Create _

    (strCommand, Null, objConfig, intProcessID)
Microsoft 365
Microsoft 365
Formerly Office 365, is a line of subscription services offered by Microsoft which adds to and includes the Microsoft Office product line.
5,188 questions
Access
Access
A family of Microsoft relational database management systems designed for ease of use.
402 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
10,001 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. louis1-6497 0 Reputation points
    2024-09-11T17:57:00.37+00:00

    I think the code below works.

    However note:

    • that I did see that not every start is successful for reasons not always known
    • not all applications can be started with a hidden window
    • some applications need time to start and/or stop
    • some applications can not be started multiple times in parallel

    So it is mandatory to check the result code!

    It is possible to extend this function with extra options (directory to use etc, as additional (optional) parameter. But that makes thinks more complex and I do not need that at the moment

    Be aware that you need to make this function / the MsAccess or Excel version trusted, otherwise the windows macro security protection will show a popup and the application will be killed,

    Sub StartProcessWithWMI(ByRef PID As Integer, ByVal CmdStr As String, ByRef Result As Integer, Optional ByVal HideWindow As Boolean = False)

    '>> TO RUN THIS CODE the code / this file MUST be trusted by Windows !! <<
    
    '>> Use e.g. File -> Options -> TrustCenter -> add location of this DB as trusted path
    
    '>> If Result <> '0' the process has not been started !! see below 'ResultCode'
    
    'https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/create-method-in-class-win32-process
    
    'PID can be verified via windows taskmanager tab details
    
    '>> ResultCode <<
    
    'Successful completion(0)
    
    'Access denied(2)
    
    'Insufficient privilege(3)
    
    'Unknown failure(8)
    
    'Path Not found(9)
    
    'Invalid Parameter(21)
    
    'Other (22 4294967295)
    
    Dim process As Object
    
    
    
    Dim strComputer As String
    
    Dim objWMIService As Object
    
    Dim objStartup As Object
    
    Dim objConfig As Object
    
    strComputer = "."   'local computer name is specified using '.'
    
    Set objWMIService = GetObject("winmgmts:" _
    
         & "{impersonationLevel=impersonate}!\\" _
    
        & strComputer & "\root\cimv2")
    
    Set objStartup = objWMIService.Get("Win32_ProcessStartup")
    
    
    
    If HideWindow Then
    
        Set objConfig = objStartup.SpawnInstance_
    
        objConfig.ShowWindow = vbHide
    
    Else
    
        Set objConfig = objStartup.SpawnInstance_
    
        objConfig.ShowWindow = vbNormalFocus
    
    End If
    
     
    
    Set process = GetObject("winmgmts:Win32_Process")
    
    Result = process.Create(CmdStr, Null, objConfig, PID)
    
    Set process = Nothing
    
    Set objConfig = Nothing
    
    Set objStartup = Nothing
    
    Set objConfig = Nothing
    
    DoEvents
    

    End Sub

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.