gpt4 book ai didi

.net - 在 C# 中使用 native DLL

转载 作者:行者123 更新时间:2023-11-30 18:02:08 25 4
gpt4 key购买 nike

我们聘请了一名 C 程序员来开发用于 .NET 应用程序的 native 组件。我们就概念性 API 达成了一致。我将向他的方法传递两个数组,他将返回一个数组。今天我收到了代码。这是头文件。真实姓名被隐藏:

__declspec(dllexport) int NativeMethod(
struct params * config,
int c_input_a_rows,
struct input_a_row *input_a_rows,
int c_input_b_rows,
struct input_b_row *input_b_rows,
int c_count,
int *p_c_output_rows,
struct output_row * output_rows);

struct params
{
int a;
int b;
int c;
double d;
double e;
int f;
int g;
char h[1000];
};

struct input_a_row
{
int a;
int b;
double c;
};

struct input_b_row
{
int a;
int b;
int c;
int d;
int e;
double f;
double g;
};

struct output_row
{
int a;
int b;
int c;
int d;
int e;
int f;
int g;
double h;
double i;
double j;
};

由此我使用 P/Invoke Interop Assistant 生成了 .NET 代码。我无法通过打开 DLL 来使其工作。它提示该文件没有程序集 list 。所以我将头文件插入 SigImpl Translate Snipped 并得到:

[DllImport("the.dll", EntryPoint="NativeMethod")]
public static extern int NativeMethod(
ref params config,
int c_input_a_rows,
ref input_a_row input_a_rows,
int c_input_b_rows,
ref input_b_row input_b_rows,
int c_count,
ref int p_c_output_rows,
ref output_row output_rows);

它还按预期创建所有结构。每个都有一个类属性:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]

两个问题。这段代码生成正确吗?第二,我该如何使用它?签名没有数组。我知道我可能需要以某种方式使用指针,但是如何呢?我不希望你为我解决这个问题,但是你能给我指出一些方法来理解如何在不参加低 native 编程类(class)的情况下解决这个问题吗?谢谢!

最佳答案

好吧,在我发布这个问题后,我又开始黑客攻击了。我尝试的第一件事成功了!

    private static extern int NativeMethod(
params config,
input_a_row[] input_a_rows,
input_b_row[] input_b_rows,
int count,
ref output_row[] output_rows);

我还有一件事要做。我收到一个错误,指出未找到入口点。我通过使用 DUMPBIN(从 VS 控制台)发现导出实际上名为 ?NativeMethod@@YAHPAUconfig@@HPAUinput_row_a@@HPAUinput_row_b@@HPAHPAUoutput_row@@@Z 解决了这个问题。这是多么脆弱?!

关于.net - 在 C# 中使用 native DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9420282/

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