Example #1
0
        private static void LoadModule()
        {
            if (_moduleHandle == IntPtr.Zero)
            {
                // Load the Scintilla module into memory
                if ((_moduleHandle = NativeMethods.LoadLibrary(_moduleName)) == IntPtr.Zero)
                {
                    string message = string.Format(Resources.Culture, Resources.Exception_CannotLoadModule, _moduleName);
                    throw new Win32Exception(message, new Win32Exception(Marshal.GetLastWin32Error()));
                }

                // Get the direct function. We use GetProcAddress instead of DllImport
                // because we don't know the name of the module ahead of time.
                _directFunction = Marshal.GetDelegateForFunctionPointer(
                    NativeMethods.GetProcAddress(_moduleHandle, "Scintilla_DirectFunction"),
                    typeof(NativeMethods.Scintilla_DirectFunction)) as NativeMethods.Scintilla_DirectFunction;

                if (_directFunction == null)
                {
                    string message = string.Format(Resources.Culture, Resources.Exception_InvalidModule, _moduleName);
                    throw new Win32Exception(message, new Win32Exception(Marshal.GetLastWin32Error()));
                }
            }
        }
Example #2
0
        private static void LoadModule()
        {
            if (_moduleHandle == IntPtr.Zero)
            {
                string fileName = _moduleName;

                /*
                // This extra bit of path resolution is only necessary at design-time because the SciLexer* DLLs and
                // the ScintillaNET assembly are not always in the same folder (like they are at runtime).
                ITypeResolutionService typeResolutionService = GetService(typeof(ITypeResolutionService)) as ITypeResolutionService;
                if (typeResolutionService != null)
                {
                    DirectoryInfo di = new DirectoryInfo(typeResolutionService.GetPathOfAssembly(base.GetType().Assembly.GetName()));

                    do
                    {
                        if (File.Exists(di.FullName + _moduleName))
                        {
                            fileName = di.FullName + _moduleName;
                            break;
                        }

                        di = di.Parent;
                    } while (di.Name != "ScintillaNET");

                    if (fileName == _moduleName)
                    {
                        // Check one last place--the libraries folder
                        if (File.Exists(di.FullName + "..\\Libraries\\" + _moduleName))
                            fileName = di.FullName + "..\\Libraries\\" + _moduleName;
                    }
                }
                */

                // Load the native library
                _moduleHandle = UnsafeNativeMethods.LoadLibrary(fileName);
                if (_moduleHandle == IntPtr.Zero)
                {
                    string message = string.Format(CultureInfo.InvariantCulture, Resources.Exception_CannotLoadModule, fileName);
                    throw new Win32Exception(message, new Win32Exception(Marshal.GetLastWin32Error()));
                }
            }

            if (_sciFunction == null)
            {
                // Get the native Scintilla direct function--the only function the library exports
                IntPtr sciFunctionPointer = UnsafeNativeMethods.GetProcAddress(new HandleRef(null, _moduleHandle), "Scintilla_DirectFunction");
                if (sciFunctionPointer == IntPtr.Zero)
                    throw new Win32Exception(Resources.Exception_CannotCreateDirectFunction, new Win32Exception(Marshal.GetLastWin32Error()));

                _sciFunction = (NativeMethods.Scintilla_DirectFunction)Marshal.GetDelegateForFunctionPointer(
                    sciFunctionPointer,
                    typeof(NativeMethods.Scintilla_DirectFunction));
            }
        }