在 C# 中使用本机 DLL

发布于 2025-01-08 04:16:15 字数 1631 浏览 1 评论 0原文

我们聘请了一名 C 程序员来开发用于 .NET 应用程序的本机组件。我们就概念性 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 来使其工作。它抱怨该文件没有程序集清单。因此,我将头文件插入 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)]

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

We hired a C programmer to develop a native component for use in a .NET application. We agreed on a conceptual API. I will pass his method two arrays and he will give back an array. I got the code today. Here is the header file. Real names are obscured:

__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;
};

From this I generated .NET code using P/Invoke Interop Assistant. I was not able to get it to work by opening the DLL. It complained that the file has no assembly manifest. So I plugged the header file into the SigImpl Translate Snipped and got this:

[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);

It also create all as structs as expected. Each one has a class attribute:

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

Two questions. Did this code generate correctly? Second, how do I use it? The signature does not have arrays. I know I probably need to use pointers somehow, but how? I don't expect you to solve this for me, but can you point me to some way to understand how to figure it out without taking a course in low native programming? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅气尐潴 2025-01-15 04:16:15

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

    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 解决了这个问题。那是多么脆弱啊?!

Okay, after I posted this question I started hacking away again. The first thing I tried worked!

    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);

There's one other thing I had to do. I was getting an error that entry point was not found. I solved this by using DUMPBIN (from VS console) to find out that the export was actually named ?NativeMethod@@YAHPAUconfig@@HPAUinput_row_a@@HPAUinput_row_b@@HPAHPAUoutput_row@@@Z. How fragile is that?!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文