diff --git a/WelsonJS.Toolkit/WelsonJS.Toolkit/NamedSharedMemory.cs b/WelsonJS.Toolkit/WelsonJS.Toolkit/NamedSharedMemory.cs index 3695e5a..164a7d6 100644 --- a/WelsonJS.Toolkit/WelsonJS.Toolkit/NamedSharedMemory.cs +++ b/WelsonJS.Toolkit/WelsonJS.Toolkit/NamedSharedMemory.cs @@ -19,6 +19,8 @@ * */ using System; +using System.Collections.Generic; +using System.ComponentModel.Design; using System.Runtime.InteropServices; using System.Text; @@ -28,6 +30,9 @@ namespace WelsonJS { private IntPtr hFile; private IntPtr hFileMappingObject; + private string _lpName; + + public static Dictionary Cached; [Flags] public enum FileProtection : uint @@ -91,8 +96,19 @@ namespace WelsonJS public void Open(string 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); + _lpName = lpName; + + if (!Cached.ContainsKey(_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); + } + else + { + hFile = IntPtr.Zero; + hFileMappingObject = IntPtr.Zero; + } } public bool IsInitialized() @@ -131,6 +147,11 @@ namespace WelsonJS FileMappingNative.CloseHandle(hFile); hFile = IntPtr.Zero; } + + if (Cached.ContainsKey(_lpName)) + { + Cached.Remove(_lpName); + } } } } diff --git a/WelsonJS.Toolkit/WelsonJS.Toolkit/Toolkit.cs b/WelsonJS.Toolkit/WelsonJS.Toolkit/Toolkit.cs index 07e2ebf..6118735 100644 --- a/WelsonJS.Toolkit/WelsonJS.Toolkit/Toolkit.cs +++ b/WelsonJS.Toolkit/WelsonJS.Toolkit/Toolkit.cs @@ -38,15 +38,8 @@ namespace WelsonJS [ComVisible(true)] public class Toolkit { - private Dictionary sharedMemoryDict; - public static readonly string ApplicationName = "WelsonJS"; - public Toolkit() - { - sharedMemoryDict = new Dictionary(); - } - [ComVisible(true)] public bool SendClick(string title, int x, int y) { @@ -66,6 +59,7 @@ namespace WelsonJS return SendKey(hWnd, key); } + // [ComVisible(false)] public bool SendKey(IntPtr hWnd, char key) { return Window.PostMessage(hWnd, (int)Window.Message.WM_CHAR, key, IntPtr.Zero); @@ -151,20 +145,12 @@ namespace WelsonJS // [Toolkit] Access to a shared memory #96 - [ComVisible(true)] - public bool OpenNamedSharedMemory(string lpName) - { - NamedSharedMemory sharedMemory = new NamedSharedMemory(lpName); - sharedMemoryDict.Add(lpName, sharedMemory); - return sharedMemory.IsInitialized(); - } - - [ComVisible(true)] + // [ComVisible(false)] public NamedSharedMemory GetSharedMemory(string lpName) { - if (sharedMemoryDict.ContainsKey(lpName)) + if (NamedSharedMemory.Cached.ContainsKey(lpName)) { - NamedSharedMemory sharedMemory = sharedMemoryDict[lpName]; + NamedSharedMemory sharedMemory = NamedSharedMemory.Cached[lpName]; if (sharedMemory.IsInitialized()) { return sharedMemory; @@ -174,6 +160,14 @@ namespace WelsonJS return null; } + [ComVisible(true)] + public bool OpenNamedSharedMemory(string lpName) + { + NamedSharedMemory sharedMemory = new NamedSharedMemory(lpName); + return sharedMemory.IsInitialized(); + } + + [ComVisible(true)] public void CloseNamedSharedMemory(string lpName) { @@ -181,7 +175,6 @@ namespace WelsonJS if (sharedMemory != null) { sharedMemory.Close(); - sharedMemoryDict.Remove(lpName); } }