Search code examples
c#dllimportinteropservices

C# DllImport not found in context, but Interpor Services are bound in


I have a problem with my compiler not being able to import kernel32.dll, althrough I'm using System.Runtime.InteropServices. Here is the Code:

    using System;
    ...
    using System.Runtime.InteropServices;

    namespace server
    {
        class Debugconsole
        {
            public void Initialise()
            {
                [DllImport("kernel32.dll")]
                ...
            }
        }
    }

It throws a whole bunch of syntaxerrors and "Can't find "DllImport" in current context."

Thanks for your help.


Solution

  • Attributes cannot be used inside a method.
    You should move it out of your method:

    class Debugconsole
    {
        [DllImport("kernel32.dll")]
        ... the static extern method declaration ...
    
        public void Initialise()
        {
            ...
        }
    }