Page 1 of 1

Detecting the active display language in windows 7 upwards

Posted: Mon Apr 15, 2019 5:09 pm
by Bitblazer
Hi,

i am trying to correctly and reliably detect the currently used display language of a windows installation. It is simple to do by scanning the output, but i have'nt found a working reliable API way so far.

For exaple my main machine is using an english windows7 ultimate installation which has the german language pack installed as optional display language pack. If i call system commands on the commandline like "diskpart", the output differs depending on the display language i select.

The topic has been brought up on other forums including slashdot and there seems to be an easy solution by api calls like GetSystemDefaultLangID_() or GetUserDefaultLangID_() but none of them works reliably in windows 7 and windows 10 for me.

Does anybody know a reliable win32 API way to do it?

MSI maybe? It should have the answer, but where?

Thanks in advance!

Re: Detecting the active display language in windows 7 upwar

Posted: Mon Apr 15, 2019 6:01 pm
by RASHAD
Tested with Windows 10

Code: Select all

kbd$ = Space(12)
OpenWindow(0,0,0,100,100,"test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
StringGadget(0,0,0,0,0,"") ;Dummy Gadget
ButtonGadget(1,10,60,120,20,"Get Active Language")
Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = 1
    Case #PB_Event_Gadget
      Select EventGadget()       
        Case 0
          Select EventType()
            Case #PB_EventType_Focus
              GetKeyboardLayoutName_(@kbd$)
              Debug kbd$ 
          EndSelect
          
        Case 1
          SetFocus_(GadgetID(0)) 
      EndSelect
  EndSelect
Until Quit = 1
End

Code: Select all

kbd$ = Space(12)
OpenWindow(0,0,0,120,24,"test", #PB_Window_BorderLess | #PB_Window_ScreenCentered)
StringGadget(0,0,0,0,0,"") ;Dummy Gadget
ButtonGadget(1,0,0,120,24,"Get Active Language")
Repeat
  Select WaitWindowEvent()
    Case #WM_CHAR
      Quit = EventwParam()
      
    Case #PB_Event_Gadget
      Select EventGadget()       
        Case 0
          Select EventType()
            Case #PB_EventType_Focus
              GetKeyboardLayoutName_(@kbd$)
              SetActiveGadget(-1)
              MessageRequester("Info","Active Keboard : "+kbd$,#MB_OK|#MB_ICONINFORMATION)              
          EndSelect
         
        Case 1
          SetFocus_(GadgetID(0))
      EndSelect
  EndSelect
Until Quit = 27
End

Re: Detecting the active display language in windows 7 upwar

Posted: Mon Apr 15, 2019 7:04 pm
by VB6_to_PBx
Rashad , thank you for your Codes above , as i needed that also !

much more additional info in these 2 Links :

https://docs.microsoft.com/en-us/window ... uage-packs

https://docs.microsoft.com/en-us/window ... or-windows

Re: Detecting the active display language in windows 7 upwar

Posted: Mon Apr 15, 2019 7:48 pm
by Bitblazer
Thanks Rashad, but thats always 0407 (keyboard language name - just like MSI win32_operatingsystem\locale). What i need is the display language setting instead.

Re: Detecting the active display language in windows 7 upwar

Posted: Mon Apr 15, 2019 8:14 pm
by RASHAD
Check the Registry for
Windows 10 :
HKCU\Control Panel\Desktop\MuiCached

For other Windows versions
HKCU\Control Panel\Desktop\MultiUILanguageId

Code: Select all

sRemMachName.s = ""
lTopLevelKey.l = #HKEY_CURRENT_USER
sKeyName.s = "Control Panel\Desktop\MuiCached"
sValueName.s = "MachinePreferredUILanguages"

Procedure.l QueryValueEx(lhkey.l, szValueName.s) 
    Shared vValue.s 
    cch = 255 
    sValue.s = Space(255) 
    lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, 0, @cch)        
    Select lType
        Case #REG_MULTI_SZ
            lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, @sValue.s, @cch) 
            If lrc = 0 
                vValue.s = Left(sValue.s, cch-1) 
            Else 
                vValue.s = "Empty" 
            EndIf
        Default 
            lrc = -1 
    EndSelect 
    ProcedureReturn lrc 
EndProcedure

lRetVal = RegConnectRegistry_(sRemMachName, lTopLevelKey.l, @lHKeyhandle) 
lRetVal = RegOpenKeyEx_(lHKeyhandle, sKeyName, 0, #KEY_ALL_ACCESS, @lhkey) 
lRetVal = QueryValueEx(lhkey, sValueName) 
RegCloseKey_(lhkey) 

If lRetVal = 0 
  Debug vValue.s
EndIf

Re: Detecting the active display language in windows 7 upwar

Posted: Mon Apr 15, 2019 8:47 pm
by Bitblazer
Thanks but same problem - it reports en-us while the german language is active (de-de i guess) in win7.

Maybe its just a win7 problem?
\
ps: a registry search and 3 tests did seem to point at hkey_currentuser\control panel\Desktop\MuiCached\MachinePreferedUILanguages in win7

Re: Detecting the active display language in windows 7 upwar

Posted: Mon Apr 15, 2019 9:02 pm
by RASHAD
When you start your OS
What is the Active display language ?

Re: Detecting the active display language in windows 7 upwar

Posted: Tue Apr 16, 2019 5:36 am
by Bitblazer
This works with win7 even if you switch the language after boot. Thanks Rashad.

Code: Select all

sRemMachName.s = ""
lTopLevelKey.l = #HKEY_CURRENT_USER
sKeyName.s = "Control Panel\Desktop\"
sValueName.s = "PreferredUILanguages"

Procedure.l QueryValueEx(lhkey.l, szValueName.s)
    Shared vValue.s
    cch = 255
    sValue.s = Space(255)
    lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, 0, @cch)       
    Select lType
        Case #REG_MULTI_SZ
            lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, @sValue.s, @cch)
            If lrc = 0
                vValue.s = Left(sValue.s, cch-1)
            Else
                vValue.s = "Empty"
            EndIf
        Default
            lrc = -1
    EndSelect
    ProcedureReturn lrc
