Please help me understand how to put a translated MUI string from RegLoadMUIString into a message box.

robert_e_bone
robert_e_bone used Ask the Experts™
on
Hello, in the following code, a RegLoadNUIString is performed for a registry value, and I would like to know which parameter holds the translated string returned, so that I can put that value into a message box for display purposes.

Here is the code - which works - I just don't know enough to figure out what holds the returned value from the function call to RegLoadMUIString - so that I can just for the moment throw its value into a message box.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace FormTest001
{
    public partial class Form1 : Form
    {
        [DllImport("advapi32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, EntryPoint = "RegOpenKeyExW", CallingConvention = CallingConvention.StdCall)]
        public static extern int RegOpenKeyEx(UIntPtr hKey, string lpSubKey, int ulOptions, int samDesired, out UIntPtr phkResult);

        [DllImport("advapi32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, EntryPoint = "RegLoadMUIStringW", CallingConvention = CallingConvention.StdCall)]
        internal static extern int RegLoadMUIString(UIntPtr hKey, string pszValue, StringBuilder pszOutBuf, int cbOutBuf, out int pcbData, uint Flags, string pszDirectory);

        [DllImport("advapi32.dll", ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        public static extern int RegCloseKey(UIntPtr hKey);


        public static UIntPtr HKEY_LOCAL_MACHINE = new UIntPtr(0x80000002u);
        public static UIntPtr HKEY_CURRENT_USER = new UIntPtr(0x80000001u);
        public const int READKEY = 131097;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            const int BUFFSIZE = 1024;
            UIntPtr key;
            StringBuilder buffer = new StringBuilder(1024);
            int pcbData;

            long result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, @"SYSTEM\CurrentControlSet\Control\Class\{36FC9E60-C465-11CF-8056-444553540000}", 0, READKEY, out key);
            result = RegLoadMUIString(key, "ClassDesc", buffer, BUFFSIZE, out pcbData, 0, null);
            RegCloseKey(key);

        }
    }
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Software Engineer
Top Expert 2009
Commented:
In your case, the variable buffer (pszOutBuf parameter) will contain the translated string.
Check:
http://msdn.microsoft.com/en-us/library/ms724890(VS.85).aspx

Author

Commented:
I put in the following code to display that value:

            MessageBox.Show("MUI Value = " + pszOutBuf, "Error",
            MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

And get the following error: The name 'pszOutBuf' does not exist in the current context.

Author

Commented:
OK - your comment indicated the field as it is listed in the parameters from the declaration for RegLoadMUIString, and for the code I provided, in the actual call down in my button click code, the corresponding field is called buffer.

It works like a champ with that adjustment - thanks!

Robert Bone

Author

Commented:
Thanks SOOOOO much - I am off to flesh this out into the real code needed, based on the solution provided.

A VERY happy newbie - Robert Bone

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial