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?
NativeMethodsmember. Do you callNativeMethodsanywhere?constmember 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 theconstmember to be astaticmember will ensure that the static constructor is called, but then you'll find you have trouble using that member in theDllImportattribute.