Link to home
Start Free TrialLog in
Avatar of robert_e_bone
robert_e_bone

asked on

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

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);

        }
    }
}
ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of robert_e_bone
robert_e_bone

ASKER

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.
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
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