英文:
How to change / to \\ in the path to jvm.dll
问题
我花了30分钟的时间来思考选择哪个标题,因为我的问题相当奇怪。
假设我们有一个C++本机动态链接库(C.dll)。我想要借助JNA将这个dll加载到我的Java项目中。现在重要的是:在C.dll中有一个广泛使用的文件路径,从这个路径调用了这个dll。而且实际上有一个类似这样的if语句:
if (filepath[2] != _T('\\'))
{
//抛出错误并关闭dll加载
}
如果我尝试通过JNA加载这个dll,它会抛出一个内部dll错误,错误消息如下:"在位置3处缺少特定的符号 '\',位于:C:/Program Files/AdoptOpenJDK/jdk-14.0.0.36-hotspot/bin/client"。
我了解这个错误的性质,我也理解这个if语句真的很愚蠢,因为它绝对不跨平台,但是我不能更改C.dll,因为它来自第三方,你知道那个故事。
另一个问题是,我不能只是在某个地方替换这个文件路径参数(例如通过其他的wrapper.dll之类的),然后通过JNA调用这个包装器并且开心地运行,因为在C.dll内部的所有文件路径检查都是使用多个宏来硬编码的,而且隐含着所有接收和检查这个文件路径的例程是完全自动发生的(实际上由其他dll提供,C.dll必须使用这些dll,而我没有源代码,是的,情况确实很糟糕)。因此,要找到一个我可以直接替换的具体值并不容易。
因此,我的问题是:是否有可能在jvm.dll的文件路径中运行时将这些(/)替换为双(\)反斜杠?是否可能编写一个wrapper.dll,以C++或Java编写,来为我的C.dll做这个事情?我最终需要在JNA中运行它,这实际上就是为什么文件路径恰好是JAVA_HOME之类的——因为据我所了解,Java虚拟机正是在这种情况下加载C.dll的进程,这就是为什么我得到了jvm.dll的路径作为错误的文件路径。据我所知,所有的Java路径在JDK中都是使用(/)硬编码的,所以从JNA的角度来看,在我看来,这是不直接可能的。但是是否可以在不改变原始.dll的情况下通常实现呢?也许在Windows中有类似于路径替换工具的东西可以用于Java?
英文:
I thought which title to choose for 30 minutes, because my problem is rather strange.
Suppose we have a C++ native dll (C.dll). I want to load this dll with the help of JNA into my java project. Now what's important: in C.dll there is widely used a file path, from where this dll was called. And there is literally an if-statement like this:
if (filepath[2] != _T('\\'))
{
//throw an arror and close dll loading
}
If I try to load this dll via JNA, it throws an internal dll error with the following message: "There is missing a certain sign '' in: "C:/Program Files/AdoptOpenJDK/jdk-14.0.0.36-hotspot/bin/client" on position: 3"
I know the nature of that error and I also understand, that this if-statement is really silly, because it is absolutely anti-crossplatform, but I couldn't change C.dll, because it has come from 3rd party, well, you know that story.
What is also problematic - I couldn't just replace this file path parameter somewhere (f.e. via some other wrapper.dll or so), call then this wrapper via JNA and be happy, because all that file path checking inside C.dll is harcoded with the help of multiple macros and it is implied, that all the routines of receiveing and checking this file path are occured fully automatically (this is actually provided by other dlls, which C.dll have to use, and for which I don't have a source code, yes, it is that bad). So it is not so easy to find a concrete value which I could directly replace.
So, my question is: is it somehow possible to replace those (/) to double (\) backslashes in file path of jvm.dll in runtime or so? Is it possible to write a wrapper.dll, that will do it for my C.dll, either in C++ or java? I need to run this finally in JNA, that's actually why the file path is exactly JAVA_HOME or so - because as far as I understood java virtual machine is exactly the process, who loads C.dll in that situation, that's why I got the path to jvm.dll as a wrong file path. AFAIK all the java paths are harcoded with (/) in JDK, so from the JNA's side it is not directly possible in my opinion. But is it possible generally without changing original .dll? Maybe there exist something like path-replacers tools for java in Windows or so?
专注分享java语言的经验与见解,让所有开发者获益!
评论