简体   繁体   中英

Loading a Windows DLL in Java and initiate a class from it

I have a Windows DLL file from .NET, namely "System.Management.dll". I work with it using the code I write below:

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();
                }

            }

Now I need this piece of code to be in Java. So can I do this? Without anything like c++ unmanaged code. I don't want to use c++ unmanaged code to call to this dll.

I want something like this :

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:'");
    }
}

Well this last code I have to put it in Java. How can I load this DLL and call the DLL to instantiate the native class in DLL and use its methods?

Update

I saw the thing, it seems it's a lot of work to do in case I have to use that class, like selecting each of them in .net Reflector and converting them to jar files. Now as per that tutorial I saw the jar files don't contain any real code to be used.

How to use it? I mean if I need to generate the jar file actually working with the code enough. How to go about it?

And aren't there any alternatives to this?

This is not an easy road you want to go down but IKVM might be able to help.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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