public static bool Register(string filterPath, bool register) { // Load the DLL. IntPtr hModuleDLL = LoadLibrary(filterPath); if (hModuleDLL == IntPtr.Zero) { ServiceRegistration.Get <ILogger>().Warn("COMRegistration: Could not find requested filter '{0}'", filterPath); return(false); } // Obtain the required exported API. IntPtr pExportedFunction = GetProcAddress(hModuleDLL, register ? "DllRegisterServer" : "DllUnregisterServer"); if (pExportedFunction == IntPtr.Zero) { ServiceRegistration.Get <ILogger>().Warn("COMRegistration: Unable to get required API from DLL '{0}'", filterPath); return(false); } // Obtain the delegate from the exported function, whether it be // DllRegisterServer() or DllUnregisterServer(). DllRegUnRegAPI pDelegateRegUnReg = (DllRegUnRegAPI)(Marshal.GetDelegateForFunctionPointer(pExportedFunction, typeof(DllRegUnRegAPI))); // Invoke the delegate. UInt32 hResult = pDelegateRegUnReg(); if (hResult == 0) { ServiceRegistration.Get <ILogger>().Info("COMRegistration: {0} of {1} successful.", (register ? "Registration" : "Unregistration"), filterPath); } else { ServiceRegistration.Get <ILogger>().Error("COMRegistration: {0} of {2} failed. Error: {1:X}", (register ? "Registration" : "Unregistration"), hResult, filterPath); } FreeLibrary(hModuleDLL); return(true); }
static void Main(string[] args) { // Boolean to determine if registration or unregistration // is required. bool bRegistration; // Check the arguments for correctness. // Note that if the path to the DLL contains spaces, // the path must be enclosed in quotations, e.g. : // "c:\my path\my dll.dll" if (CheckArguments(args, out bRegistration) == false) { return; } //Set the current path to the same path as the files we are loading //So we can get any dependency properly string fileParentPath = new DirectoryInfo(args[1]).Parent.FullName; try { Directory.SetCurrentDirectory(fileParentPath); } catch (Exception e) { Console.WriteLine(e.Message); return; } // Load the DLL. IntPtr hModuleDLL = LoadLibrary(args[1]); if (hModuleDLL == IntPtr.Zero) { Console.WriteLine("Unable to load DLL : {0:S}.", args[1]); DisplayUsage(); return; } // Obtain the required exported API. IntPtr pExportedFunction = IntPtr.Zero; if (bRegistration) { pExportedFunction = GetProcAddress(hModuleDLL, "DllRegisterServer"); } else { pExportedFunction = GetProcAddress(hModuleDLL, "DllUnregisterServer"); } if (pExportedFunction == IntPtr.Zero) { Console.WriteLine("Unable to get required API from DLL."); return; } // Obtain the delegate from the exported function, whether it be // DllRegisterServer() or DllUnregisterServer(). DllRegUnRegAPI pDelegateRegUnReg = (DllRegUnRegAPI)(Marshal.GetDelegateForFunctionPointer(pExportedFunction, typeof(DllRegUnRegAPI))) as DllRegUnRegAPI; // Invoke the delegate. UInt32 hResult = pDelegateRegUnReg(); if (hResult == 0) { if (bRegistration) { Console.WriteLine("Registration Successful."); } else { Console.WriteLine("Unregistration Successful."); } } else { Console.WriteLine("Error occurred : {0:X}.", hResult); } FreeLibrary(hModuleDLL); hModuleDLL = IntPtr.Zero; }
static void Main() { // string[] args // Boolean to determine if registration or unregistration // is required. bool bRegistration; string[] args = new string[2]; args[0] = "/r"; //args[1] = @"C:\Users\kryte\source\repos\WinFormsEmailNotifier\bin\Debug\WinFormsEmailNotifier.dll"; //args[1] = @"C:\temp\WinFormsEmailNotifier.dll"; //args[1] = @"C:\Users\kryte\source\repos\CSDeskBand\src\ExampleWinforms\bin\Debug\ExampleWinforms.dll"; args[1] = @"C:\Users\kryte\source\repos\CSDeskBand\src\CSDeskBand\bin\Debug\CSDeskBand.dll"; // Check the arguments for correctness. // Note that if the path to the DLL contains spaces, // the path must be enclosed in quotations, e.g. : // "c:\my path\my dll.dll" if (CheckArguments(args, out bRegistration) == false) { return; } // Load the DLL. IntPtr hModuleDLL = LoadLibrary(args[1]); if (hModuleDLL == IntPtr.Zero) { Console.WriteLine("Unable to load DLL : {0:S}.", args[1]); DisplayUsage(); return; } // Obtain the required exported API. IntPtr pExportedFunction = IntPtr.Zero; if (bRegistration) { pExportedFunction = GetProcAddress(hModuleDLL, "DllRegisterServer"); } else { pExportedFunction = GetProcAddress(hModuleDLL, "DllUnregisterServer"); } if (pExportedFunction == IntPtr.Zero) { Console.WriteLine("Unable to get required API from DLL."); return; } // Obtain the delegate from the exported function, whether it be // DllRegisterServer() or DllUnregisterServer(). DllRegUnRegAPI pDelegateRegUnReg = (DllRegUnRegAPI)(Marshal.GetDelegateForFunctionPointer(pExportedFunction, typeof(DllRegUnRegAPI))) as DllRegUnRegAPI; // Invoke the delegate. UInt32 hResult = pDelegateRegUnReg(); if (hResult == 0) { if (bRegistration) { Console.WriteLine("Registration Successful."); } else { Console.WriteLine("Unregistration Successful."); } } else { Console.WriteLine("Error occurred : {0:X}.", hResult); } FreeLibrary(hModuleDLL); hModuleDLL = IntPtr.Zero; }