【发布时间】:2010-12-08 16:43:21
【问题描述】:
我的问题与尝试从 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)
任何帮助将不胜感激!
【问题讨论】:
-
如果它是句柄数组,您需要将其设为
IntPtr数组。 -
见pinvoke.net/index.aspx 有很多例子。
标签: c# c++ interop pinvoke marshalling