如何将 C 数组映射到 C#?

发布于 2024-10-06 19:58:37 字数 1024 浏览 6 评论 0原文

我的问题与尝试从 C# 调用用 C 编写的函数有关。我查看了 C 库附带的头文件,以了解 C dll 中存在的函数。这是我看到的:

C 代码(对于名为“LocGetLocations”的函数):

typedef enum {
    eLocNoError,
    eLocInvalidCriteria,
    eLocNoMatch, 
    eLocNoMoreLocations,
    eLocConnectionError, 
    eLocContextError,
    eLocMemoryError
} tLocGetStatus; 

typedef void *tLocFindCtx;

typedef void *tLocation;    

PREFIX unsigned int POSTFIX LocGetLocations
(
    tLocFindCtx pCtx, 
    tLocation *pLoc,
    unsigned int pNumLocations,
    tLocGetStatus *pStatus
);

在 C# 中,我有这样的:

[DllImport(@"VertexNative\Location.dll")]
public static extern uint LocGetLocations(IntPtr findContext, out byte[] locations, uint numberLocations, out int status);

问题是我不太知道如何处理 C# 中的 pLoc 参数。我将其作为字节数组传递,尽管我不确定这是否正确。 C 库的文档说该参数是指向句柄数组的指针。

如何在 C# 端获取数组并访问其数据?

我在 C 语言中给出的示例如下所示:

tLocation lLocation[20];

// other stuff

LocGetLocations(lCtx, lLocation, 20, &lStatus)

任何帮助将不胜感激!

My question has to do with trying to call a function written in C from C#. I've looked in a header file that came with the C library to understand the functions as they exist in the C dll. Here's what I see:

C code (for a function called "LocGetLocations"):

typedef enum {
    eLocNoError,
    eLocInvalidCriteria,
    eLocNoMatch, 
    eLocNoMoreLocations,
    eLocConnectionError, 
    eLocContextError,
    eLocMemoryError
} tLocGetStatus; 

typedef void *tLocFindCtx;

typedef void *tLocation;    

PREFIX unsigned int POSTFIX LocGetLocations
(
    tLocFindCtx pCtx, 
    tLocation *pLoc,
    unsigned int pNumLocations,
    tLocGetStatus *pStatus
);

In C#, I have this:

[DllImport(@"VertexNative\Location.dll")]
public static extern uint LocGetLocations(IntPtr findContext, out byte[] locations, uint numberLocations, out int status);

The problem is that I don't quite know how to handle the pLoc parameter in C#. I'm bringing it over as a byte array, although I'm not sure if that is correct. The C library's documentation says that that parameter is a pointer to an array of handles.

How can I get an array back on the C# side and access its data?

The example I was given in C, looks like this:

tLocation lLocation[20];

// other stuff

LocGetLocations(lCtx, lLocation, 20, &lStatus)

Any help would be much appreciated!

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

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

发布评论

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

评论(2

我爱人 2024-10-13 19:58:37

一般来说,唯一重要的是参数的大小。我记得枚举在 C 中是整数,所以你可以简单地使用它。或者更好的是,在 C# 中重新创建相同的枚举,我认为它会起作用。要记住的一件事是,在处理复杂结构时,需要使用属性来告诉框架所需的成员对齐方式。

Generally, the only thing that matters is the size of the parameters. As I recall enums are integers in C, so you can simply use that. Or better, recreate the same enum in C#, I think it would work. One thing to remember is that when dealing with complex structs, one needs to use attributes to tell the framework about the desired alignment of members.

云醉月微眠 2024-10-13 19:58:37

最后,这个签名有效:

[DllImport(@"VertexNative\Location.dll")]
public static extern uint LocGetLocations(IntPtr findContext, [Out] IntPtr[] locations, uint numberLocations, out int status);

我可以这样称呼它(需要一些重构):

IntPtr[] locations = new IntPtr[20];
int status;
// findContext is gotten from another method invocation
uint result = GeoCodesNative.LocGetLocations(findContext, locations, 20, out status);

感谢您的帮助!

In the end, this signature works:

[DllImport(@"VertexNative\Location.dll")]
public static extern uint LocGetLocations(IntPtr findContext, [Out] IntPtr[] locations, uint numberLocations, out int status);

And I can call it like this (some refactoring needed):

IntPtr[] locations = new IntPtr[20];
int status;
// findContext is gotten from another method invocation
uint result = GeoCodesNative.LocGetLocations(findContext, locations, 20, out status);

Thanks for the help!

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