From 6d19e7e49bf46458e6fb30e535188735d37f01a6 Mon Sep 17 00:00:00 2001 From: Mikhail Shahovalov Date: Fri, 7 Nov 2025 01:01:31 +0300 Subject: [PATCH] base implementation --- QuickPrintBridge/QuickPrintBridge.cs | 62 ++++++++++++++++++++++++ QuickPrintBridge/QuickPrintBridge.csproj | 2 +- 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 QuickPrintBridge/QuickPrintBridge.cs diff --git a/QuickPrintBridge/QuickPrintBridge.cs b/QuickPrintBridge/QuickPrintBridge.cs new file mode 100644 index 0000000..bf2d88b --- /dev/null +++ b/QuickPrintBridge/QuickPrintBridge.cs @@ -0,0 +1,62 @@ +using System.Runtime.InteropServices; +using Kompas6API5; +using Microsoft.Win32; + +namespace QuickPrintBridge; + +[ClassInterface(ClassInterfaceType.AutoDual)] +public sealed class QuickPrintBridge +{ + [return: MarshalAs(UnmanagedType.BStr)] + public string GetLibraryName() => nameof(QuickPrintBridge); + + // Головная функция библиотеки + public void ExternalRunCommand([In] short command, [In] short mode, + [In, MarshalAs(UnmanagedType.IDispatch)] + object kompas_) + { + KompasObject kompas = (KompasObject)kompas_; + kompas.ksMessage("Привет!"); + } + + public short GetProtectNumber() + { + return 111; + } + + // Эта функция выполняется при регистрации класса для COM + // Она добавляет в ветку реестра компонента раздел Kompas_Library, + // который сигнализирует о том, что класс является приложением Компас, + // а также заменяет имя InprocServer32 на полное, с указанием пути. + // Все это делается для того, чтобы иметь возможность подключить + // библиотеку на вкладке ActiveX. + [ComRegisterFunction] + public static void RegisterKompasLib(Type t) + { + try + { + RegistryKey? regKey = Registry.LocalMachine; + string keyName = @"SOFTWARE\Classes\CLSID\{" + t.GUID.ToString() + "}"; + regKey = regKey.OpenSubKey(keyName, true); + regKey.CreateSubKey("Kompas_Library"); + regKey = regKey.OpenSubKey("InprocServer32", true); + regKey.SetValue(null, System.Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\mscoree.dll"); + regKey.Close(); + } + catch (Exception ex) + { + //MessageBox.Show(string.Format("При регистрации класса для COM-Interop произошла ошибка:\n{0}", ex)); + } + } + + // Эта функция удаляет раздел Kompas_Library из реестра + [ComUnregisterFunction] + public static void UnregisterKompasLib(Type t) + { + RegistryKey regKey = Registry.LocalMachine; + string keyName = @"SOFTWARE\Classes\CLSID\{" + t.GUID.ToString() + "}"; + RegistryKey subKey = regKey.OpenSubKey(keyName, true); + subKey.DeleteSubKey("Kompas_Library"); + subKey.Close(); + } +} \ No newline at end of file diff --git a/QuickPrintBridge/QuickPrintBridge.csproj b/QuickPrintBridge/QuickPrintBridge.csproj index d89542f..3a6f928 100644 --- a/QuickPrintBridge/QuickPrintBridge.csproj +++ b/QuickPrintBridge/QuickPrintBridge.csproj @@ -1,7 +1,7 @@  - net8.0 + net8.0-windows enable enable true