从前端javascript中加载的DLL调用函数(在clientside javascript中加载dll)

时间:2016-01-14 13:51:13

标签: javascript c# c++ .net dll

我有一个简单的客户端javascript应用程序。我希望它加载DLL文件(对于SwipeReader CR100)并从javascript代码中调用DLL库中的函数。第二种是向SwipeReader触发的事件添加侦听器,如DocumentRead或DocumentReadError,并在javascript中处理它们。

所以,我有4个小问题需要解决:

  1. 在javascript(主要是Chrome V8引擎)中加载DLL。
  2. DLL中的调用函数。
  3. 将侦听器添加到DLL中触发的事件。
  4. 在回调中使用响应对象在JS中执行某些操作(alert,console.log数据)
  5. 之前是否有人这样做过,或者这是否可能?

    提前谢谢你, 丹尼尔。

2 个答案:

答案 0 :(得分:4)

我已经完成了。解决方案是使用EdgeJS作为NodeJS V8和C#CLR之间的桥梁。 Edge加载DLL并在V8和CLR之间创建通信通道,消息是在每个语言VM之间编组的Func<object, Task<object>>function(payload, callback)形式的函数。

我将在下面发布代码示例:
C#

public abstract class NetWebSocket
{
    private Func<object, Task<object>>  SendImpl { get; set; }

    protected NetWebSocket(Func<object, Task<object>> sendImpl)
    {
        this.SendImpl = sendImpl;
    }

    protected abstract Task ReceiveAsync(string message);

    public Func<object, Task<object>> ReceiveImpl
    {
        get
        {
            return async (input) =>
            {
                Console.Out.WriteLine(input);
                await this.ReceiveAsync((string) input);
                return Task.FromResult<object>(null);
            };
        }
    }

    protected async Task SendAsync(string message)
    {
        await this.SendImpl(message);
        return;
    }
}

public class MyNetWebSocketImpl : NetWebSocket
{
    public CHello module;
    private string JSONCodelineDataRepr = "not set";

    public MyNetWebSocketImpl(Func<object, Task<object>> sendImpl) : base(sendImpl)
    {
        // do other stuff after calling the super class constructor  
        module = new CHello();
        module.DocumentReadEvent += this.DocumentReadEventHandler;
        module.DocumentReadErrorEvent += this.DocumentReadErrorEventHandler;
        // uncomment after the websocket communication works
        module.Start();
    }

    protected override async Task ReceiveAsync(string message)
    {
        // not really needed because only the NodeJS Server listens to C# .NET Server messages
        Console.WriteLine(message);
        if (message.Equals("shutdown"))
        {
            module.Close();
        }
        // not necessary (can comment the send function call)
        // if I eventually receive a message, respond with the JSON representation of the Patient ID Card
        await this.SendAsync("I received a message from you, but I'll ignore it and send you the Patient" +
                             " ID Card Data instead.. I'm a fish, so start phishing! PersonData = " +
                             JSONCodelineDataRepr);
        return;
    }

    private async void DocumentReadEventHandler(string args)
    {
        this.JSONCodelineDataRepr = args;
        await this.SendAsync(args);
    }

    private async void DocumentReadErrorEventHandler(string args)
    {
        await this.SendAsync(args);
    }
}

public class Startup
{

    public static MyNetWebSocketImpl ws;

    public async Task<object> Invoke(Func<object, Task<object>> sendImpl)
    {
        ws = new MyNetWebSocketImpl(sendImpl);

        return ws.ReceiveImpl;
    }
}

Javascript / NodeJS

(function(e,d,g,e){

var edge = require('edge'),
    http = require('http'),
    WebSocketServer = require('ws').Server,
    swipe = edge.func('./dlls/ActiveXCOM.dll');

var server = http.createServer(function(req,res){
    res.writeHead(200, {'Content-Type' : 'text/html'});
    res.end((
        function () { /*
            <!DOCTYPE html>
            <html>
            <head>
            </head>
            <body>
                <div id='jsonOutput'>
                </div>
                <script>
                    var ws = new WebSocket('ws://' + window.document.location.host);

                    ws.onmessage = function (event) {
                        console.log(event.data);
                        var div = document.getElementById('jsonOutput');
                        div.innerHTML = event.data;
                    }

                    ws.onopen = function (event) {
                        // send something to the server
                        ws.send('I am the client from the browser calling you, the NodeJS WebSocketServer!');
                    }

                    ws.onclose = function (event) {
                        alert('websocket closing');
                    }

                    window.onbeforeunload = function myonbeforeUnload() {
                        return "Are you sure?";
                    }

                    window.onunload = function myonunload() {
                        confirm('Are you really sure?');
                        ws.close();
                        return "Are you really sure?";
                    }
                </script>
            </body>
            </html>
        */}).toString().match(/[^]*\/\*([^]*)\*\/\}$/)[1]);
});

var wss = new WebSocketServer({server: server});

wss.on('connection', function(ws) {
    var sendImpl = function (message, callback) {
        console.log(message);
        ws.send(message);
        callback();
    };

    var receiveHandler = swipe(sendImpl, true); 

    ws.on('message', function (message) {
        receiveHandler(message);
    });

    ws.on('close', function close(){
        console.log('****************************The client disconnected!');
        receiveHandler('shutdown');
        delete receiveHandler;
    });
});

server.listen(process.env.PORT || 8080);
module.exports = this;
})();

我希望实施清楚。如果您对此有任何疑问,请不要犹豫,写信给我。

快乐的编码!

[不要听取反对者的意见!]

答案 1 :(得分:1)

  

从javascript代码中调用DLL库中的函数。

没有。您无法在JavaScript中加载DLL。

  

为什么?

JavaScript在浏览器的堆栈中执行。要使任何可用的东西,您必须使用浏览器连接它,然后使其可访问;太多的工作。

  

解决方法

您可以使用C#编写的客户端应用程序连接到JS websocket,然后传输数据。 WebSocket可以检查特定的数据块,并按照您想要的方式进行处理。

我已经按照我在使用指纹扫描仪的项目中描述的方式使用它。效果很好!如果你添加一点加密,甚至更好!