How To Get the Current User Locale ID in a VB EXE Without Restarting (217751)



The information in this article applies to:

  • Microsoft Visual Basic Learning Edition for Windows 5.0
  • Microsoft Visual Basic Learning Edition for Windows 6.0
  • Microsoft Visual Basic Professional Edition for Windows 5.0
  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 5.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0

This article was previously published under Q217751

SUMMARY

The Locale ID (LCID) is a 32-bit value that consists of a language ID plus a sort ID and other reserved bits. A language ID is a 16-bit value consisting of a primary language ID and a secondary language ID. After changes have been made to the Language setting in the Regional Settings applet, it is sometimes necessary to get the current user LCID value either in the IDE or within an application without closing down the application. This can be done by calling GetThreadLocale from within an ActiveX EXE server. The server creates its own thread and, by calling GetThreadLocale, the current value can be obtained.

Without using an out-of-process server, the value obtained by calling GetThreadLocale from within a standard EXE application might not be current. You might also see that the value does not get updated in the IDE.

Some of the API calls that use the LCID as a parameter are GetLocaleInfo, GetTimeFormat, and GetDateFormat.

MORE INFORMATION

To create server and client programs to illustrate this technique, do the following:

Create the ActiveX server to get the current value of the LCID.

  1. Create a new ActiveX EXE project in Visual Basic. Class1 is created by default.
  2. In the Class Module, Class1, place the following code:
    Private Declare Function GetThreadLocale Lib "kernel32" () As Long
    Public Function GetLCID() As Long
        GetLCID = GetThreadLocale()
    End Function
  3. Click on the Properties menu and select Project1.Properties.
  4. On the General tab, set the Project Name and the description to lcidsrvr.
  5. Also on the General tab, set the Threading model to Thread Per Object.
  6. Click OK.
  7. From the File menu, click on the Make Project1.exe item and enter LCIDsrv as the name of the EXE.
  8. Click OK.

Create the client application and run the example.

  1. Bring up another instance of Visual Basic and create a Standard EXE project. Form1 is created by default.
  2. Place a TextBox (Text1) and a CommandButton (Command1) on Form1.
  3. Place the following code in the General Declarations section of Form1:
    Private Declare Function GetThreadLocale Lib "kernel32" () As Long
    Private Sub Command1_Click()
        Dim LCID As Long
        Dim oLCID As Object
        ' Use out of proc server to get LCID in separate thread
        Set oLCID = CreateObject("lcidsrvr.class1")
        LCID = oLCID.GetLCID
        Text1.Text = LCID & " (" & Hex$(LCID) & ")"
    End Sub
    					
  4. Bring up the Regional Settings applet by clicking on the Start button and selecting Settings. Click on Control Panel, and then click on Regional Settings.
  5. Set the Language on the Regional Settings tab to English (United States) and click the Apply button. (If the Language is already set to English (United States), leave as is.)
  6. Run the application and click Command1. You should see the current value of the LCID appear in the TextBox. For US English, this value is 1033 or 409 hex.
  7. Change the Language in Regional Settings to English (United Kingdom) and click the Apply button.
  8. Click Command1 and the LCID value displayed in the TextBox is 2057 or 809 hex.

Modification Type:MinorLast Reviewed:7/1/2004
Keywords:kbAPI kbhowto kbLocalSvr KB217751 kbAudDeveloper