getObject by ProcessId

Marcel G

Mitglied
Hallo zusammen,
ich schreib grad ein Programm, das mit eine zweite Instanz Beispielsweise von EXCEL starten soll. Dies mach ich über den Shell Befehl, da ich ncoh weitere Parameter mitgebe.
Nun bekomme ich aber nur die ProzessID zurück, brauch aber das Object.
Hat von euch vielleicht jemand eine Idee wie man da drauf kommt? Ich hab im Netz schon bissle geforscht, aber einen wirklichen Ansatz hab ich auch nicht gefunden.
Code:
Private Declare Function openProcess Lib "kernel32" Alias "OpenProcess" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long

Public Function startProcess(command As String) As Long
    Dim hProcess As Long
    Dim ProcessID As Long
    Dim exitCode As Long
    
    processStartedId = Shell(command, 1)
    startProcess = processStartedId
    processStarted = True
    
    hProcess = openProcess(&H400, False, ProcessID)
    Do
       Call GetExitCodeProcess(hProcess, exitCode)
       DoEvents
    Loop While exitCode = &H103&
End Function

Sub Makro1()
    Dim oLibSystem As LibSystem
    Set oLibSystem = New LibSystem
    
    Dim ProcessID As Long
    ProcessID = oLibSystem.startProcess("C:\Programme\Microsoft Office\Office10\EXCEL.EXE")
    

    Set oWMIService = GetObject("winmgmts:\\.\root\cimv2")
    Dim collectionProcessList
    Set collectionProcessList = oWMIService.ExecQuery("Select * from Win32_Process Where ProcessID = " & processID)


    Dim activeObject
    For Each objProcess In collectionProcessList
        Set activeObject = objProcess
    Next
    'nun hab ich ein sWBemObject (WMI), aber wie komm ich nun von diesem auf das COM-Object/OLE Object. Vielleicht über die CLSID?

    Dim activeApplication As Object
    Set activeApplication = GetObject(, "EXCEL.Application") '<--- hier sollte das object über die processID zurück gegeben werden 
können
End Sub
 
Zuletzt bearbeitet:
Zurück