shared memory #96

This commit is contained in:
Namhyeon Go 2024-01-04 15:23:26 +09:00
parent dddfa3cc47
commit 28fd89f22d
9 changed files with 139 additions and 23 deletions

Binary file not shown.

BIN
WelsonJS.Toolkit.pdb Normal file

Binary file not shown.

View File

@ -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
{

View File

@ -0,0 +1,69 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
namespace WelsonJS
{
public class ProcessTool
{
public static List<int> ProcessIDs = new List<int>();
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;
}
}
}
}

View File

@ -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(); };

View File

@ -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);
}
}
}

View File

@ -49,6 +49,7 @@
<ItemGroup>
<Compile Include="Compression\LZ77.cs" />
<Compile Include="NamedSharedMemory.cs" />
<Compile Include="ProcessTool.cs" />
<Compile Include="Prompt.cs" />
<Compile Include="Toolkit.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

View File

@ -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;

View File

@ -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);
}
}
}