EndProcedure

lRetVal = RegConnectRegistry_(sRemMachName, lTopLevelKey.l, @lHKeyhandle)
lRetVal = RegOpenKeyEx_(lHKeyhandle, sKeyName, 0, #KEY_ALL_ACCESS, @lhkey)
lRetVal = QueryValueEx(lhkey, sValueName)
RegCloseKey_(lhkey)

If lRetVal = 0
  Debug vValue.s
EndIf

Re: Detecting the active display language in windows 7 upwar

Posted: Thu Apr 18, 2019 5:38 am
by kvitaliy
There is no such key in my system (win 7x64)
lTopLevelKey.l = #HKEY_CURRENT_USER
sKeyName.s = "Control Panel\Desktop\"
sValueName.s = "PreferredUILanguages"
This code works:

Code: Select all

sRemMachName.s = ""
lTopLevelKey.l = #HKEY_CURRENT_USER
sKeyName.s = "Control Panel\International"
sValueName.s ="LocaleName" ;  or "sLanguage"  ; or "sCountry"

Procedure.l QueryValueEx(lhkey.l, szValueName.s)
    Shared vValue.s
    cch = 255
    sValue.s = Space(255)
    lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, 0, @cch)       
    Select lType
        Case #REG_SZ
            lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, @sValue.s, @cch)
            If lrc = 0
                vValue.s = Left(sValue.s, cch-1)
            Else
                vValue.s = "Empty"
            EndIf
        Default
            lrc = -1
    EndSelect
    ProcedureReturn lrc
EndProcedure

lRetVal = RegConnectRegistry_(sRemMachName, lTopLevelKey.l, @lHKeyhandle)
lRetVal = RegOpenKeyEx_(lHKeyhandle, sKeyName, 0, #KEY_ALL_ACCESS, @lhkey)
lRetVal = QueryValueEx(lhkey, sValueName)
RegCloseKey_(lhkey)

If lRetVal = 0
  Debug vValue.s
EndIf

Re: Detecting the active display language in windows 7 upwar

Posted: Thu Apr 18, 2019 11:56 am
by Bitblazer
kvitaliy wrote:There is no such key in my system (win 7x64)
lTopLevelKey.l = #HKEY_CURRENT_USER
sKeyName.s = "Control Panel\Desktop\"
sValueName.s = "PreferredUILanguages"
This code works:

Code: Select all

sRemMachName.s = ""
lTopLevelKey.l = #HKEY_CURRENT_USER
sKeyName.s = "Control Panel\International"
sValueName.s ="LocaleName" ;  or "sLanguage"  ; or "sCountry"

Procedure.l QueryValueEx(lhkey.l, szValueName.s)
    Shared vValue.s
    cch = 255
    sValue.s = Space(255)
    lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, 0, @cch)       
    Select lType
        Case #REG_SZ
            lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, @sValue.s, @cch)
            If lrc = 0
                vValue.s = Left(sValue.s, cch-1)
            Else
                vValue.s = "Empty"
            EndIf
        Default
            lrc = -1
    EndSelect
    ProcedureReturn lrc
EndProcedure

lRetVal = RegConnectRegistry_(sRemMachName, lTopLevelKey.l, @lHKeyhandle)
lRetVal = RegOpenKeyEx_(lHKeyhandle, sKeyName, 0, #KEY_ALL_ACCESS, @lhkey)
lRetVal = QueryValueEx(lhkey, sValueName)
RegCloseKey_(lhkey)

If lRetVal = 0
  Debug vValue.s
EndIf
beware - on my english win7 ultimate installation with two installed display languages (german and english), the following keys all point to german

Code: Select all

lTopLevelKey.l = #HKEY_CURRENT_USER
sKeyName.s = "Control Panel\International"
sValueName.s ="LocaleName" ;  or "sLanguage"  ; or "sCountry"
while

Code: Select all

lTopLevelKey.l = #HKEY_CURRENT_USER
sKeyName.s = "Control Panel\Desktop\MuiCached"
sValueName.s ="MachinePreferredUIlanguages"
correctly points to the currently active display language.

So i guess we should use that key and only if it doesnt exist, use the others as fallback option. Thats what im going to do now (and probably evaluate it by calling an OS tool and check which language the tool actually uses ;). What a mess :o