簡體   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