繁体   English   中英

用Java加载Windows DLL并从中初始化一个类

[英]Loading a Windows DLL in Java and initiate a class from it

我有一个来自.NET的Windows DLL文件,即“ System.Management.dll”。 我使用下面编写的代码来处理它:

ManagementObjectSearcher searcher =
                new ManagementObjectSearcher("root\\CIMV2",
                "SELECT * FROM Win32_LogicalDisk WHERE Name = 'C:'");

            foreach (ManagementObject queryObj in searcher.Get())
            {
                Console.WriteLine("Win32_LogicalDisk instance: ");
                if (queryObj["VolumeSerialNumber"] != null)
                {
                    Console.WriteLine("Drive Name : " + queryObj["Name"]);
                    Console.WriteLine("VolumeSerialNumber:", queryObj["VolumeSerialNumber"]);
                    SysdriveSerial = queryObj["VolumeSerialNumber"].ToString();
                }

            }

现在,我需要用Java编写这段代码。 那我可以这样做吗? 没有像c ++这样的非托管代码。 我不想使用C ++非托管代码来调用此dll。

我想要这样的东西:

public class CallToCsharp {
    private static native void ManagementObjectSearcher();

    public static void main(String[] args) {

            System.loadLibrary("System.Management");
               System.out.println("Loaded");
                      ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2",
                    "SELECT * FROM Win32_LogicalDisk WHERE Name = 'C:'");
    }
}

好吧,这最后的代码我必须放在Java中。 如何加载此DLL并调用DLL以实例化DLL中的本机类并使用其方法?

更新资料

我看到了这件事,看来如果要使用该类,需要做很多工作,例如在.net Reflector中选择每个类并将它们转换为jar文件。 现在,按照该教程,我看到jar文件不包含任何要使用的真实代码。

如何使用它? 我的意思是,如果我需要生成实际足以与代码一起使用的jar文件。 怎么做呢?

而且没有其他选择吗?

您想走这条路并不容易,但是IKVM也许可以提供帮助。

Overview

IKVM makes it possible to develop .NET applications using the Java language. Here's how it works:

Identify .NET classes you want to use in your application.
Identify which .NET dll's contain the .NET classes you identified in step 1.
Tip: If you're developing on Windows, the Microsoft .NET SDK Class Reference documentation
identifies the assembly / dll for a .NET class at the bottom of each class overview page.

Use the ikvmstub application to generate a Java jar file for each dll you identified in step 2.

The ikvmstub tool analyzes the .NET classes in the designated dll and generates a jar file 
containing Java interfaces and stub classes. This information is needed by the Java source 
compiler, which knows nothing about .NET assemblies.

Compile your Java source code using javac or jikes, with the ikvmstub-generated jar files on the
compiler classpath.
Compile the resulting Java classes using ikvmc. Use the -reference option to reference the dll's 
containing the .NET classes you used; do not include the ikvmstub-generated jar files on the 
compiler classpath.

For an example of this, see the tutorial.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM