diff --git a/WelsonJS.Toolkit.Experimental.dll b/WelsonJS.Toolkit.Experimental.dll deleted file mode 100644 index 6762992..0000000 Binary files a/WelsonJS.Toolkit.Experimental.dll and /dev/null differ diff --git a/WelsonJS.Toolkit.dll b/WelsonJS.Toolkit.dll index c62b666..4ea8d7b 100644 Binary files a/WelsonJS.Toolkit.dll and b/WelsonJS.Toolkit.dll differ diff --git a/WelsonJS.Toolkit/WelsonJS.Toolkit/NamedSharedMemory.cs b/WelsonJS.Toolkit/WelsonJS.Toolkit/NamedSharedMemory.cs index d267f28..c64f585 100644 --- a/WelsonJS.Toolkit/WelsonJS.Toolkit/NamedSharedMemory.cs +++ b/WelsonJS.Toolkit/WelsonJS.Toolkit/NamedSharedMemory.cs @@ -29,9 +29,8 @@ namespace WelsonJS { private IntPtr hFile; private IntPtr hFileMappingObject; - private string _lpName; - - public static Dictionary Cached; + private string lpName; + private static Dictionary memDict; [Flags] public enum FileProtection : uint @@ -63,7 +62,7 @@ namespace WelsonJS FILE_MAP_ALL_ACCESS = 0xF001F } - public class FileMappingNative + public static class FileMappingNative { public const int INVALID_HANDLE_VALUE = -1; @@ -90,24 +89,31 @@ namespace WelsonJS public NamedSharedMemory(string lpName) { - Open(lpName); + this.lpName = lpName; + Open(); } - public void Open(string lpName) + public bool Open() { - _lpName = lpName; + if (memDict.ContainsKey(lpName)) + { + hFile = memDict[lpName].hFile; + hFileMappingObject = memDict[lpName].hFileMappingObject; + return true; + } - if (!Cached.ContainsKey(_lpName)) + try { - hFile = FileMappingNative.CreateFileMapping((IntPtr)(-1), IntPtr.Zero, FileProtection.PAGE_READWRITE, 0u, 1024u, _lpName); + hFile = FileMappingNative.CreateFileMapping((IntPtr)(-1), IntPtr.Zero, FileProtection.PAGE_READWRITE, 0u, 1024u, lpName); hFileMappingObject = FileMappingNative.MapViewOfFile(hFile, FileMapAccess.FILE_MAP_ALL_ACCESS, 0u, 0u, 1024u); - Cached.Add(_lpName, this); + memDict.Add(lpName, this); } - else + catch { - hFile = IntPtr.Zero; - hFileMappingObject = IntPtr.Zero; + return false; } + + return IsInitialized(); } public bool IsInitialized() @@ -120,37 +126,59 @@ namespace WelsonJS return Marshal.PtrToStringAnsi(hFileMappingObject); } - public void WriteText(string text, int size = 1024) + public bool WriteText(string text, int size = 1024) { - byte[] bytes = Encoding.ASCII.GetBytes(text); - byte[] array = new byte[size + 1]; - Array.Copy(bytes, array, bytes.Length); - Marshal.Copy(array, 0, hFileMappingObject, size); + try + { + byte[] bytes = Encoding.ASCII.GetBytes(text); + byte[] array = new byte[size + 1]; + Array.Copy(bytes, array, bytes.Length); + Marshal.Copy(array, 0, hFileMappingObject, size); + } + catch + { + return false; + } + + return true; } - public void Clear(int size = 1024) + public bool Clear(int size = 1024) { - Marshal.Copy(new byte[size + 1], 0, hFileMappingObject, size); + try + { + Marshal.Copy(new byte[size + 1], 0, hFileMappingObject, size); + } + catch + { + return false; + } + + return true; } - public void Close() + public bool Close() { - if (hFileMappingObject != IntPtr.Zero) + try { - FileMappingNative.UnmapViewOfFile(hFileMappingObject); - hFileMappingObject = IntPtr.Zero; + if (hFileMappingObject != IntPtr.Zero) + { + FileMappingNative.UnmapViewOfFile(hFileMappingObject); + hFileMappingObject = IntPtr.Zero; + } + + if (hFile != IntPtr.Zero) + { + FileMappingNative.CloseHandle(hFile); + hFile = IntPtr.Zero; + } + } + catch + { + return false; } - if (hFile != IntPtr.Zero) - { - FileMappingNative.CloseHandle(hFile); - hFile = IntPtr.Zero; - } - - if (Cached.ContainsKey(_lpName)) - { - Cached.Remove(_lpName); - } + return true; } } } diff --git a/WelsonJS.Toolkit/WelsonJS.Toolkit/Toolkit.cs b/WelsonJS.Toolkit/WelsonJS.Toolkit/Toolkit.cs index ef0f03a..466dcde 100644 --- a/WelsonJS.Toolkit/WelsonJS.Toolkit/Toolkit.cs +++ b/WelsonJS.Toolkit/WelsonJS.Toolkit/Toolkit.cs @@ -144,73 +144,53 @@ namespace WelsonJS } // [Toolkit] Access to a shared memory #96 - - // [ComVisible(false)] - public NamedSharedMemory GetSharedMemory(string lpName) + [ComVisible(true)] + public bool WriteTextToSharedMemory(string lpName, string text) { - if (NamedSharedMemory.Cached.ContainsKey(lpName)) + NamedSharedMemory mem = new NamedSharedMemory(lpName); + if (mem.IsInitialized()) { - NamedSharedMemory sharedMemory = NamedSharedMemory.Cached[lpName]; - if (sharedMemory.IsInitialized()) - { - return sharedMemory; - } + return mem.WriteText(text); } - return null; - } - - [ComVisible(true)] - public bool OpenNamedSharedMemory(string lpName) - { - NamedSharedMemory sharedMemory = new NamedSharedMemory(lpName); - return sharedMemory.IsInitialized(); - } - - - [ComVisible(true)] - public void CloseNamedSharedMemory(string lpName) - { - NamedSharedMemory sharedMemory = GetSharedMemory(lpName); - if (sharedMemory != null) - { - sharedMemory.Close(); - } + return false; } [ComVisible(true)] public string ReadTextFromSharedMemory(string lpName) { - NamedSharedMemory sharedMemory = GetSharedMemory(lpName); - if (sharedMemory != null) - { - return sharedMemory.ReadText(); + NamedSharedMemory mem = new NamedSharedMemory(lpName); + if (mem.IsInitialized()) { + return mem.ReadText(); } return ""; } [ComVisible(true)] - public void WriteTextToSharedMemory(string lpName, string text) + public bool ClearSharedMemory(string lpName) { - NamedSharedMemory sharedMemory = GetSharedMemory(lpName); - if (sharedMemory != null) + NamedSharedMemory mem = new NamedSharedMemory(lpName); + if (mem.IsInitialized()) { - sharedMemory.WriteText(text); + return mem.Clear(); } + + return false; } [ComVisible(true)] - public void ClearSharedMemory(string lpName) + public bool CloseSharedMemory(string lpName) { - NamedSharedMemory sharedMemory = GetSharedMemory(lpName); - if (sharedMemory != null) + NamedSharedMemory mem = new NamedSharedMemory(lpName); + if (mem.IsInitialized()) { - sharedMemory.Clear(); + return mem.Close(); } + + return false; } - [ComVisible(true)] public void CompressLZ77(string input) { Compression.LZ77.Compress(input); diff --git a/lib/toolkit.js b/lib/toolkit.js index 7e42fbb..6fc3291 100644 --- a/lib/toolkit.js +++ b/lib/toolkit.js @@ -63,26 +63,25 @@ function prompt(message, _default) { // [Toolkit] Access to a shared memory #96 function NamedSharedMemory(name) { - var Toolkit = create().getInterface(); + var _interface = create().getInterface(); this.name = name; - this.isInitialized = false; - this.open = function() { - this.isInitialized = Toolkit.OpenNamedSharedMemory(this.name); - }; - - this.readText = function() { - return Toolkit.ReadTextFromSharedMemory(this.name); - }; - this.writeText = function(text) { - Toolkit.WriteTextToSharedMemory(this.name, text); + return _interface.WriteTextToSharedMemory(this.name, text); }; - this.clear = function() { - Toolkit.ClearSharedMemory(this.name); + this.readText = function() { + return _interface.ReadTextFromSharedMemory(this.name); }; + + this.clear = function() { + return _interface.ClearSharedMemory(this.name); + }; + + this.close = function() { + return _interface.CloseSharedMemory(this.name); + }; } exports.create = create; @@ -95,7 +94,7 @@ exports.confirm = confirm; exports.prompt = prompt; exports.NamedSharedMemory = NamedSharedMemory; -exports.VERSIONINFO = "WelsonJS.Toolkit Native API version 0.3.2"; +exports.VERSIONINFO = "WelsonJS.Toolkit Native API version 0.3.3"; exports.AUTHOR = "abuse@catswords.net"; exports.global = global; exports.require = global.require;