Electron调用.Net的Dll以及将.Net程序作为子进程运行
调用.Net Dll
const edge = require('electron-edge-js');
var testDll = edge.func({
assemblyFile: "./dll/electronedge.dll",
typeName: "electronedge.MQ",
methodName: "Invoke"
});
window.setInterval(heartbeat, 100);
var indexOption=0;
function heartbeat(){
testDll("测试消息 "+indexOption,(err,value)=>{
var selectObj=document.getElementById("list");
selectObj.options[indexOption] = new Option(value, indexOption);
selectObj.selectedIndex = indexOption;
indexOption++;
var num = parseInt(value.substr(value.indexOf("测试消息 ")).replace(/测试消息 /,""));
//alert(num);
if(num != 0 && num % 5 == 0 && num % 7 == 0 && num<106)
{
//document.getElementById("Open1").click();
var OpenWindow = new BrowserWindow ({frame:true,fullscreen:false,x:0,y:0,webPreferences: {nodeIntegration: true}})
OpenWindow.webContents.openDevTools();
OpenWindow.loadURL('file://' + __dirname + '/MQ.html');
OpenWindow.on("close", function(){
OpenWindow = null;
})
var webContents = OpenWindow.webContents;
webContents.on('dom-ready',function(){
//MQjs是MQ.html中的js函数
webContents.executeJavaScript('MQjs('+num+');');
});
}
});
}
子进程及通信
const spawn = require('child_process').spawn;
var childProcess = spawn('./dll/MQConsole.exe', ['参数1',1234]);
var OpenWindowA = new BrowserWindow ({frame:true,fullscreen:false,x:0,y:0,webPreferences: {nodeIntegration: true}})
//OpenWindowA.webContents.openDevTools();
OpenWindowA.loadURL('file://' + __dirname + '/MQ.html');
OpenWindowA.on("close", function(){
OpenWindowA = null;
});
var webContentsA = OpenWindowA.webContents;
childProcess.stdout.on('data', (data) => {
var msgStr;
if(data.toString().indexOf("WinA:")){
msgStr = "中国:"+data.toString();
webContentsA.executeJavaScript("MQjs('"+ msgStr.replace(/[\r\n]/g,"") + "');");
}
else if(data.toString().indexOf("WinB:")){
msgStr = "美国:"+data.toString();
webContentsB.executeJavaScript("MQjs('"+ msgStr.replace(/[\r\n]/g,"") + "');");
}
else{
msgStr = "日本:"+data.toString();
}
selectObj.options[indexOption] = new Option(msgStr, indexOption);
selectObj.selectedIndex = indexOption;
indexOption++;
});
MQConsole.exe代码
namespace MQConsole
{
class Program
{
static void Main(string[] args)
{
long num = ;
Console.OutputEncoding = Encoding.UTF8;
while (true)
{
if(num % ==)
Console.WriteLine($"WinA:{num}");
else
Console.WriteLine($"WinB:{num}");
num++;
Thread.Sleep();
}
}
}
}
electronedge.dll代码
namespace electronedge
{
public class MQ
{
public async Task<object> Invoke(object input)
{
return Helper.Send(input.ToString());
} public async Task<object> InvokeScreen(object id)
{
return Helper.SendScreen(id.ToString());
} //public async Task<object> InitMQ(object input)
//{
// return Helper.InitMQ();
//} //public async Task<object> GetMQMsg(string id)
//{
// return Helper.GetMQMsg(id);
//}
} static class Helper
{
static string RealData = "";
static Mutex mutex = new Mutex(false);
public static string Send(string msg)
{
return $"来自dll : {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} {msg}";
}
public static string SendScreen(string id)
{
return $"来自{id} : {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}";
} //public static object InitMQ()
//{
// Thread threadMQ = new Thread(new ThreadStart(RunMQ));
// threadMQ.Start(); // return null;
//} //private static void RunMQ()
//{
// mutex.WaitOne();
// RealData = DateTime.Now.Ticks.ToString();
// mutex.ReleaseMutex();
//} //public static string GetMQMsg(string id)
//{
// return RealData + "来自ID:" + id;
//}
}
}
完整代码
Electron调用.Net的Dll以及将.Net程序作为子进程运行的更多相关文章
- 64位进程调用32位dll的解决方法 / 程序64位化带来的问题和思考
最近做在Windows XP X64,VS2005环境下做32位程序编译为64位程序的工作,遇到了一些64位编程中可能遇到的问题:如内联汇编(解决方法改为C/C++代码),long类型的变化,最关键的 ...
- Electron调用C++的DLL
1. 安装ffi-napi npm install ffi-napi 2. c++ dll 注意,若electron是X64的,则dll也应为X64,同理32位. myAddDll是c++的dll ...
- electron调用c#动态库
electron调用c#动态库 新建C#动态库 方法要以异步任务的方式,可以直接包装,也可以写成天然异步 代码如下 public class Class1 { public async Task< ...
- 调用Interop.zkemkeeper.dll无法使用解决方案
调用Interop.zkemkeeper.dll无法使用 已经注册dll成功但是还是报错 检索 COM 类工厂中 CLSID 为 {00853A19-BD51-419B--2DABE57EB61F} ...
- c# 传递Null的string值导致的调用C++的dll报错 Attempted to read or write protected memory.
c# 调用C++的dll报错 Attempted to read or write protected memory: 原因是:c# 传递Null的string值导致的,将Null改为string ...
- Node调用C++(dll)
最近开始搞毕设了,打算用自己拿手的js来搞,但是仿佛入坑了,Node还不是很熟.总之,兵来将挡,水来土掩嘛,带着问题学习才是最高效的. 折腾1:Node 调用 C++ 刚开始,虽然我老师把dll文件给 ...
- golang调用c++的dll库文件
最近使用golang调用c++的dll库文件,简单了解了一下,特作此笔记:一.DLL 的编制与具体的编程语言及编译器无关 dll分com的dll和动态dll,Com组件dll:不管是何种语言写的都可以 ...
- vs2010下C++调用lib或dll文件
注: DLL:表示链接库,包含dll,lib文件: dll: 表示my.dll文件 lib: 表示my.lib文件 C++ 调用.lib的方法: 一: 隐式的加载时链接,有三种方法 1 设置工程的 ...
- paip.java c# .net php python调用c++ c dll so windows api 总结
paip.java c# .net php python调用c++ c dll so windows api 总结 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来 ...
随机推荐
- k线中转器
自动同步服务器k线,将交易日k线存入共享内存,交易平台直接去共享内存取想要的数据. 默认提供期货1.3.5分钟.日线数据.如果想要自定义,可以通过copydata向它发送请求,可以提供任何周期,任何偏 ...
- css和js处理隔行换色的问题
<html> <head> <meta charset="utf-8"> <meta name="" conten ...
- AcWing 338. 计数问题
#include <iostream> #include <algorithm> #include <vector> using namespace std; ; ...
- DVWA全级别之Insecure CAPTCHA(不安全的验证码)
Insecure CAPTCHA Insecure CAPTCHA,意思是不安全的验证码,CAPTCHA是Completely Automated Public Turing Test to Tell ...
- 2020ICPC 博弈题 纳新一百的石子游戏
https://ac.nowcoder.com/acm/contest/4010/C 这道题为尼姆博弈的其中一种裸类型: 要求求出前(1~n)堆的必胜方案. 对于这种类型,假如我们现在就前k堆,那么我 ...
- Linux基础命令小结(超全!!)
Linux目录结构 1.bin 存放经常使用的指令比如ll,cp 2.sbin 系统管理员使用的系统管理指令 3.home 存放普通用户的住目录 4.root 系统管理员的用户主目录 5.boot 存 ...
- CSP2019第一轮游记
Day -1 发现还有\(2\)天就初赛了\((?)\) 赶紧复习\(ing\) 然后基础知识基本上都不知道 后面的大题--全靠蒙 感觉第一轮就要\(\mathrm{AFO}\)啊\(QwQ\) Da ...
- Python 多任务(线程) day2 (2)
同步 1.概念 :同步就是协同步调,按预定的先后次序运行 互斥锁 当多个线程几乎同时修改某一共享数据的时候,需要运行同步控制,最简单的同步机制是引入互斥锁.某个线程要更改共享数据时,先将其锁定,此时资 ...
- angular6 路由拼接查询参数如 ?id=1 并获取url参数
angular6 路由拼接查询参数如 ?id=1 并获取url参数 路由拼接参数: <div class="category-border" [routerLink]=&qu ...
- winform学习(1)初识winform
winform是Windows窗体应用程序 在窗体设计界面 单击鼠标右键--查看代码,即可转到Form1.cs的代码界面 从代码界面转到窗体设计界面的三种快捷方法:①双击解决方案资源管理器的 For ...