簡體   English   中英

從前端javascript中加載的DLL調用函數(在clientside javascript中加載dll)

[英]Call Function from DLL loaded in front-end javascript (load dll in clientside javascript)

我有一個簡單的客戶端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數據)

有沒有人這樣做過,或者甚至可能這樣做?

謝謝你,丹尼爾。

我做到了 解決方案是使用EdgeJS作為NodeJS V8和C#CLR之間的橋梁。 Edge加載DLL並在V8和CLR之間創建通信通道,消息是在每個語言VM之間編組的Func<object, Task<object>>function(payload, callback)形式的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;
})();

我希望實施清楚。 如果您有任何理解,請不要猶豫,寫信給我。

快樂的編碼!

[不要聽反對者!]

從javascript代碼中調用DLL庫中的函數。

不能。您無法在JavaScript中加載DLL。

為什么?

JavaScript在瀏覽器的堆棧中執行。 要使任何可用的東西,您必須使用瀏覽器連接它,然后使其可訪問; 太多的工作。

解決

您可以使用C#編寫的客戶端應用程序連接到JS websocket,然后傳輸數據。 WebSocket可以檢查特定的數據塊,並以您希望的方式處理它。

我已經按照我在使用指紋掃描儀的項目中描述的方式使用它。 效果很好! 如果你添加一點加密,甚至更好!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM