gpt4 book ai didi

java - 无法使用 JNI4NET 工具生成 C# 代理 dll,将批处理文件作为可信程序集运行?

转载 作者:可可西里 更新时间:2023-11-01 11:24:54 26 4
gpt4 key购买 nike

我正在努力获取 tool JNI4NET工作,以便我可以使用我的 C# 应用程序中的一些 Java 代码。作为一个简单的初始测试,我创建了一个简单的 Java 类库,其中包含一个类 Person 和一个方法 public String GetName() { return "NoBody"; }。从这里开始,我一直按照 JNI 下载中提供的示例编辑 generateProxies.cmd 以创建 jar 的 DLL 包装器。

我在这方面运气不佳,所以我决定尝试执行相同的操作,但使用示例,特别是名为 myJavaDemoCalc 的示例。在示例文件夹中执行 generateProxies.cmd 时会抛出错误。

(有需要我会转载这张图) enter image description here

我关注了the link in the exception尽管我有点理解这意味着什么,但我不确定从链接文章末尾建议的远程源加载是否一定安全。

我也很困惑为什么看到 generateProxies.cmd 并因此从我的 C: 驱动器运行 ProxyGen.exe 时抛出异常。

有人知道我接下来可以尝试什么或知道这里的问题吗?

这里的引用是 myJavaDemoCalcgenerateProxies.cmd 源代码

@echo off
copy ..\..\lib\*.* work
..\..\bin\proxygen.exe work\myJavaDemoCalc.jar -wd work
cd work
call build.cmd
cd ..

echo compiling usage
csc.exe /nologo /warn:0 /reference:work\jni4net.n-0.8.8.0.dll /reference:work\myJavaDemoCalc.j4n.dll /out:work\demo.exe /target:exe MyCalcUsageInDotnet.cs

最佳答案

我假设您下载了该 zip 文件,然后立即解压缩了所有文件。

但是,由于该 zip 文件确实来自不受信任的区域,即 Internet,因此其中的文件也将保持不受信任。它包含带有区域标识符的备用数据流。

当框架加载这些程序集时,它会检查它们是否可信。具有该区域标识符的程序集仍然存在不会被加载。那是你得到的异常(exception):

System.IO.FileLoadException: Could not load file or assembly 'file:///jni4net.n-0.8.8.0.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515) --->
System.NotSupportedException: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch.

解决此问题的最快方法是打开下载的 zip 文件的属性窗口并勾选unblock解压所有文件之前:

enter image description here

如果您已经将所有文件提取到一个文件夹中,您可以使用 powershell 命令 unblock-file

Get-ChildItem -Path 'c:\path\to\files' -Recurse | Unblock-File

但是如果您确定您将始终使用受信任的程序集运行 proxygen.exe,您可以通过添加 loadFromRemoteSources 元素来添加 MSDN 文章中提供的建议在现有的 proxygen.exe.config 中:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
<supportedRuntime version="v2.0.50727"/>
</startup>
<!-- trust all the thingz -->
<runtime>
<loadFromRemoteSources enabled="true"/>
</runtime>
</configuration>

关于java - 无法使用 JNI4NET 工具生成 C# 代理 dll,将批处理文件作为可信程序集运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41042368/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com