Link to home
Start Free TrialLog in
Avatar of robert_e_bone
robert_e_bone

asked on

Newbie - I got a solution that was missing an explanation on how to include a reference - please help me

I got the following solution code, and the provider did not explain how I needed to go about properly adding a namespace (reference?  Using?  something else?).  The provider's last comment is immediately below, followed by the code solution he provided.  I get 40-50 errors when I try to add a using statement for System.Runtime.InteropServices namespace, and my original question was closed after I got a nastygram from the moderator on my still having my question open.  (Sorry for all the background there) - can SOMEONE please detail what I need to do to get the solution code below to work properly in C#?

(provider's last comment) Partly my fault--I forgot to tell you to include the System.Runtime.InteropServices namespace. This is needed for the DllImport statements  :)

(following is provided solution code that I cannot get working)
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;

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

    public Form1()
    {
        InitializeComponent();
    }
}
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

Thanks - I'll give it a try right away and follow up with this thread.
Well - I don't quite know what I am doing wrong, but it still fails.

Your comment indicated to add 'using System.Runtime.InteropServices' in the beginning of my 'cs' file.

Where do I find that file?  I added it to the top end of my Form1.h file and got all kinds of errors.

Please forgive my newness to all of this and give me a little more of a rundown on where this needs to go.

Thanks - REALLY!!!

Bob Bone
Bob:
Your question is a C# question. Thus I am assuming that your form is defined at Form1.cs file.
Find the file that contains "public partial class Form1 : Form" that is your cs file.
and at the top add the reference as:
using System.Runtime.Interop;
Note: C# projects do not have Form1.h (This is for C++ projects to contain header info)
uh oh!

I'll have that cup of coffee I was debating and respond shortly.

Bob Bone
Wow!  I'm quite the slow one today.

Thank you SOOOOOO much.  I do believe you have just cemented my retaining my monthly membership in the Experts Exchange.

I am happily off to go rework the example into my real code.

You are all collectively my heroes today.

Bob Bone
WAHOO!!!!  Thanks for the speedy response AND your patience with my being such a newbie at this.