-5

I am having trouble implementing an answer I got here!, Can anyone help me access this private static class?

namespace VEParameterTool
{
    class ProcessorPlugIn
    {
        private static class UnsafeNativeMethods
        {
            [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Ansi)]
            static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)]string lpFileName);

            [DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
            static extern IntPtr GetProcAddress(IntPtr hModule, string procName);

            [DllImport("kernel32", SetLastError = true)]
            static extern bool FreeLibrary(IntPtr hModule);
        }      

        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        private delegate ErrorCode ProcessorFunction(ref IntPtr pRx);

        IntPtr hLib = UnsafeNativeMethods.LoadLibrary("Processor.dll");
    }
}

When I try to LoadLibrary, I get the error about protection levels:

'VEParameterTool.ProcessorPlugIn.UnsafeNativeMethods.LoadLibrary(string)' is inaccessible due to its protection level

I have searched for the solution but cant see anything to do with static classes.

Any hep would be greatly appreciated.

Andy

Andy
  • 259
  • 1
  • 4
  • 16
  • 7
    declaring your classes/methods as public may help. – L.B Nov 11 '14 at 22:34
  • `Andy` are you familiar with Access levels of Classes for example `Private, Public, Protected Sealed...etc` did you read the Encapsulation section in C# as well as Access Modifier Levels..? – MethodMan Nov 11 '14 at 22:37
  • 1
    no, now for real... are you trying to import a dll via COM and use it as a static class? umm.... naugh. –  Nov 11 '14 at 22:38
  • Why are you downvoting my legitimate question that was answered, i am dynamically loading a dll and now its working great thats to the perfect answer below. You didn't understand the question, which i also said was from a previous answer before you start insulting it. The answer was that there was missing public keyword inside the private static class. – Andy Nov 13 '14 at 00:56
  • @Andy it does sometimes seem the community is quick on the downvote but not so much on the upvotes. However the tooltip of the downvote button does list "lack of any research" - a quick google search on your error message would have (seemingly) shed light on your issue. Additionally a link to the original question may have been a good idea. – Kritner Nov 13 '14 at 13:22

2 Answers2

3

Why are you attempting to declare a private static class? The only method I can think of potentially being able to access information within the class is if you were to make it a nested private static class with public methods - and based on the code posted that is not the case.

Private means just that - it cannot be accessed from outside. Read more about access modifiers here: http://msdn.microsoft.com/en-us/library/ms173121.aspx

See here for an example of a private static class implementation:

https://dotnetfiddle.net/Lyjlbr

using System;

public class Program
{
    public static void Main()
    {
        Bar.DoStuff();

        // Bar.DoOtherStuff(); // Cannot be done due to protection level

        Bar.DoStuffAndOtherStuff(); // note that this public static method calls a private static method from the inner class
    }

    private static class Bar
    {
        public static void DoStuff()
        {
            Console.WriteLine("Test");
        }

        public static void DoStuffAndOtherStuff()
        {
            DoStuff();
            DoOtherStuff();
        }

        private static void DoOtherStuff()
        {
            Console.WriteLine("other stuff");
        }
    }
}

EDIT: apparently you can also use reflection to access private classes/members/functions, but I don't know much about it. See here: Why can reflection access protected/private member of class in C#?

Community
  • 1
  • 1
Kritner
  • 13,557
  • 10
  • 46
  • 72
2

You can leave the inner class private, making it only accessible to the ProcessorPlugIn class, but you have to make the methods public.

private static class UnsafeNativeMethods
{
    [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Ansi)]
    public static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)]string lpFileName);

    [DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
    public static extern IntPtr GetProcAddress(IntPtr hModule, string procName);

    [DllImport("kernel32", SetLastError = true)]
    public static extern bool FreeLibrary(IntPtr hModule);
}

These methods will be only accessible from where their containing class can be accessed, in this example, ProcessorPlugIn.

Steve Czetty
  • 6,147
  • 9
  • 39
  • 48