在java中访问dll方法 [英] Accessing dll methods in java

查看:107
本文介绍了在java中访问dll方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问已经在c#中编写的java中的dll方法。从以下代码中,我试图构建成功生成的dll。

  using System; 
使用Microsoft.Win32;


命名空间Cyber​​oamWinHelper
{
public class RegistryAccess
{
public static String getValue(String key)
{
RegistryKey rk = Registry.CurrentUser;
RegistryKey rk1 = rk.OpenSubKey(Software\\Test,RegistryKeyPermissionCheck.ReadWriteSubTree,Sy​​stem.Security.AccessControl.RegistryRights.FullControl);
rk1.SetValue(key,val1);
return rk1.GetValue(key).ToString();
}
public static void createSubkey(String name)
{
RegistryKey rk = Registry.CurrentUser;
rk.CreateSubKey(Software\\Test);
}
}
}

此后我加载在我的java程序代码中生成的dll如下

  public class JNI {

/ * *
* @param args命令行参数
* /
public native String getValue(String key);

public static void main(String [] args){
// TODO代码应用程序逻辑在这里

try
{
系统。的LoadLibrary( Cyber​​oamWinHelper);
JNI j = new JNI();
System.out.println(j.getValue(abc));
}
catch(UnsatisfiedLinkError e)
{
System.out.println(Ex+ e.getMessage());
}
}
}

运行此代码后,给我以下错误。

 Exjni.JNI.getValue(Ljava / lang / String;)Ljava / lang / String;  

我不明白这个错误是在说什么,但我想解决它。
我还有一个问题是因为我正在调用的方法是静态方法将以这种方式被调用?我的意思是调用静态方法我们需要


classname.methodname


所以这里可以调用方法?

解决方案

你只能通过如果这些方法实际上是以这种方式被称为JNI。你的方法绝对不是。你在这里做什么(对不起这么钝)绝对没有成功的机会 - 这根本不行。



有几种方法可以继续。一个是了解JNI以及如何编写实际使用它的库。 这里是这个的规范参考。这样做与C#增加了另一层复杂性。



另一种方法是放弃JNI,并使用更合适的机制来访问方法。您可以在这里了解 JNA ;这将完全适合您的目标。


I am trying to accessing dll methods in java which has been written in c#. From the following code i am trying to build dll which is generated successfully.

using System;
using Microsoft.Win32;


namespace CyberoamWinHelper
{
    public class RegistryAccess
    {        
        public static String getValue(String key)
        {
            RegistryKey rk = Registry.CurrentUser;
            RegistryKey rk1=rk.OpenSubKey("Software\\Test", RegistryKeyPermissionCheck.ReadWriteSubTree, System.Security.AccessControl.RegistryRights.FullControl);
            rk1.SetValue(key, "val1");
            return rk1.GetValue(key).ToString();
        }
        public static void createSubkey(String name)
        {
            RegistryKey rk = Registry.CurrentUser;
            rk.CreateSubKey("Software\\Test");
        }
    }
}

After this i am loading the generated dll in my java program code of which is as follows

public class JNI {

    /**
     * @param args the command line arguments
     */
    public native String getValue(String key);    

    public static void main(String[] args) {
        // TODO code application logic here

        try
        {
            System.loadLibrary("CyberoamWinHelper");
            JNI j=new JNI();       
            System.out.println(j.getValue("abc"));
        }
        catch(UnsatisfiedLinkError  e)
        {
            System.out.println("Ex" + e.getMessage());
        }
    }
}

After running this code it is giving me the following error.

"Exjni.JNI.getValue(Ljava/lang/String;)Ljava/lang/String;"

Well i am not understanding what this error is saying but i want to solve it. And one more question i am having is since the method i am calling is a static method will it be called in this way? i mean to call static method we need

"classname.methodname"

so here will it be able to call the method?

解决方案

You can only call methods via JNI if those methods were in fact designed to be called this way. Your methods absolutely are not. What you're doing here has (sorry to be so blunt) absolutely no chance of ever succeeding -- it simply doesn't work this way.

There are several ways you might proceed. One would be to learn about JNI and how to write libraries that actually work with it. Here is the canonical reference for this. Doing this with C# adds yet another layer of complexity, though.

Another way would be to give up on JNI altogether and use a more appropriate mechanism to access the methods. You can learn about JNA here; it would be entirely better suited to your goals.

这篇关于在java中访问dll方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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