using System; using System.Collections.Generic; using System.IO; using System.Reflection; using System.Runtime.InteropServices; namespace Scorpiox.Ssms; /// /// P/Invoke bindings for every scorpiox-ssms-client export (29). /// /// THIS FILE IS THE REFERENCE for the future WinUI3 / SwiftUI-bridge app — /// copy it verbatim. It matches exports.json exactly (names, order). /// /// Rules baked in here (all load-bearing, see dll-console-test-repo-pattern): /// * Library name INCLUDES ".dll" — required for MSIX; on Linux the /// resolver below maps it onto libscorpiox-ssms-client.so. /// * CallingConvention.Cdecl — plain C library. /// * [MarshalAs(UnmanagedType.LPUTF8Str)] on EVERY const char* PARAM. /// Without it Windows marshals cp1252 and mangles non-ASCII before it /// ever reaches C (Linux never catches this). /// * IntPtr for EVERY const char* RETURN + Marshal.PtrToStringUTF8. /// Marshaling a char*-returning export directly to string aborts the /// process on the Linux .NET 9 runtime ("free(): invalid pointer"). /// * Strings returned by ssms_client_result/last_error/cell are STATIC /// scratch inside the DLL — valid only until the next call, NEVER free. /// * ssms_client_set_io takes a native scorpiox_io vtable pointer; the /// console harness never installs one — the DLL auto-loads its built-in /// backend (libcurl on Linux, WinHTTP on Windows). Passing IntPtr.Zero /// to set_io explicitly restores that default. A WinUI3 app can build /// the vtable with Marshal.GetFunctionPointerForDelegate + a native /// struct if it wants a custom transport; every delegate MUST carry /// [UnmanagedFunctionPointer(CallingConvention.Cdecl)] and every string /// param [MarshalAs(LPUTF8Str)] (CharSet.Ansi default = cp1252 on /// Windows). /// public static class SsmsClientInterop { public const string DllName = "scorpiox-ssms-client.dll"; static SsmsClientInterop() { try { NativeLibrary.SetDllImportResolver(typeof(SsmsClientInterop).Assembly, Resolve); } catch (InvalidOperationException) { /* resolver already registered */ } } static IntPtr Resolve(string libraryName, Assembly assembly, DllImportSearchPath? searchPath) { if (!string.Equals(libraryName, DllName, StringComparison.OrdinalIgnoreCase)) return IntPtr.Zero; string stem = libraryName.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) ? libraryName.Substring(0, libraryName.Length - 4) : libraryName; var candidates = new List(); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) candidates.Add(stem + ".dll"); else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) candidates.Add("lib" + stem + ".dylib"); else candidates.Add("lib" + stem + ".so"); candidates.Add(stem + ".dll"); candidates.Add("lib" + stem + ".so"); candidates.Add(stem); string? dir = null; try { dir = Path.GetDirectoryName(typeof(SsmsClientInterop).Assembly.Location); } catch { /* single-file publish */ } foreach (string candidate in candidates) { if (dir != null) { string full = Path.Combine(dir, candidate); if (File.Exists(full) && NativeLibrary.TryLoad(full, assembly, searchPath, out IntPtr h)) return h; } if (NativeLibrary.TryLoad(candidate, assembly, searchPath, out IntPtr h2)) return h2; } return IntPtr.Zero; } /* ── lifecycle ─────────────────────────────────────────────── */ [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_init")] public static extern int Init(); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_cleanup")] public static extern void Cleanup(); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_set_host")] public static extern void SetHost( [MarshalAs(UnmanagedType.LPUTF8Str)] string baseUrl); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_set_password")] public static extern void SetPassword( [MarshalAs(UnmanagedType.LPUTF8Str)] string pwd); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_set_jwt")] public static extern void SetJwt( [MarshalAs(UnmanagedType.LPUTF8Str)] string token); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_set_io")] public static extern void SetIo(IntPtr ioVtable); /* ── API calls ─────────────────────────────────────────────── */ [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_ping")] public static extern int Ping(); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_list_connections")] public static extern int ListConnections(); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_save_connection")] public static extern int SaveConnection( [MarshalAs(UnmanagedType.LPUTF8Str)] string profileJson); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_delete_connection")] public static extern int DeleteConnection( [MarshalAs(UnmanagedType.LPUTF8Str)] string id); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_test_connection")] public static extern int TestConnection( [MarshalAs(UnmanagedType.LPUTF8Str)] string profileJson); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_tree")] public static extern int Tree( [MarshalAs(UnmanagedType.LPUTF8Str)] string server, [MarshalAs(UnmanagedType.LPUTF8Str)] string nodeKind, [MarshalAs(UnmanagedType.LPUTF8Str)] string db, [MarshalAs(UnmanagedType.LPUTF8Str)] string schema, [MarshalAs(UnmanagedType.LPUTF8Str)] string obj); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_query")] public static extern int Query( [MarshalAs(UnmanagedType.LPUTF8Str)] string server, [MarshalAs(UnmanagedType.LPUTF8Str)] string db, [MarshalAs(UnmanagedType.LPUTF8Str)] string sql, int maxRows, int timeoutS, [MarshalAs(UnmanagedType.LPUTF8Str)] string queryToken); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_cancel")] public static extern int Cancel( [MarshalAs(UnmanagedType.LPUTF8Str)] string server, [MarshalAs(UnmanagedType.LPUTF8Str)] string queryToken); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_cancel_spid")] public static extern int CancelSpid( [MarshalAs(UnmanagedType.LPUTF8Str)] string server, int spid); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_editgrid_load")] public static extern int EditgridLoad( [MarshalAs(UnmanagedType.LPUTF8Str)] string server, [MarshalAs(UnmanagedType.LPUTF8Str)] string db, [MarshalAs(UnmanagedType.LPUTF8Str)] string schema, [MarshalAs(UnmanagedType.LPUTF8Str)] string table, int topRows); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_editgrid_apply")] public static extern int EditgridApply( [MarshalAs(UnmanagedType.LPUTF8Str)] string applyJson); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_script")] public static extern int Script( [MarshalAs(UnmanagedType.LPUTF8Str)] string server, [MarshalAs(UnmanagedType.LPUTF8Str)] string action, [MarshalAs(UnmanagedType.LPUTF8Str)] string kind, [MarshalAs(UnmanagedType.LPUTF8Str)] string db, [MarshalAs(UnmanagedType.LPUTF8Str)] string schema, [MarshalAs(UnmanagedType.LPUTF8Str)] string obj); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_activity")] public static extern int Activity( [MarshalAs(UnmanagedType.LPUTF8Str)] string server, [MarshalAs(UnmanagedType.LPUTF8Str)] string action); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_activity_kill")] public static extern int ActivityKill( [MarshalAs(UnmanagedType.LPUTF8Str)] string server, int spid); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_storage")] public static extern int Storage( [MarshalAs(UnmanagedType.LPUTF8Str)] string server, [MarshalAs(UnmanagedType.LPUTF8Str)] string action, [MarshalAs(UnmanagedType.LPUTF8Str)] string db); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_storage_post")] public static extern int StoragePost( [MarshalAs(UnmanagedType.LPUTF8Str)] string action, [MarshalAs(UnmanagedType.LPUTF8Str)] string jsonBody); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_admin")] public static extern int Admin( [MarshalAs(UnmanagedType.LPUTF8Str)] string server, [MarshalAs(UnmanagedType.LPUTF8Str)] string action, [MarshalAs(UnmanagedType.LPUTF8Str)] string db, [MarshalAs(UnmanagedType.LPUTF8Str)] string extra); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_job_control")] public static extern int JobControl( [MarshalAs(UnmanagedType.LPUTF8Str)] string server, [MarshalAs(UnmanagedType.LPUTF8Str)] string jobId, [MarshalAs(UnmanagedType.LPUTF8Str)] string verb); /* ── results (IntPtr + PtrToStringUTF8 — NEVER direct string) ── */ [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_result")] private static extern IntPtr ResultRawPtr(); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_last_error")] private static extern IntPtr LastErrorRawPtr(); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_cell")] private static extern IntPtr CellRawPtr(int row, int col); public static string Result => Marshal.PtrToStringUTF8(ResultRawPtr()) ?? ""; public static string LastError => Marshal.PtrToStringUTF8(LastErrorRawPtr()) ?? ""; public static string Cell(int row, int col) => Marshal.PtrToStringUTF8(CellRawPtr(row, col)) ?? ""; [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_row_count")] public static extern int RowCount(); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ssms_client_col_count")] public static extern int ColCount(); }