[Toolkit] Access to a shared memory #96

This commit is contained in:
Namhyeon Go 2023-12-14 04:47:47 +09:00
parent 32020e0d84
commit 44fa579390
3 changed files with 200 additions and 7 deletions

View File

@ -0,0 +1,118 @@
// NamedSharedMemory.cs
// https://github.com/gnh1201/welsonjs
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace WelsonJS
{
public class NamedSharedMemory
{
private IntPtr hFile;
private IntPtr hFileMappingObject;
[Flags]
public enum FileProtection : uint
{
PAGE_NOACCESS = 1u,
PAGE_READONLY = 2u,
PAGE_READWRITE = 4u,
PAGE_WRITECOPY = 8u,
PAGE_EXECUTE = 0x10u,
PAGE_EXECUTE_READ = 0x20u,
PAGE_EXECUTE_READWRITE = 0x40u,
PAGE_EXECUTE_WRITECOPY = 0x80u,
PAGE_GUARD = 0x100u,
PAGE_NOCACHE = 0x200u,
PAGE_WRITECOMBINE = 0x400u,
SEC_FILE = 0x800000u,
SEC_IMAGE = 0x1000000u,
SEC_RESERVE = 0x4000000u,
SEC_COMMIT = 0x8000000u,
SEC_NOCACHE = 0x10000000u
}
[Flags]
public enum FileMapAccess
{
FILE_MAP_COPY = 1,
FILE_MAP_WRITE = 2,
FILE_MAP_READ = 4,
FILE_MAP_ALL_ACCESS = 0xF001F
}
public class FileMappingNative
{
public const int INVALID_HANDLE_VALUE = -1;
[DllImport("Kernel32.dll", SetLastError = true)]
public static extern IntPtr CreateFileMapping(IntPtr hFile, IntPtr lpAttributes, FileProtection flProtect, uint dwMaximumSizeHigh, uint dwMaximumSizeLow, string lpName);
[DllImport("Kernel32.dll", SetLastError = true)]
public static extern IntPtr MapViewOfFile(IntPtr hFileMappingObject, FileMapAccess dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, uint dwNumberOfBytesToMap);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr OpenFileMapping(FileMapAccess dwDesiredAccess, bool bInheritHandle, string lpName);
[DllImport("Kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool UnmapViewOfFile(IntPtr lpBaseAddress);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CloseHandle(IntPtr hHandle);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern uint GetLastError();
}
public NamedSharedMemory(string lpName)
{
Open(lpName);
}
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);
}
public bool IsInitialized()
{
return hFile != IntPtr.Zero;
}
public string ReadText()
{
return Marshal.PtrToStringAnsi(hFileMappingObject);
}
public void 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);
}
public void Clear(int size = 1024)
{
Marshal.Copy(new byte[size + 1], 0, hFileMappingObject, size);
}
public void Close()
{
if (hFileMappingObject != IntPtr.Zero)
{
FileMappingNative.UnmapViewOfFile(hFileMappingObject);
hFileMappingObject = IntPtr.Zero;
}
if (hFile != IntPtr.Zero)
{
FileMappingNative.CloseHandle(hFile);
hFile = IntPtr.Zero;
}
}
}
}

View File

@ -1,4 +1,6 @@
using System.Windows.Forms;
// Prompt.cs
// https://github.com/gnh1201/welsonjs
using System.Windows.Forms;
namespace WelsonJS
{

View File

@ -1,15 +1,18 @@
/*
* WelsonJS.Toolkit: WelsonJS C#.NET native component
* WelsonJS.Toolkit: WelsonJS dotnet native component
*
* description:
* WelsonJS - Build Windows desktop apps with JavaScript, HTML, and CSS based on WSH/HTA.
* https://github.com/gnh1201/welsonjs
* WelsonJS - Build a Windows app on the Windows built-in JavaScript engine
*
* website:
* - https://github.com/gnh1201/welsonjs
* - https://catswords.social/@catswords_oss
*
* author:
* Namhyeon Go <abuse@catswords.net>
*
* license:
* gnh1201/welsonjs is licensed under the Microsoft Reciprocal License (MS-RL)
* GPLv3 or MS-RL(Microsoft Reciprocal License)
*
* references:
* - https://stackoverflow.com/questions/9004352/call-a-function-in-a-console-app-from-vbscript
@ -23,6 +26,7 @@
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
@ -31,8 +35,9 @@ namespace WelsonJS
{
[ComVisible(true)]
public class Toolkit
{
private static string ApplicationName = "WelsonJS";
{
private Dictionary<string, NamedSharedMemory> sharedMemoryDict;
public static string ApplicationName = "WelsonJS";
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
@ -212,5 +217,73 @@ namespace WelsonJS
return false;
}
}
// [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)]
public NamedSharedMemory GetSharedMemory(string lpName)
{
if (sharedMemoryDict.ContainsKey(lpName))
{
NamedSharedMemory sharedMemory = sharedMemoryDict[lpName];
if (sharedMemory.IsInitialized())
{
return sharedMemory;
}
}
return null;
}
[ComVisible(true)]
public void CloseNamedSharedMemory(string lpName)
{
NamedSharedMemory sharedMemory = GetSharedMemory(lpName);
if (sharedMemory != null)
{
sharedMemory.Close();
sharedMemoryDict.Remove(lpName);
}
}
[ComVisible(true)]
public string ReadTextFromSharedMemory(string lpName)
{
NamedSharedMemory sharedMemory = GetSharedMemory(lpName);
if (sharedMemory != null)
{
return sharedMemory.ReadText();
}
return "";
}
[ComVisible(true)]
public void WriteTextToSharedMemory(string lpName, string text)
{
NamedSharedMemory sharedMemory = GetSharedMemory(lpName);
if (sharedMemory != null)
{
sharedMemory.WriteText(text);
}
}
[ComVisible(true)]
public void ClearSharedMemory(string lpName)
{
NamedSharedMemory sharedMemory = GetSharedMemory(lpName);
if (sharedMemory != null)
{
sharedMemory.Clear();
}
}
}
}