/// <summary> /// Register an inproc COM server, usually a .dll or .ocx /// </summary> /// <param name="path"></param> private void RegisterDllServer(string path) { IntPtr handle = new IntPtr(); // set the error mode to prevent failure message boxes from being displayed. uint oldErrorMode = SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS); handle = LoadLibrary(path); int error = Marshal.GetLastWin32Error(); if (handle.ToInt32() == 0 && error != 0) { throw new BuildException(string.Format(CultureInfo.InvariantCulture, "Error loading dll '{0}'.", path), Location); } string entryPoint = "DllRegisterServer"; string action = "register"; if (Unregister) { entryPoint = "DllUnregisterServer"; action = "unregister"; } IntPtr address = GetProcAddress(handle, entryPoint); error = Marshal.GetLastWin32Error(); if (address.ToInt32() == 0 && error != 0) { string message = string.Format(CultureInfo.InvariantCulture, "Error {0}ing dll. '{1}' has no {2} function.", action, path, entryPoint); FreeLibrary(handle); throw new BuildException(message, Location); } try { // Do the actual registration here DynamicPInvoke.DynamicDllFuncInvoke(path, entryPoint); } catch (TargetInvocationException ex) { throw new BuildException(string.Format(CultureInfo.InvariantCulture, "Error while {0}ing '{1}'", action, path), Location, ex.InnerException); } finally { if (handle.ToInt32() != 0) { // need to call FreeLibrary a second time as the DynPInvoke added an extra ref FreeLibrary(handle); } } // unload the library FreeLibrary(handle); SetErrorMode(oldErrorMode); }
/// <summary> /// Register an inproc COM server, usually a .dll or .ocx /// </summary> /// <param name="path"></param> void RegisterDllServer(string path) { IntPtr handle = new IntPtr(); handle = LoadLibrary(path); int error = Marshal.GetLastWin32Error(); if (handle.ToInt32() == 0 && error != 0) { throw new BuildException("Error loading dll : " + path, Location); } string entryPoint = "DllRegisterServer"; string action = "register"; if (Unregister) { entryPoint = "DllUnregisterServer"; action = "unregister"; } IntPtr address = GetProcAddress(handle, entryPoint); error = Marshal.GetLastWin32Error(); if (address.ToInt32() == 0 && error != 0) { string message = string.Format("Error {0}ing dll. {1} has no {2} function.", action, path, entryPoint); FreeLibrary(handle); throw new BuildException(message, Location); } // unload the library FreeLibrary(handle); error = Marshal.GetLastWin32Error(); try { // Do the actual registration here DynamicPInvoke.DynamicDllFuncInvoke(path, entryPoint); } catch (Exception e) { string message = string.Format("Error during registration: {0}; {1}", e.Message, e.InnerException.Message); throw new BuildException(message, Location); } }