mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-05-09 13:16:05 +00:00
shared memory #96
This commit is contained in:
parent
dddfa3cc47
commit
28fd89f22d
Binary file not shown.
BIN
WelsonJS.Toolkit.pdb
Normal file
BIN
WelsonJS.Toolkit.pdb
Normal file
Binary file not shown.
|
@ -123,13 +123,28 @@ namespace WelsonJS
|
||||||
|
|
||||||
public string ReadText()
|
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)
|
public bool WriteText(string text, int size = 1024)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
if (hFile == IntPtr.Zero || hFileMappingObject == IntPtr.Zero)
|
||||||
|
{
|
||||||
|
throw new Exception("Could not access the shared memory");
|
||||||
|
}
|
||||||
byte[] bytes = Encoding.ASCII.GetBytes(text);
|
byte[] bytes = Encoding.ASCII.GetBytes(text);
|
||||||
byte[] array = new byte[size + 1];
|
byte[] array = new byte[size + 1];
|
||||||
Array.Copy(bytes, array, bytes.Length);
|
Array.Copy(bytes, array, bytes.Length);
|
||||||
|
@ -161,17 +176,15 @@ namespace WelsonJS
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (hFileMappingObject != IntPtr.Zero)
|
if (hFile == IntPtr.Zero || hFileMappingObject == IntPtr.Zero)
|
||||||
{
|
{
|
||||||
FileMappingNative.UnmapViewOfFile(hFileMappingObject);
|
throw new Exception("Could not access the shared memory");
|
||||||
hFileMappingObject = IntPtr.Zero;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hFile != IntPtr.Zero)
|
FileMappingNative.UnmapViewOfFile(hFileMappingObject);
|
||||||
{
|
hFileMappingObject = IntPtr.Zero;
|
||||||
FileMappingNative.CloseHandle(hFile);
|
FileMappingNative.CloseHandle(hFile);
|
||||||
hFile = IntPtr.Zero;
|
hFile = IntPtr.Zero;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
|
|
69
WelsonJS.Toolkit/WelsonJS.Toolkit/ProcessTool.cs
Normal file
69
WelsonJS.Toolkit/WelsonJS.Toolkit/ProcessTool.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -34,7 +34,7 @@ namespace WelsonJS
|
||||||
Text = caption,
|
Text = caption,
|
||||||
StartPosition = FormStartPosition.CenterScreen
|
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 };
|
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 };
|
Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 70, DialogResult = DialogResult.OK };
|
||||||
confirmation.Click += (sender, e) => { prompt.Close(); };
|
confirmation.Click += (sender, e) => { prompt.Close(); };
|
||||||
|
|
|
@ -190,15 +190,34 @@ namespace WelsonJS
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void CompressLZ77(string input)
|
[ComVisible(true)]
|
||||||
|
public string GetFilePathFromDialog()
|
||||||
{
|
{
|
||||||
Compression.LZ77.Compress(input);
|
return ProcessTool.OpenFileDialog();
|
||||||
}
|
}
|
||||||
|
|
||||||
[ComVisible(true)]
|
[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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,6 +49,7 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Compression\LZ77.cs" />
|
<Compile Include="Compression\LZ77.cs" />
|
||||||
<Compile Include="NamedSharedMemory.cs" />
|
<Compile Include="NamedSharedMemory.cs" />
|
||||||
|
<Compile Include="ProcessTool.cs" />
|
||||||
<Compile Include="Prompt.cs" />
|
<Compile Include="Prompt.cs" />
|
||||||
<Compile Include="Toolkit.cs" />
|
<Compile Include="Toolkit.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
|
|
@ -79,9 +79,17 @@ function NamedSharedMemory(name) {
|
||||||
return _interface.ClearSharedMemory(this.name);
|
return _interface.ClearSharedMemory(this.name);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.close = function() {
|
this.close = function() {
|
||||||
return _interface.CloseSharedMemory(this.name);
|
return _interface.CloseSharedMemory(this.name);
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function openProcess(filepath) {
|
||||||
|
return getInterface().OpenProcess(filepath);
|
||||||
|
}
|
||||||
|
|
||||||
|
function closeProcess(pid) {
|
||||||
|
return getInterface().CloseProcess(pid);
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.create = create;
|
exports.create = create;
|
||||||
|
@ -93,8 +101,10 @@ exports.alert = alert;
|
||||||
exports.confirm = confirm;
|
exports.confirm = confirm;
|
||||||
exports.prompt = prompt;
|
exports.prompt = prompt;
|
||||||
exports.NamedSharedMemory = NamedSharedMemory;
|
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.AUTHOR = "abuse@catswords.net";
|
||||||
exports.global = global;
|
exports.global = global;
|
||||||
exports.require = global.require;
|
exports.require = global.require;
|
||||||
|
|
|
@ -868,14 +868,18 @@ var test_implements = {
|
||||||
if (!memName) {
|
if (!memName) {
|
||||||
console.log("Aborted.");
|
console.log("Aborted.");
|
||||||
} else {
|
} else {
|
||||||
|
// Open the shared memory
|
||||||
mem = new Toolkit.NamedSharedMemory(memName);
|
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);
|
console.log("Listening the shared memory:", memName);
|
||||||
while (true) {
|
while (true) {
|
||||||
var message = mem.readText(memName);
|
var message = mem.readText(memName);
|
||||||
if (!!message) {
|
console.log(memName + ": ", message);
|
||||||
console.log(memName + ": " + message);
|
sleep(100);
|
||||||
}
|
|
||||||
sleep(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user