gpt4 book ai didi

c# - 如何在 C# 中 P/调用 char* []

转载 作者:行者123 更新时间:2023-11-30 01:17:09 25 4
gpt4 key购买 nike

如何在 C# 中 P/调用 char* []

任何 1 告诉以后如何继续。我想从 C# 向 C++ DLL 发送参数。我用谷歌搜索了很多网站,但没有解决方案。

C 函数

public void* find(char*[] argv)
{
}

C#函数我想用以下参数参数调用这个函数

char *Argv[] = { "Tool", "Sachin", "192.168.1.1", "3", "400"};

提前致谢。

最佳答案

有多种方法可以做到这一点...这里只是几个...但我概述了您需要注意的其他信息。


正确导出/导入“查找”功能

首先,您需要确保“导出的”C 函数和 DllImport 已正确定义(如果您已经在工作,请忽略此部分)。

如果您的 C 函数被编译为使用 cdecl 调用约定(通常是 C/C++ 项目中的默认调用约定),那么您需要使用 CallingConvention = CallingConvention.CdeclDllImport 例如

[DllImport("yourdll.dll", CharSet = Ansi, CallingConvention = CallingConvention.Cdecl)]
public IntPtr find([In] String[] args);

如果您的 C 函数被编译为使用“stdcall”调用约定(通过项目选项或通过放置 WINAPI 宏,或函数定义上的 __stdcall 装饰),那么您需要使用 CallingConvention = CallingConvention.Stdcall (这是 DllImport 上的默认设置)例如

[DllImport("yourdll.dll", CharSet = Ansi)]
public IntPtr find([In] String[] args);

此外,在定义“C”函数时,您可以使用 extern "C" 来阻止 C++ 编译器对函数名称的处理。

然后您可以使用 __declspec(export),或使用 .DEF 文件将函数指定为导出条目。


你“找到”的契约(Contract)是什么?

重要提示:您需要知道“查找”功能的契约(Contract)...即它会寻找什么来表示该参数列表的“结束”...它可能使用 NULL,或者它可能使用空字符串等。

通常,像这样的函数有另一个参数表示数组中项目数的“计数”,因此不需要“标记”。

对于下面的示例,我假设它在最后使用 NULL...如果不是这样,您将不得不相应地修改代码。

如果您的某些参数可能为 NULL,那么您将不得不更改用于表示结束的“哨兵”。


您可以在 C# 和 native 类型之间进行“编码”(使用编码属性):

    // Note: this uses a "NULL" to mark the end of the array...
// because your "find" function doesn't have a "count" parameter, and I'm
// assuming it uses a NULL parameter to detect the end.

IntPtr opaqueresult = find(new string[] { "Tool", "Sachin", "192.168.1.1", "3", "400", null}); // call the function in that DLL.

或者您可以自己执行编码逻辑(根据需要重新排列和扩展代码以清理内存等):

// When need to use IntPtr as we are allocating the native memory.
[DllImport("yourdll.dll", CharSet = Ansi)]
public IntPtr find([In] IntPtr args);

IntPtr []allocatednativestrings;
IntPtr args = AllocateAnsiIntPtrArrayWithSentinel( new string[] { "Tool", "Sachin", "192.168.1.1", "3", "400"}, out allocatednativestrings);

IntPtr opaqueresult = find(args); // call the function in that DLL.

// If "find" DOESN'T hold onto the pointers passed into it, then you can "free"
// the memory just after you make the call....otherwise you can't...you then
// have to decide how/who has responsibility for freeing that memory and when.

// Free the "strings", and the memory containing the pointers
FreeAnsiIntPtrArrayWithSentinel(args, allocatednativestrings);

(Note: I am only using this bit of hacky code, as it was mentioned in a comment above...
http://www.codeproject.com/Articles/17450/Marshal-an-Array-of-Zero-Terminated-Strings-or-Str ....
the crucial thing is to make sure you "free" the memory properly, when you are finished with it....
this can be done neater...just giving you enough to get the idea)

public static IntPtr AllocateAnsiIntPtrArrayWithSentinel(string[] InputStrArray, out IntPtr[] InPointers)
{
int size = InputStrArray.Length + 1; // +1 for NULL sentinel

//build array of pointers to string
InPointers = new IntPtr[size];

int dim = IntPtr.Size * size;

IntPtr rRoot = Marshal.AllocCoTaskMem(dim);

int i = 0;
foreach(string arg in args)
{
if (arg == null)
{
System.Diagnostics.Debug.Assert(false, "this code needs changing to support NULL arguments");
}

InPointers[i++] = Marshal.StringToCoTaskMemAnsi(arg);
}

// The NULL sentinel...don't need to do this as already initialized to null...
// but just making it clearer for you.
InPointers[size-1] = IntPtr.Zero;

//copy the array of pointers
Marshal.Copy(InPointers, 0, rRoot, size);
return rRoot;
}

public static void FreeAnsiIntPtrArrayWithSentinel(IntPtr args, IntPtr[] intptrs)
{
foreach(IntPtr ptr in intptrs)
{
if (ptr != IntPtr.Zero) // we need to avoid the sentinel
Marshal.FreeCoTaskMem(ptr); // free the mem allocated for the string
}

// free the memory that contained the list of string pointers and sentinel
Marshal.FreeCoTaskMem(args);
}

关于c# - 如何在 C# 中 P/调用 char* [],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25137788/

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