Get process owner returns an error in VB

0

I have some code that I found on this site which seems to work great most of the time.

Here is the code :

 Private Function GetProcessOwner(processId As Integer) As String
    Try
        Dim query = "Select * From Win32_Process Where ProcessID = " & processId
        Dim searcher = New ManagementObjectSearcher(query)
        Dim processList = searcher.Get()
        For Each obj As ManagementObject In processList
            Dim argList As String() = {String.Empty, String.Empty}
            Dim returnVal = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList))
            If returnVal = 0 Then
                Return argList(1) & "\" & argList(0)
            End If
        Next
    Catch ex As Exception
    End Try

    Return ""
End Function

However, sometime I get this :

RuntimeCallableWrapper avec l'erreur suivante : Un appel sortant ne peut pas être effectué étant donné que l’application répartit un appel entrant synchrone. (Exception de HRESULT : 0x8001010D (RPC_E_CANTCALLOUT_ININPUTSYNCCALL)). Cela se produit habituellement car le contexte COM 0x78e910 dans lequel ce RuntimeCallableWrapper a été créé a été déconnecté ou est occupé à autre chose et ne peut pas traiter la transition des contextes. Aucun proxy ne sera utilisé pour traiter la demande sur le composant COM. Cela peut entraîner des dysfonctionnements ou des pertes de données. Pour éviter ce problème, assurez-vous que tous les contextes/cloisonnements/threads COM restent actifs et sont disponibles pour la transition des contextes, tant que l'application n'en a pas terminé avec les RuntimeCallableWrappers qui représentent les composants COM qui s'y trouvent.

It in french but I can't really understand it in my own language so I can't really translate. Can anyone tell me that I am doing wrong and what to do to correct it ?

I found there was another way to get the process owner (http://www.codeproject.com/Articles/14828/How-To-Get-Process-Owner-ID-and-Current-User-SID) , but it's in c# and the whole project is in VB. Plus when I copy the code I get compile error on the word HANDLE. I wouldn't mind using this code but I just can't make it work

Can anybody help me ?

Thsnks

vb.net
process

1 Answer

1
  1. translate.google.com is your friend.
  2. From the translation, it looks like you might have an outstanding previous invocation going.
  3. As ManagementObjectSearcher implements IDisposable, I would create (and dispose) of it with a Using block like so:

    Using searcher as New ManagementObjectSearcher(query) [...] End Using

That will ensure that searcher is properly disposed of, as it has references to unmanaged resources. (Having a previous searcher around could be the cause of your problems.)


User contributions licensed under CC BY-SA 3.0