CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2015
    Posts
    1

    Error at CreateProcess, after setting parentprocess with UpdateProcThreadAttribute

    Hi there,

    our main application is called bei a starte rapplication, which checks for updates, installs them and then calls the main application. If the starter application is called via Explorer it's running as a child process of Explorer. When the starter application calls the main application, the main application is running as a child process of the starter application until the starter application closes itself automatically. After that the main process is running seperatly and not as a child process, which causes problems later on. I tried to solve this by setting the parent process of the main application to Explorer. Therefore I've translated the following code to VB.NET, which calls Notepad with any parent process:
    Code:
    Imports System.Diagnostics
    Imports System.IO
    Imports System.Runtime.InteropServices
    
    Public Class ProcessCreator_Test
     <DllImport("kernel32.dll")> _
     Private Shared Function CreateProcess(lpApplicationName As String, lpCommandLine As String, ByRef lpProcessAttributes As SECURITY_ATTRIBUTES, ByRef lpThreadAttributes As SECURITY_ATTRIBUTES, bInheritHandles As Boolean, dwCreationFlags As UInteger, _
      lpEnvironment As IntPtr, lpCurrentDirectory As String, <[In]> ByRef lpStartupInfo As STARTUPINFOEX, ByRef lpProcessInformation As PROCESS_INFORMATION) As <MarshalAs(UnmanagedType.Bool)> Boolean
     End Function
    
     <DllImport("kernel32.dll", SetLastError:=True)> _
     Private Shared Function UpdateProcThreadAttribute(ByRef lpAttributeList As IntPtr, dwFlags As UInteger, Attribute As IntPtr, lpValue As IntPtr, cbSize As IntPtr, lpPreviousValue As IntPtr, _
      lpReturnSize As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
     End Function
    
     <DllImport("kernel32.dll", SetLastError:=True)> _
     Private Shared Function InitializeProcThreadAttributeList(ByRef lpAttributeList As IntPtr, dwAttributeCount As Integer, dwFlags As Integer, ByRef lpSize As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
     End Function
    
     <DllImport("kernel32.dll", SetLastError:=True)> _
     Private Shared Sub DeleteProcThreadAttributeList(ByRef lpAttributeList As IntPtr)
     End Sub
    
     Public Shared Function CreateProcess(parentProcessId As Integer) As Boolean
    
      Const EXTENDED_STARTUPINFO_PRESENT As UInteger = &H80000
      Const PROC_THREAD_ATTRIBUTE_PARENT_PROCESS As Integer = &H20000
    
      Dim pInfo As PROCESS_INFORMATION = New PROCESS_INFORMATION()
      Dim sInfoEx As STARTUPINFOEX = New STARTUPINFOEX()
      sInfoEx.StartupInfo = New STARTUPINFO()
      sInfoEx.StartupInfo.cb = Marshal.SizeOf(sInfoEx)
    
      If parentProcessId > 0 Then
       Dim lpSize = IntPtr.Zero
       Dim success = InitializeProcThreadAttributeList(Nothing, 1, 0, lpSize)
       If success OrElse lpSize = IntPtr.Zero Then
        Return False
       End If
    
       sInfoEx.lpAttributeList = Marshal.AllocHGlobal(lpSize)
       If sInfoEx.lpAttributeList = IntPtr.Zero Then
        Return False
       End If
    
       success = InitializeProcThreadAttributeList(sInfoEx.lpAttributeList, 1, 0, lpSize)
       If Not success Then
        Return False
       End If
    
       Dim parentHandle = Process.GetProcessById(parentProcessId).Handle
       success = UpdateProcThreadAttribute(sInfoEx.lpAttributeList, 0, New IntPtr(PROC_THREAD_ATTRIBUTE_PARENT_PROCESS), parentHandle, New IntPtr(Marshal.SizeOf(parentHandle)), Nothing, Nothing)
       If Not success Then
        Return False
       End If
    
      End If
    
    
      Dim pSec As SECURITY_ATTRIBUTES = New SECURITY_ATTRIBUTES()
      Dim tSec As SECURITY_ATTRIBUTES = New SECURITY_ATTRIBUTES()
      pSec.nLength = Marshal.SizeOf(pSec)
      tSec.nLength = Marshal.SizeOf(tSec)
      Dim lpApplicationName = Path.Combine(Environment.SystemDirectory, "notepad.exe")
      Return CreateProcess(lpApplicationName, Nothing, pSec, tSec, False, EXTENDED_STARTUPINFO_PRESENT, _
       IntPtr.Zero, Nothing, sInfoEx, pInfo)
    
    
      DeleteProcThreadAttributeList(sInfoEx.lpAttributeList)
     End Function
    
     <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
     Private Structure STARTUPINFOEX
      Public StartupInfo As STARTUPINFO
      Public lpAttributeList As IntPtr
     End Structure
    
     <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
     Private Structure STARTUPINFO
      Public cb As Int32
      Public lpReserved As String
      Public lpDesktop As String
      Public lpTitle As String
      Public dwX As Int32
      Public dwY As Int32
      Public dwXSize As Int32
      Public dwYSize As Int32
      Public dwXCountChars As Int32
      Public dwYCountChars As Int32
      Public dwFillAttribute As Int32
      Public dwFlags As Int32
      Public wShowWindow As Int16
      Public cbReserved2 As Int16
      Public lpReserved2 As IntPtr
      Public hStdInput As IntPtr
      Public hStdOutput As IntPtr
      Public hStdError As IntPtr
     End Structure
    
     <StructLayout(LayoutKind.Sequential)> _
     Friend Structure PROCESS_INFORMATION
      Public hProcess As IntPtr
      Public hThread As IntPtr
      Public dwProcessId As Integer
      Public dwThreadId As Integer
     End Structure
    
     <StructLayout(LayoutKind.Sequential)> _
     Public Structure SECURITY_ATTRIBUTES
      Public nLength As Integer
      Public lpSecurityDescriptor As IntPtr
      Public bInheritHandle As Integer
     End Structure
    End Class
    This code is called with Explorer as parent process like following:
    Code:
    ProcessCreator_test.CreateProcess(Process.GetProcessesByName("explorer")(0).Id)
    Unfortunately the CreateProcess call leads to the following error (in German):
    Die Laufzeit hat einen schwerwiegenden Fehler entdeckt. Fehleradresse: "0x7316d1be" in Thread "0x1718". Fehlercode: 0xc0000005. Bei diesem Fehler könnte es sich um ein Problem in der CLR oder in den unsicheren oder nicht verifizierbaren Teilen des Benutzercodes handeln. Übliche Ursachen dieses Bugs sind Marshallerfehler für COM-Interop oder PInvoke, die den Stapel beschädigen können.
    Could you please help me to solve this Problem?

    Thanks a lot

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Error at CreateProcess, after setting parentprocess with UpdateProcThreadAttribut

    Windows doesn't have the concept of 'child' processes. It could be that you are relying on some environment settings that aren't getting passed; however, short of these, once one process launches another process, Windows doesn't track any interaction between the processes.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured