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

    public Form1()
Who is Participating?
Miguel OzSoftware EngineerCommented:
In your case, you need to type at the start of your cs file:
using System.Runtime.InteropServices;

Note: To add an assembly reference:
-right click your project.
- In project context menu, click add reference...
- On the list search for the appropiate reference and click OK
robert_e_boneAuthor Commented:
Thanks - I'll give it a try right away and follow up with this thread.
robert_e_boneAuthor Commented:
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Miguel OzSoftware EngineerCommented:
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)
robert_e_boneAuthor Commented:
uh oh!

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

Bob Bone
robert_e_boneAuthor Commented:
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
robert_e_boneAuthor Commented:
WAHOO!!!!  Thanks for the speedy response AND your patience with my being such a newbie at this.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.