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();
    }
}
robert_e_boneAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
robert_e_boneAuthor Commented:
Thanks - I'll give it a try right away and follow up with this thread.
0
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
0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

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

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

Bob Bone
0
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
0
robert_e_boneAuthor Commented:
WAHOO!!!!  Thanks for the speedy response AND your patience with my being such a newbie at this.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.