1

I have the following class:

 private static class NativeSomeWrapper
   {
       [DllImport(NativeMethods.myCeeLib, EntryPoint = "Get_300_bars",
           CallingConvention = CallingConvention.Cdecl)]
       [return: MarshalAs(UnmanagedType.SysInt)]
       internal static extern IntPtr Get300bars([MarshalAs(UnmanagedType.SysInt)] IntPtr assessment);
   }

And i have a static class that has responsibility to initilize the dll:

internal static class NativeMethods
{
    public const string myCeeLib= "myCeeLib.dll";


    static NativeMethods()
    {
        var path = GetPathToMyCeeLibFile();
        var err = SetPath(path);
        if (err != ErrorCode.Ok)
        {
            throw new FileNotFoundException("Coulnt find myCeeLib file.");
        }
    }
}

The problem is that NativeMethods constructor is not getting called as expected.

How can i make sure that NativeMethods static constructor is called and the path to library file is set correctly?

Cœur
  • 37,241
  • 25
  • 195
  • 267
persianLife
  • 1,206
  • 3
  • 17
  • 22
  • 2
    It will be called as soon as you access an `NativeMethods` member. Do you call `NativeMethods` anywhere? – Dmitry Egorov Jul 18 '16 at 21:14
  • @DmitryEgorov In the flag where I call `[DllImport(NativeMethods.myCeeLib...]`. does that count? – persianLife Jul 18 '16 at 21:15
  • 1
    Do you access the NativeMethods class someway? Check this: http://stackoverflow.com/questions/1437352/when-is-a-static-constructor-called-in-c – Alberto Chiesa Jul 18 '16 at 21:15
  • 3
    @Quantic That's a static constructor, as the question says. It's not just a method. – Servy Jul 18 '16 at 21:16
  • @servy Thanks, I deleted my comment, oops. – Quantic Jul 18 '16 at 21:18
  • @A.Chiesa I access the `NativeMethods` within the flag: `[DllImport(NativeMethods.myCeeLib...]`. I expected that this would access it before the function call. – persianLife Jul 18 '16 at 21:20
  • 2
    As noted in the marked duplicate, static constructor is called only when you first create an instance of the class or access any static member. A `const` member is a not a static member, and so doesn't cause the static constructor to be called. And of course, from this you can easily infer that changing the `const` member to be a `static` member _will_ ensure that the static constructor is called, but then you'll find you have trouble using that member in the `DllImport` attribute. – Peter Duniho Jul 18 '16 at 21:36
  • All that said, it seems to me all this belongs in the `NativeSomeWrapper` class with the `DllImport`, and if there then that would address the entire issue (not counting your reliance on the current working directly which is in and of itself a code maintenance problem, but that's a whole 'nother ball o' wax). – Peter Duniho Jul 18 '16 at 21:36
  • @PeterDuniho Please don't assume so much and don't close a question before trying to understanding it, – persianLife Jul 18 '16 at 21:40
  • See also http://stackoverflow.com/questions/9398509/how-does-a-static-constructor-work and https://stackoverflow.com/questions/6907959/how-to-trigger-a-static-constructor. I do understand your question, and the answer to your question is found in the marked duplicate. All you have to do is read the documentation, but if that doesn't work, you can also read the many similar questions on Stack Overflow, including the marked duplicate. There is nothing new at all about your question here, nor the answers you've gotten. – Peter Duniho Jul 18 '16 at 21:42
  • 2
    I agree, but the issue is not about static constructors. The actual question is if there is a way to check whether a DllImport file exists or perhaps even how to set the path... and that is where I agree, should be a new question. – Michael Jul 18 '16 at 21:46
  • Not sure it helps, but check this out: [DefaultDllImportSearchPaths](https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.defaultdllimportsearchpathsattribute.defaultdllimportsearchpathsattribute(v=vs.110).aspx) – Michael Jul 18 '16 at 21:47
  • @PeterDuniho Linking to random links won't justify it. The problem is that while accessing the static class with flag: `[DllImport(NativeMethods.myCeeLib...]` won't call the static constructor. The links you provide give an explanation of when a static constructor is called and NOT a solution to this problem. – RayOldProf Jul 19 '16 at 11:27

1 Answers1

3

The compiler is going to compute the value of that attribute at compile time, so the static constructor isn't running when you execute your program because by the time the application starts the value has already been computed, and you don't need to access the class to get it (thus triggering the static constructor).

Servy
  • 202,030
  • 26
  • 332
  • 449