P/调用接受指向字符串对的指针的本机函数

发布于 2024-10-08 04:27:56 字数 474 浏览 0 评论 0原文

我有一个 Visual Studio 2008 C# .NET 2.0CF 应用程序,我需要在其中 P/Invoke 具有以下签名的本机函数:

/// @brief count - number of pairs
/// @brief pairs - pairs of pointers to strings
void Foo( int count, const char* pairs[][ 2 ] );

在 C++ 中,可以使用:

const char* pairs[][2] = { { "Hello", "Bob" }, { "Goodbye", "Diane" } };
Foo( 2, pairs );

How would one write the [DllImport()] 这样一个函数的签名?

谢谢, 保罗·H

I have a Visual Studio 2008 C# .NET 2.0CF application where I need to P/Invoke a native function with the following signature:

/// @brief count - number of pairs
/// @brief pairs - pairs of pointers to strings
void Foo( int count, const char* pairs[][ 2 ] );

In C++, this could be used:

const char* pairs[][2] = { { "Hello", "Bob" }, { "Goodbye", "Diane" } };
Foo( 2, pairs );

How would one write the [DllImport()] signature of such a function?

Thanks,
PaulH

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

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

发布评论

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

评论(1

九歌凝 2024-10-15 04:27:56

像这样:

[DllImport("...")]
private static extern void Foo(int count, string[] pairs);

与您的示例相对应的 C# 是:

var pairs = new[] { "Hello", "Bob", "Goodbye", "Diane" };
Foo(pairs.Length >> 1, pairs);

我使用模拟 C 库对此进行了测试,它的工作原理与广告中所宣传的一样。

libtest.c:

#include <stdio.h>

void Foo( int count, const char* pairs[][ 2 ] ) {
    int i;

    for (i = 0; i < count; i++) {
        printf("%d: %s\n", i, pairs[i][0]);
        printf("%d: %s\n", i, pairs[i][1]);
    }
}

test.cs:

using System;
using System.Runtime.InteropServices;

public static class Foobar {
    public static void Main() {
        var strings = new[] {
            "The", "quick",
            "brown", "fox",
            "jumped", "over",
            "the", "lazy",
            "dog", "LOL"
        };

        Foo(strings.Length >> 1, strings);
    }

    [DllImport("test")]
    private static extern void Foo(int count, string[] pairs);
}

输出:

0: The
0: quick
1: brown
1: fox
2: jumped
2: over
3: the
3: lazy
4: dog
4: LOL

Like this:

[DllImport("...")]
private static extern void Foo(int count, string[] pairs);

The C# that corresponds to your example is:

var pairs = new[] { "Hello", "Bob", "Goodbye", "Diane" };
Foo(pairs.Length >> 1, pairs);

I tested this with a mock C library and it works as advertised.

libtest.c:

#include <stdio.h>

void Foo( int count, const char* pairs[][ 2 ] ) {
    int i;

    for (i = 0; i < count; i++) {
        printf("%d: %s\n", i, pairs[i][0]);
        printf("%d: %s\n", i, pairs[i][1]);
    }
}

test.cs:

using System;
using System.Runtime.InteropServices;

public static class Foobar {
    public static void Main() {
        var strings = new[] {
            "The", "quick",
            "brown", "fox",
            "jumped", "over",
            "the", "lazy",
            "dog", "LOL"
        };

        Foo(strings.Length >> 1, strings);
    }

    [DllImport("test")]
    private static extern void Foo(int count, string[] pairs);
}

Output:

0: The
0: quick
1: brown
1: fox
2: jumped
2: over
3: the
3: lazy
4: dog
4: LOL
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文