Click here to Skip to main content
15,894,410 members
Articles / Programming Languages / Visual Basic

Running the Microsoft AppLocale Utility in an Automated Batch Script

Rate me:
Please Sign up or sign in to vote.
4.67/5 (2 votes)
30 Oct 2009CPOL3 min read 87.2K   300   15  
Getting AppLocale to run in a batch script
Imports System
Imports System.Management

Module clsRunAppLocale

    '**************************************************************************
    ' Function: Main
    ' Author:   Austin Rappa
    ' Date:     4/15/2008
    ' Purpose:  Run MS AppLoc.exe in a wrapper to wait until the child process it launches finishes.
    '**************************************************************************
    Sub Main()
        Dim procAppLoc As Process
        Dim procChild As Process
        Dim strCommandLineArgs() As String
        Dim strAppLoc As String
        Dim strArguments As String
        Dim intAppLocID As Integer
        Dim intChildProcessID As Integer
        Dim i As Integer

        Try
            strCommandLineArgs = Environment.GetCommandLineArgs

            'Check we have a good amount of command-line arguments
            If strCommandLineArgs.Length > 1 Then

                'Output: Header
                Console.WriteLine("AppLoc.exe Wrapper")
                Console.WriteLine("--------------------------------------")
                Debug.WriteLine("AppLoc.exe Wrapper")
                Debug.WriteLine("--------------------------------------")

                'Set up filename and arguements
                strAppLoc = Environment.GetEnvironmentVariable("windir") & "\AppPatch\AppLoc.exe"
                strArguments = FormatCommandLineArguments(strCommandLineArgs)

                'Create the AppLocale process object. 
                procAppLoc = New Process

                'Set up the process
                procAppLoc.StartInfo.FileName = strAppLoc
                procAppLoc.StartInfo.Arguments = strArguments
                procAppLoc.StartInfo.ErrorDialog = True
                procAppLoc.EnableRaisingEvents = True

                'Output: Starting AppLoc.exe
                Console.WriteLine(" Starting AppLoc.exe")
                Debug.WriteLine(" Starting AppLoc.exe")

                'Start the process
                procAppLoc.Start()

                'Get the AppLocale process ID
                intAppLocID = procAppLoc.Id

                'Wait for AppLocale to fire off the child process and exit
                procAppLoc.WaitForExit()
                procAppLoc.Dispose()

                'Output: Getting child process info
                Console.WriteLine(" Getting child process info")
                Debug.WriteLine(" Getting child process info")

                'Get the child process ID
                intChildProcessID = GetChildProcessID(intAppLocID)

                If intChildProcessID > -1 Then

                    'Get the child process object
                    procChild = Process.GetProcessById(intChildProcessID)

                    'Output: Waiting for child process to exit
                    Console.WriteLine(" Waiting for child process to exit")
                    Debug.WriteLine(" Waiting for child process to exit")

                    'Wait for the child process to exit
                    procChild.WaitForExit()
                    procChild.Dispose()

                    'Output: Done
                    Console.WriteLine(" Done")
                    Debug.WriteLine(" Done")
                Else
                    'Output: No child process found
                    Console.WriteLine(" No child process found")
                    Debug.WriteLine(" No child process found")
                End If
            Else
                'Output help
                Console.WriteLine("Creates a wrapper around AppLoc.exe to wait for it's child process to finish.")
                Console.WriteLine("")
                Console.WriteLine("RunAppLocale.exe program [args] [/L locale id]")
                Console.WriteLine("")
                Debug.WriteLine("Creates a wrapper around AppLoc.exe to wait for it's child process to finish.")
                Debug.WriteLine("")
                Debug.WriteLine("RunnppLocale.exe program [args] [/L locale id]")
                Debug.WriteLine("")
            End If

        Catch ex As Exception
            Console.WriteLine("Error in RunAppLoc.exe: " & ex.Message)
            Debug.WriteLine("Error in RunAppLoc.exe: " & ex.Message)
        End Try

        'Cleanup
        Erase strCommandLineArgs
        procChild = Nothing
        procAppLoc = Nothing
        strAppLoc = vbNullString
        strArguments = vbNullString
    End Sub




    '**************************************************************************
    ' Function: GetChildProcessID
    ' Author:  Austin Rappa
    ' Date:     4/15/2008
    ' Purpose:  Users the Win32_Process object to find the child PID that a process has launched.
    '**************************************************************************
    Function GetChildProcessID(ByVal intParentProcessID As Integer) As Integer
        Dim objWMI As Object
        Dim colItems As Object
        Dim objItem As Object
        Dim intProcessID As Integer
        Const wbemFlagReturnImmediately = &H10
        Const wbemFlagForwardOnly = &H20
        Const Computer = "."

        'Initialize variables
        intProcessID = -1

        'Set up Win32_Process object
        objWMI = GetObject("winmgmts:\\" & Computer & "\root\CIMV2")
        colItems = objWMI.ExecQuery("SELECT * FROM Win32_Process", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
        colItems = objWMI.ExecQuery("SELECT * FROM Win32_Process", "WQL")

        'Find the child process and retrieve its ID value
        For Each objItem In colItems
            If objItem.ParentProcessId = intParentProcessID Then
                intProcessID = objItem.ProcessId
            End If
        Next

        'Cleanup
        objItem = Nothing
        colItems = Nothing
        objWMI = Nothing

        Return intProcessID
    End Function





    '**************************************************************************
    ' Function: FormatCommandLineArguments
    ' Author:  Austin Rappa
    ' Date:     4/15/2008
    ' Purpose:  Format the arguments passed from the command line in a format that AppLoc will enjoy.
    '**************************************************************************
    Function FormatCommandLineArguments(ByRef strArgs() As String) As String
        Dim strReturnValue As String
        Dim strProgram As String
        Dim strArguments As String
        Dim strLocaleID As String
        Dim i As Integer

        strProgram = strArgs(1)
        strLocaleID = strArgs(strArgs.Length - 1)

        strArguments = ""
        For i = 2 To strArgs.Length - 2
            strArguments &= strArgs(i).Replace("""", "") & " "
        Next
        strArguments = strArguments.Trim

        If strArguments.Length > 0 Then
            strReturnValue = strProgram & " """ & strArguments & """ " & strLocaleID
        Else
            strReturnValue = strProgram & " " & strLocaleID
        End If

        Return strReturnValue
    End Function

End Module

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Symantec Corp.
United States United States
Austin likes to play football, go surfing, bar hopping, sing karaoke, and play Guitar Hero and Wii.

Comments and Discussions