如何远程加载dll库? [英] How to load dll library remotely?

查看:121
本文介绍了如何远程加载dll库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台安装了应用程序的远程计算机,它的C语言编写的API被编译成dll.

I have a remote machine which has an application installed and has its APIs written in C compiled into a dll.

我想使用通过在Java中通过JNA远程加载dll公开的API与应用程序进行交互.也就是说,我的客户端代码需要将dll加载到目标计算机中并与应用程序进行交互.

I want to interact with the application using the APIs exposed by loading the dll through JNA in java remotely. i.e., my client code need to load the dll in the target machine and interact with the application.

我探索了使用JMI的可能性,但它增加了更多的复杂性.

I explored the possibility of using JMI, but it adds more complexity.

如何使用JNA/JNI远程加载dll文件?

How to load dll files remotely using JNA/JNI?

推荐答案

您可以相应地指定dll的位置.我正在一个需要类似功能的项目中.请参考下面的代码.与客户端共享目标计算机中dll的位置后,您可以通过指定如下所示的路径来访问dll.

You can specify the location of the dll accordingly. I'm working on a project which requires similar features. Refer the code below . Once you shared the location of the dll in the target machine with your client , you can access the dll by specifying the path as shown below.

public class TestRemoteDll{
public native String readFile();


public static void main(String args[]){
    System.load("\\\\{Device's Name}\\Users\\Milan.AF\\Desktop\\RemoteDir\\Remotedll.dll");
    TestRemoteDll test = new TestRemoteDll();
    System.out.println("Calling native method!");
    String sum = test.readFile();
    System.out.println("Returned from Native Method");

    System.out.println(sum);
}

}

并确保也相应地创建了dll(使用的dll文件也应与客户端共享).创建dll时,必须以类似的方式指定文件的位置,如下所示. /p>

And make sure you create the dll accordingly as well(The files dll use should be shared with the client as well ).You have to specify the location of files in a similar way when you create a dll as shown below.

#include "stdafx.h"
#include "iostream"
#include<string>
#include <fstream>
#include "Remotedll.h"
using namespace std;


string RemoteDll::readFile() {
int sum=0,x;
ifstream inFile;
inFile.open("\\\\{Device's Name}\\temp\\intSum.txt");
if (!inFile) {
    return "Failed to open file!";
        }
while (inFile >> x) {
    sum = sum + x;
}
inFile.close();
string str = to_string(sum);
return  "File operation successful! Sum =" + str    ;

}

我希望这会有所帮助.

这篇关于如何远程加载dll库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