Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. GetThreadPreferredUILanguages

GetThreadPreferredUILanguages

Scheduled Pinned Locked Moved C#
questioncsharpc++visual-studiocom
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    marca292
    wrote on last edited by
    #1

    Hi, I need to get all installed languages on the computer. "Control Panel / Regional and Language / Keyboards and Languages / Display language". How can I get the selected language in .NET? Our application has language settings for a lot of different languages. If our application don't has support for the selected language in "Display language" then we have to see if we has support for a fallback language. I tried to use "GetThreadPreferredUILanguages" call and it work fine when I use C++, but I can't get is working on .NET. Is there an .NET call I can use instead to get selected languages and also the fallback languages? C++ code

    wchar\_t buf\[2323\];
    
    wchar\_t \*pBuf;
    
    ULONG numLangs;
    
    ULONG size= 2323;
    
    
    
    GetThreadPreferredUILanguages(MUI\_MERGE\_USER\_FALLBACK, &numLangs, buf, &
    

    C# code

    [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]

        static extern System.Boolean GetThreadPreferredUILanguages(
    
            System.UInt32 dwFlags,
    
            ref System.UInt32 pulNumLanguages,
    
            **out** System.IntPtr pwszLanguagesBuffer,
    
            ref System.UInt32 pcchLanguagesBuffer
    
            );
    
    
    
    
    
          uint MUI\_MERGE\_USER\_FALLBACK = 0x20;
    
            uint numLang = 0;
    
            IntPtr pwszLangBuf = new IntPtr();
    
            uint langBuf = 2323;
    
            bool b = GetThreadPreferredUILanguages(MUI\_MERGE\_USER\_FALLBACK, ref numLang, out pwszLangBuf, ref langBuf);
    
    
    
            string tmp = Marshal.PtrToStringAuto(pwszLangBuf
    

    tmp string variable contains only a lot of chinese characters after Marshal.PtrToStringAuto(pwszLangBuf);. pulNumLanguages and pcchLanguagesBuffer returns the same in both C++ and C#. See more information about GetThreadPreferredUILanguages on http://msdn.microsoft.com/en-us/library/dd318128(v=vs.85).aspx[^] What is wrong? Regards Olof

    L B 2 Replies Last reply
    0
    • M marca292

      Hi, I need to get all installed languages on the computer. "Control Panel / Regional and Language / Keyboards and Languages / Display language". How can I get the selected language in .NET? Our application has language settings for a lot of different languages. If our application don't has support for the selected language in "Display language" then we have to see if we has support for a fallback language. I tried to use "GetThreadPreferredUILanguages" call and it work fine when I use C++, but I can't get is working on .NET. Is there an .NET call I can use instead to get selected languages and also the fallback languages? C++ code

      wchar\_t buf\[2323\];
      
      wchar\_t \*pBuf;
      
      ULONG numLangs;
      
      ULONG size= 2323;
      
      
      
      GetThreadPreferredUILanguages(MUI\_MERGE\_USER\_FALLBACK, &numLangs, buf, &
      

      C# code

      [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]

          static extern System.Boolean GetThreadPreferredUILanguages(
      
              System.UInt32 dwFlags,
      
              ref System.UInt32 pulNumLanguages,
      
              **out** System.IntPtr pwszLanguagesBuffer,
      
              ref System.UInt32 pcchLanguagesBuffer
      
              );
      
      
      
      
      
            uint MUI\_MERGE\_USER\_FALLBACK = 0x20;
      
              uint numLang = 0;
      
              IntPtr pwszLangBuf = new IntPtr();
      
              uint langBuf = 2323;
      
              bool b = GetThreadPreferredUILanguages(MUI\_MERGE\_USER\_FALLBACK, ref numLang, out pwszLangBuf, ref langBuf);
      
      
      
              string tmp = Marshal.PtrToStringAuto(pwszLangBuf
      

      tmp string variable contains only a lot of chinese characters after Marshal.PtrToStringAuto(pwszLangBuf);. pulNumLanguages and pcchLanguagesBuffer returns the same in both C++ and C#. See more information about GetThreadPreferredUILanguages on http://msdn.microsoft.com/en-us/library/dd318128(v=vs.85).aspx[^] What is wrong? Regards Olof

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, 1. I'm not familiar with GetThreadPreferredUILanguages however I do know a thing or two about P/Invoke, and wrote a little article[^] on the subject. What you need here goes beyond the scope of my article though. 2. Your C# code is not doing what your C code does; in particular you should preallocate the result buffer, as you did with wchar_t buf[2323];. The native function you're calling expects a third parameter pwszLanguagesBuffer that is either a NULL, or a valid pointer to a buffer you provide for it to fill. 3. This seems like a reasonable approach: create a byte buffer, have it pinned (with GCHandle), call the native function, then use some kind of Marshal.PtrToString() for each string you need to extract from the buffer. Free the handle once all strings got extracted. 4. I would recommend you call GetThreadPreferredUILanguages twice, once without an output buffer (IntPtr.Zero), just to get the required buffer length; then the regular call with a sufficient buffer size. :)

      Luc Pattyn [My Articles] Nil Volentibus Arduum

      1 Reply Last reply
      0
      • M marca292

        Hi, I need to get all installed languages on the computer. "Control Panel / Regional and Language / Keyboards and Languages / Display language". How can I get the selected language in .NET? Our application has language settings for a lot of different languages. If our application don't has support for the selected language in "Display language" then we have to see if we has support for a fallback language. I tried to use "GetThreadPreferredUILanguages" call and it work fine when I use C++, but I can't get is working on .NET. Is there an .NET call I can use instead to get selected languages and also the fallback languages? C++ code

        wchar\_t buf\[2323\];
        
        wchar\_t \*pBuf;
        
        ULONG numLangs;
        
        ULONG size= 2323;
        
        
        
        GetThreadPreferredUILanguages(MUI\_MERGE\_USER\_FALLBACK, &numLangs, buf, &
        

        C# code

        [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]

            static extern System.Boolean GetThreadPreferredUILanguages(
        
                System.UInt32 dwFlags,
        
                ref System.UInt32 pulNumLanguages,
        
                **out** System.IntPtr pwszLanguagesBuffer,
        
                ref System.UInt32 pcchLanguagesBuffer
        
                );
        
        
        
        
        
              uint MUI\_MERGE\_USER\_FALLBACK = 0x20;
        
                uint numLang = 0;
        
                IntPtr pwszLangBuf = new IntPtr();
        
                uint langBuf = 2323;
        
                bool b = GetThreadPreferredUILanguages(MUI\_MERGE\_USER\_FALLBACK, ref numLang, out pwszLangBuf, ref langBuf);
        
        
        
                string tmp = Marshal.PtrToStringAuto(pwszLangBuf
        

        tmp string variable contains only a lot of chinese characters after Marshal.PtrToStringAuto(pwszLangBuf);. pulNumLanguages and pcchLanguagesBuffer returns the same in both C++ and C#. See more information about GetThreadPreferredUILanguages on http://msdn.microsoft.com/en-us/library/dd318128(v=vs.85).aspx[^] What is wrong? Regards Olof

        B Offline
        B Offline
        BobJanova
        wrote on last edited by
        #3

        Have a look at the documentation for CultureInfo[^] and related classes.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups