diff --git a/WelsonJS.Toolkit.dll b/WelsonJS.Toolkit.dll index 66687d8..4d069c0 100644 Binary files a/WelsonJS.Toolkit.dll and b/WelsonJS.Toolkit.dll differ diff --git a/WelsonJS.Toolkit.pdb b/WelsonJS.Toolkit.pdb new file mode 100644 index 0000000..5b1b931 Binary files /dev/null and b/WelsonJS.Toolkit.pdb differ diff --git a/WelsonJS.Toolkit/WelsonJS.Toolkit/NamedSharedMemory.cs b/WelsonJS.Toolkit/WelsonJS.Toolkit/NamedSharedMemory.cs index 9ed39ca..8c78a49 100644 --- a/WelsonJS.Toolkit/WelsonJS.Toolkit/NamedSharedMemory.cs +++ b/WelsonJS.Toolkit/WelsonJS.Toolkit/NamedSharedMemory.cs @@ -123,13 +123,28 @@ namespace WelsonJS public string ReadText() { - return Marshal.PtrToStringAnsi(hFileMappingObject); + try + { + if (hFile == IntPtr.Zero || hFileMappingObject == IntPtr.Zero) + { + throw new Exception("Could not access the shared memory"); + } + return Marshal.PtrToStringAnsi(hFileMappingObject); + } + catch (Exception e) + { + return "Exception: " + e.Message; + } } public bool WriteText(string text, int size = 1024) { try { + if (hFile == IntPtr.Zero || hFileMappingObject == IntPtr.Zero) + { + throw new Exception("Could not access the shared memory"); + } byte[] bytes = Encoding.ASCII.GetBytes(text); byte[] array = new byte[size + 1]; Array.Copy(bytes, array, bytes.Length); @@ -161,17 +176,15 @@ namespace WelsonJS { try { - if (hFileMappingObject != IntPtr.Zero) + if (hFile == IntPtr.Zero || hFileMappingObject == IntPtr.Zero) { - FileMappingNative.UnmapViewOfFile(hFileMappingObject); - hFileMappingObject = IntPtr.Zero; + throw new Exception("Could not access the shared memory"); } - if (hFile != IntPtr.Zero) - { - FileMappingNative.CloseHandle(hFile); - hFile = IntPtr.Zero; - } + FileMappingNative.UnmapViewOfFile(hFileMappingObject); + hFileMappingObject = IntPtr.Zero; + FileMappingNative.CloseHandle(hFile); + hFile = IntPtr.Zero; } catch { diff --git a/WelsonJS.Toolkit/WelsonJS.Toolkit/ProcessTool.cs b/WelsonJS.Toolkit/WelsonJS.Toolkit/ProcessTool.cs new file mode 100644 index 0000000..9bb1ec0 --- /dev/null +++ b/WelsonJS.Toolkit/WelsonJS.Toolkit/ProcessTool.cs @@ -0,0 +1,69 @@ +using System.Collections.Generic; +using System.Diagnostics; +using System.Windows.Forms; + +namespace WelsonJS +{ + public class ProcessTool + { + public static List ProcessIDs = new List(); + + public static string OpenFileDialog() + { + string filepath = string.Empty; + + using (OpenFileDialog openFileDialog = new OpenFileDialog()) + { + openFileDialog.Filter = "All files (*.*)|*.*"; + openFileDialog.RestoreDirectory = true; + if (openFileDialog.ShowDialog() == DialogResult.OK) + { + filepath = openFileDialog.FileName; + } + } + + return filepath; + } + + public static int Open(string filepath) + { + if (string.IsNullOrEmpty(filepath)) + { + filepath = OpenFileDialog(); + if (string.IsNullOrEmpty(filepath)) + { + return -1; + } + } + + try + { + Process process = new Process(); + process.StartInfo.FileName = filepath; + process.Start(); + + int processId = process.Id; + ProcessIDs.Add(processId); + + return processId; + } + catch + { + return -1; + } + } + + public static bool Close(int processId) + { + try + { + Process.GetProcessById(processId).CloseMainWindow(); + return true; + } + catch + { + return false; + } + } + } +} diff --git a/WelsonJS.Toolkit/WelsonJS.Toolkit/Prompt.cs b/WelsonJS.Toolkit/WelsonJS.Toolkit/Prompt.cs index eef093b..240e82d 100644 --- a/WelsonJS.Toolkit/WelsonJS.Toolkit/Prompt.cs +++ b/WelsonJS.Toolkit/WelsonJS.Toolkit/Prompt.cs @@ -34,7 +34,7 @@ namespace WelsonJS Text = caption, StartPosition = FormStartPosition.CenterScreen }; - Label textLabel = new Label() { Left = 50, Top = 20, Text = text }; + Label textLabel = new Label() { Left = 50, Top = 20, Width = 400, Text = text }; TextBox textBox = new TextBox() { Left = 50, Top = 50, Width = 400 }; Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 70, DialogResult = DialogResult.OK }; confirmation.Click += (sender, e) => { prompt.Close(); }; diff --git a/WelsonJS.Toolkit/WelsonJS.Toolkit/Toolkit.cs b/WelsonJS.Toolkit/WelsonJS.Toolkit/Toolkit.cs index 7386947..618331b 100644 --- a/WelsonJS.Toolkit/WelsonJS.Toolkit/Toolkit.cs +++ b/WelsonJS.Toolkit/WelsonJS.Toolkit/Toolkit.cs @@ -190,15 +190,34 @@ namespace WelsonJS return false; } - public void CompressLZ77(string input) + [ComVisible(true)] + public string GetFilePathFromDialog() { - Compression.LZ77.Compress(input); + return ProcessTool.OpenFileDialog(); } [ComVisible(true)] - public string DecompressLZ77(string compressData) + public int OpenProcess(string filepath) { - return Compression.LZ77.Decompress(compressData); + return ProcessTool.Open(filepath); + } + + [ComVisible(true)] + public bool CloseProcess(int processID) + { + return ProcessTool.Close(processID); + } + + [ComVisible(true)] + public void CompressLZ77(string data) + { + Compression.LZ77.Compress(data); + } + + [ComVisible(true)] + public string DecompressLZ77(string compressedData) + { + return Compression.LZ77.Decompress(compressedData); } } } diff --git a/WelsonJS.Toolkit/WelsonJS.Toolkit/WelsonJS.Toolkit.csproj b/WelsonJS.Toolkit/WelsonJS.Toolkit/WelsonJS.Toolkit.csproj index 8e6ddcb..b871e23 100644 --- a/WelsonJS.Toolkit/WelsonJS.Toolkit/WelsonJS.Toolkit.csproj +++ b/WelsonJS.Toolkit/WelsonJS.Toolkit/WelsonJS.Toolkit.csproj @@ -49,6 +49,7 @@ + diff --git a/lib/toolkit.js b/lib/toolkit.js index 6fc3291..f565d18 100644 --- a/lib/toolkit.js +++ b/lib/toolkit.js @@ -78,10 +78,18 @@ function NamedSharedMemory(name) { this.clear = function() { return _interface.ClearSharedMemory(this.name); }; - - this.close = function() { - return _interface.CloseSharedMemory(this.name); - }; + + this.close = function() { + return _interface.CloseSharedMemory(this.name); + }; +} + +function openProcess(filepath) { + return getInterface().OpenProcess(filepath); +} + +function closeProcess(pid) { + return getInterface().CloseProcess(pid); } exports.create = create; @@ -93,8 +101,10 @@ exports.alert = alert; exports.confirm = confirm; exports.prompt = prompt; exports.NamedSharedMemory = NamedSharedMemory; +exports.openProcess = openProcess; +exports.closeProcess = closeProcess; -exports.VERSIONINFO = "WelsonJS.Toolkit Native API version 0.3.3"; +exports.VERSIONINFO = "WelsonJS.Toolkit Native API version 0.3.4"; exports.AUTHOR = "abuse@catswords.net"; exports.global = global; exports.require = global.require; diff --git a/testloader.js b/testloader.js index 2f46965..864d6ee 100644 --- a/testloader.js +++ b/testloader.js @@ -868,14 +868,18 @@ var test_implements = { if (!memName) { console.log("Aborted."); } else { + // Open the shared memory mem = new Toolkit.NamedSharedMemory(memName); + + // Open the second process will be communicate + Toolkit.openProcess(); + + // Listen the shared memory console.log("Listening the shared memory:", memName); while (true) { var message = mem.readText(memName); - if (!!message) { - console.log(memName + ": " + message); - } - sleep(1); + console.log(memName + ": ", message); + sleep(100); } } }