gpt4 book ai didi

c# - 创建一个简单的 c# dll 并从 ruby​​ 访问它

转载 作者:数据小太阳 更新时间:2023-10-29 07:53:27 24 4
gpt4 key购买 nike

我正在尝试制作我的第一个 c# dll。我希望能够使用 win32API 从 ruby​​ 调用它的方法/函数。我制作了这个 dll:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassLibrary1
{
public class Class1
{

public int PropA = 10;

public void Multiply(Int32 myFactor)
{
PropA *= myFactor;

}
}
}

我用 Visual Studio 2010 编译它并得到我的 ClassLibrary1.dll 文件。

现在对于 ruby 部分,我尝试了这个:

f = "c:/path_to_file/ClassLibrary1.dll"
mult = Win32API.new(f,"Multiply",["I"],"I")

但是,我收到以下错误:

Error: #<RuntimeError: (eval):0:in `initialize': GetProcAddress: Multiply or MultiplyA

老实说,我从来没有创建过dll,也没有使用过c#的经验。我只是想开始。我之前通过 win32API 将许多 dll 与 Ruby 一起使用(主要是 user32)。

我猜我的 dll 有问题?

问候,

最佳答案

要像在 Win32 上下文中那样使用 DLL 中的函数,这些函数需要从 DLL 导出可见。这些函数通常带有 dllexport 标记,并将显示在 DLL 的导出表中。您可以使用 dumpbin (MSDN) 等实用程序验证 user32 和其他 Win32 DLL 是否执行此操作.

dllexport, dllimport @ MSDN

不幸的是,.NET 不直接支持使函数导出可见,只能通过 DllImport (MSDN) 引入此类函数。属性。

COM 是从非 .NET 环境使用 .NET 中编写的代码的最简单方法,但如果您必须使用较低级别的绑定(bind),则可以使用 C、C++/CLI 或任何其他语言制作中间 DLL和框架组合,可以将导出 header 写入 dll,以调用您的 .NET 代码。

此外,还有几篇文章介绍了如何通过编译后自动化或其他解决方法来实现这一点,这里有几篇供您引用。

How to Automate Exporting .NET Function to Unmanaged Programs @ CodeProject

Unmanaged Exports @ Google Sites

DllExport - Provides C-style exports for pure .NET assemblies

关于c# - 创建一个简单的 c# dll 并从 ruby​​ 访问它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14366491/

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