Click here to Skip to main content
15,867,939 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 86.7K   300   15   14
Getting AppLocale to run in a batch script

Introduction

Run Microsoft AppLocale Utility successfully in a batch script by calling it with a wrapper written in .NET.

Background

The Microsoft AppLocale is a nifty utility that allows a program to run under a different system locale without having to reset the region settings and reboot the PC. This is ideal for applications, such as HTML Help Workshop, that require to be run under a target locale code. You can find more info about the utility here.

However there are two drawbacks to this utility if you want to use it in an automated batch script.

  • A nag screen appears when calling AppLocale from the command line.
  • AppLocale runs, spawns the called application in a child process, then quits while the child process is still active.

Luckily there is a patch/hack for AppLocale to remove the nag screen that can be found here.

The second issue is a bit more tricky because you want the spawned application to complete before continuing to the next batch command. For example, let's say we want a batch file to compile a Japanese language CHM file, then move it to a location on another computer.

  1. AppLocale will call HTML Help Workshop under the Japanese code page, and as soon as the application is launched, AppLocale will quit.
  2. HTML Help Workshop will begin to compile the CHM file.
  3. The batch file goes to its next command which is to move (a currently non-existent) CHM file... and we get file not found errors.
  4. Finally the CHM file is finished compiling and ready to be moved.

See the problem? How can we get around this? Well I am glad you asked.

Using the Code

What this code will do is launch AppLocale and grab its Windows Process ID, search for the running processes whose Parent Process ID is equal to AppLocale's Process ID, then wait for that process to end.

For our purposes, we want to create a command-line project in Visual Studio .NET. First we need to create a Process object to call AppLocale, then grab its Process ID which we can use to search for the child process that it launches. For StartInfo.Arguments we want to pass in the program we are calling, its arguments and local ID in the same style noted in the AppLocale documentation.

VB.NET
Dim procAppLoc As Process
Dim intAppLocID as Integer

'Create the AppLocale process object.
procAppLoc = New Process

'Set up the process
procAppLoc.StartInfo.FileName = "C:\Windows\AppPatch\AppLoc.exe"
procAppLoc.StartInfo.Arguments = Program we are calling, its arguments & local ID
procAppLoc.StartInfo.ErrorDialog = True
procAppLoc.EnableRaisingEvents = True

'Start the process
procAppLoc.Start()

'Get the AppLocale process ID
intAppLocID = procAppLoc.Id

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

Next we use a Win32_Process object which gathers more information than the regular Process object, to search for the process that has a Parent Process ID equal to our AppLocale Process ID.

VB.NET
Dim objWMI As Object
Dim colItems As Object
Dim objItem As Object
Dim intChildProcessID As Integer

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

'Find the child process and retrieve its ID value
intChildProcessID = -1
For Each objItem In colItems
   If objItem.ParentProcessId = intAppLocID Then
      intChildProcessID = objItem.ProcessId
   End If
Next

So now that we found the Process ID of the application that AppLocale launched, we can place this into a Process object and wait for it to exit before continuing.

VB.NET
Dim procChild As Process

If intChildProcessID > -1 Then
   'Get the child process object
   procChild = Process.GetProcessById(intChildProcessID)

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

Now the program we have created will wait for the spawned child process to end before it ends itself and the batch script can move to the next command.

Once AppLocale is installed, the AppLocale executable file is patched and this code is compiled. You can now call this program instead of calling AppLocale with the same set of arguments.

MyAppLocWrapper.exe program [arguments] /L[local id]

Points of Interest

There is another alternative to AppLocale, and that is SBAppLocale. It did not work for our needs but that doesn't mean it won't work for you and I would suggest considering it as an option. You can find information about SBAppLocale here.

One thing I noticed is that AppLocale can be very finicky when being called from the command line. Here are a few points to note:

  • AppLocale can only call executables and not batch files.
  • Pass in all of the program arguments surrounded by quotes.
  • Always use absolute paths.
  • If you see the GUI window appear, it probably means it didn't like what was input.
  • Always delete the C:\Windows\AppPatch\AppLoc.tmp file to ensure a clean run.
  • In Control Panel > Regional and Language Options > Language tab, check all boxes in the "Supplemental language support" section.

A trick to get around not being able to launch a batch file is to run AppLocale with cmd.exe as the called program like this:

C:\Windows\AppPatch\AppLoc.exe C:\WINDOWS\system32\cmd.exe "/c
C:\Scripts\MyBatchFile.bat FakeParameter1" /L1041

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

 
QuestionAppLocale wrapper for SendTo links? Pin
Alexander Firestone8-May-08 2:22
Alexander Firestone8-May-08 2:22 
AnswerRe: AppLocale wrapper for SendTo links? Pin
Austin Rappa14-May-08 8:53
Austin Rappa14-May-08 8:53 
GeneralRe: AppLocale wrapper for SendTo links? Pin
Alexander Firestone14-May-08 10:37
Alexander Firestone14-May-08 10:37 
GeneralRe: AppLocale wrapper for SendTo links? Pin
Austin Rappa14-May-08 11:06
Austin Rappa14-May-08 11:06 
AnswerRe: AppLocale wrapper for SendTo links? Pin
Austin Rappa22-May-08 12:19
Austin Rappa22-May-08 12:19 
GeneralRe: AppLocale wrapper for SendTo links? Pin
Alexander Firestone22-May-08 12:55
Alexander Firestone22-May-08 12:55 
GeneralRe: AppLocale wrapper for SendTo links? Pin
Mirko Plitt1-Dec-08 22:35
Mirko Plitt1-Dec-08 22:35 
AnswerRe: AppLocale wrapper for SendTo links? Pin
Mirko Plitt3-Dec-08 22:11
Mirko Plitt3-Dec-08 22:11 
GeneralRe: AppLocale wrapper for SendTo links? Pin
spamfighter21-Jun-10 11:06
spamfighter21-Jun-10 11:06 
GeneralWhy not... Pin
SlimFast200025-Apr-08 1:41
SlimFast200025-Apr-08 1:41 
GeneralRe: Why not... [modified] Pin
Austin Rappa29-Apr-08 9:29
Austin Rappa29-Apr-08 9:29 
GeneralSBAppLocale Pin
SteveKing24-Apr-08 19:23
SteveKing24-Apr-08 19:23 
GeneralRe: SBAppLocale Pin
Austin Rappa24-Apr-08 20:28
Austin Rappa24-Apr-08 20:28 
GeneralRe: SBAppLocale Pin
SteveKing25-Apr-08 4:19
SteveKing25-Apr-08 4:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.