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

Cannot generate C# proxy dll with JNI4NET tool, running batch file as trusted assembly?

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

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

(有需要我会转载这张图)

我已经关注了 the link in the exception 虽然我有点理解它的意思,但我不确定从链接文章末尾建议的远程源加载是否一定安全。

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

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

此处供参考的是来自 myJavaDemoCalc

generateProxies.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,因此其中的文件也将保持不受信任。它包含带有区域标识符的备用数据流。

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

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 文件的属性 window 并勾选 unblock ,然后 解压所有文件:

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

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

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

中添加 loadFromRemoteSources 元素
<?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>