- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
目前我正在做一个项目,我必须在 Unity 中从 C# 调用其他人编写的一些 Julia 脚本。我一直在尝试做一些基本的例子,看看哪些有效,哪些无效。在 Julia 文档中,它说使用函数:jl_get_function
获取指向带有 julia 模块的函数的指针。但是,我在 libjulia.dll
中得到了一个 EntryPointNotFound
,当我在计算机上使用 DependencyWalker 打开 dll 时,我找不到调用它的函数。我是疯了还是安装了一些奇怪的东西? jl_eval_string
和 jl_unbox_float64
等其他东西工作正常。
此外,我不完全确定如何为 jl_get_function
获取指向模块的指针。我想过从内存映射文件对象中获取指针,或者从 jl_eval_string(include([目录中的模块名称]));
中获取 IntPtr
,但是我不知道。
这是我在 Julia 中用于此测试的代码。
module TestModule
export calculate
function calculate(a::Float64,b::Float64)::Float64
return 3a+b^2
end
function calcMore(a,b)
return ones(a,b)::Array{Float64,2};
end
function moreTest(a::Float64, b::Float64)
return (a+b)::Float64;
end
end
这是我用 C# 编写的代码,被删减了一些
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace TestCInCSharp
{
class Program
{
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool SetDllDirectory(string lpPathName);
[DllImport("libjulia.dll", SetLastError = true)]
public static extern void jl_init(string path);
[DllImport("libjulia.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr jl_eval_string(string input);
[DllImport("libjulia.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr jl_box_float64(float value);
[DllImport("libjulia.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern double jl_unbox_float64(IntPtr value);
[DllImport("libjulia.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr jl_get_function(IntPtr func, string name);
[DllImport("libjulia.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr jl_call2(IntPtr func, IntPtr v1, IntPtr v2);
[DllImport("libjulia.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void jl_atexit_hook(int a);
static void Main(string[] args)
{
string p = @"C:\Users\schulk4\Documents\Programming\TestJuliaSim\Assets\test_julia.jl";
string julia_path = @"C:\Users\schulk4\AppData\Local\Julia-0.5.2\bin";
IntPtr module, module2;
IntPtr a, b, c;
SetDllDirectory(julia_path);
jl_init(julia_path);
p = @"C:\\Users\\schulk4\\Documents\\Programming\\TestJuliaSim\\Assets\\test_julia.jl";
p = "include(\"" + p + "\")";
module = jl_eval_string(p); //holds module pointer?
a = jl_eval_string("TestModule.calculate(3.0,4.0)");
double d = jl_unbox_float64(a);
Console.WriteLine(d);
a = jl_eval_string("TestModule.calculate");
b = jl_box_float64(3.0f);
c = jl_box_float64(4.0f);
module2 = jl_call2(a, b, c);
d = jl_unbox_float64(module2);
Console.WriteLine(d);
a = jl_eval_string("TestModule.moreTest");
b = jl_box_float64(12.0f);
c = jl_box_float64(13.0f);
module2 = jl_call2(a, b, c);
d = jl_unbox_float64(module2);
Console.WriteLine(d);
IntPtr f = jl_get_function(module, "calculate"); //EntryPointNotFoundException
jl_atexit_hook(0);
Console.ReadLine();
}
}
您可以看到我尝试在代码中使用 jl_eval_string
获取指向函数的指针。这是异常之前运行的示例:
25
1.5977136277678E-314
1.08223857600744E-314
我遇到了各种各样的问题,我只是想知道是否有人能够帮助我。我对这个话题不是很熟悉,大约一周前我了解了P/Invoke
。
最佳答案
jl_get_function
是一个内联函数。您可以使用 jl_get_global
。
另请注意,您的代码随时可能崩溃。所有需要在对 julia 运行时/函数的调用中使用的 jl_value_t*
都必须是 root 的。请参阅嵌入文档中的内存管理部分。我不知道如何将其转换为 C#。
关于c# - 将 Julia 脚本嵌入到 C# : jl_get_function doesn't exist?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44314254/
目前我正在做一个项目,我必须在 Unity 中从 C# 调用其他人编写的一些 Julia 脚本。我一直在尝试做一些基本的例子,看看哪些有效,哪些无效。在 Julia 文档中,它说使用函数:jl_get
我是一名优秀的程序员,十分优秀!