Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中加载Windows DLL并从中启动类_Java_.net - Fatal编程技术网

在Java中加载Windows DLL并从中启动类

在Java中加载Windows DLL并从中启动类,java,.net,Java,.net,我有一个来自.NET的Windows DLL文件,即“System.Management.DLL”。我使用下面编写的代码处理它: ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_LogicalDisk WHERE Name = 'C:'"); foreac

我有一个来自.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。< /P> 我想要这样的东西:

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中的本机类并使用其方法

使现代化 我看到了这一点,如果我不得不使用这个类,那么似乎有很多工作要做,比如在.NETReflector中选择它们并将它们转换为jar文件。现在根据那个教程,我看到jar文件不包含任何要使用的实际代码

如何使用它?我的意思是,如果我需要生成jar文件,并实际使用足够的代码。怎么办


还有没有别的办法呢?

这不是一条你想走的容易的路,但也许能帮上忙

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.

嘿,谢谢你的信息。。。但是我不能看这个例子。。。这里没有任何链接:(嘿,我看到了一件事,如果我不得不使用这个类,那么它似乎有很多工作要做,比如在.net Reflector中选择每个类并将它们转换为jar文件……现在根据那个教程,我看到jar文件不包含任何要使用的真实代码……如何使用它actually@Joy你要做的是相当多的工作一次应该处理整个程序集,而不仅仅是一个类文件。请尝试浏览此页