Update native component

This commit is contained in:
Namhyeon Go 2023-12-17 00:11:42 +09:00
parent 9bc4621962
commit ba47a7f62d
2 changed files with 35 additions and 21 deletions

View File

@ -19,6 +19,8 @@
* *
*/ */
using System; using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
@ -28,6 +30,9 @@ namespace WelsonJS
{ {
private IntPtr hFile; private IntPtr hFile;
private IntPtr hFileMappingObject; private IntPtr hFileMappingObject;
private string _lpName;
public static Dictionary<string, NamedSharedMemory> Cached;
[Flags] [Flags]
public enum FileProtection : uint public enum FileProtection : uint
@ -91,8 +96,19 @@ namespace WelsonJS
public void Open(string lpName) public void Open(string lpName)
{ {
hFile = FileMappingNative.CreateFileMapping((IntPtr)(-1), IntPtr.Zero, FileProtection.PAGE_READWRITE, 0u, 1024u, lpName); _lpName = lpName;
hFileMappingObject = FileMappingNative.MapViewOfFile(hFile, FileMapAccess.FILE_MAP_ALL_ACCESS, 0u, 0u, 1024u);
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() public bool IsInitialized()
@ -131,6 +147,11 @@ namespace WelsonJS
FileMappingNative.CloseHandle(hFile); FileMappingNative.CloseHandle(hFile);
hFile = IntPtr.Zero; hFile = IntPtr.Zero;
} }
if (Cached.ContainsKey(_lpName))
{
Cached.Remove(_lpName);
}
} }
} }
} }

View File

@ -38,15 +38,8 @@ namespace WelsonJS
[ComVisible(true)] [ComVisible(true)]
public class Toolkit public class Toolkit
{ {
private Dictionary<string, NamedSharedMemory> sharedMemoryDict;
public static readonly string ApplicationName = "WelsonJS"; public static readonly string ApplicationName = "WelsonJS";
public Toolkit()
{
sharedMemoryDict = new Dictionary<string, NamedSharedMemory>();
}
[ComVisible(true)] [ComVisible(true)]
public bool SendClick(string title, int x, int y) public bool SendClick(string title, int x, int y)
{ {
@ -66,6 +59,7 @@ namespace WelsonJS
return SendKey(hWnd, key); return SendKey(hWnd, key);
} }
// [ComVisible(false)]
public bool SendKey(IntPtr hWnd, char key) public bool SendKey(IntPtr hWnd, char key)
{ {
return Window.PostMessage(hWnd, (int)Window.Message.WM_CHAR, key, IntPtr.Zero); 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 // [Toolkit] Access to a shared memory #96
[ComVisible(true)] // [ComVisible(false)]
public bool OpenNamedSharedMemory(string lpName)
{
NamedSharedMemory sharedMemory = new NamedSharedMemory(lpName);
sharedMemoryDict.Add(lpName, sharedMemory);
return sharedMemory.IsInitialized();
}
[ComVisible(true)]
public NamedSharedMemory GetSharedMemory(string lpName) 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()) if (sharedMemory.IsInitialized())
{ {
return sharedMemory; return sharedMemory;
@ -174,6 +160,14 @@ namespace WelsonJS
return null; return null;
} }
[ComVisible(true)]
public bool OpenNamedSharedMemory(string lpName)
{
NamedSharedMemory sharedMemory = new NamedSharedMemory(lpName);
return sharedMemory.IsInitialized();
}
[ComVisible(true)] [ComVisible(true)]
public void CloseNamedSharedMemory(string lpName) public void CloseNamedSharedMemory(string lpName)
{ {
@ -181,7 +175,6 @@ namespace WelsonJS
if (sharedMemory != null) if (sharedMemory != null)
{ {
sharedMemory.Close(); sharedMemory.Close();
sharedMemoryDict.Remove(lpName);
} }
} }