繁体   English   中英

将c ++ dLL的char指针数组传递给c#字符串

[英]passing char pointer array of c++ dLL to c# string

考虑以下C ++代码

#include "stdafx.h"
    #include<iostream>
    using namespace std;


this much part i want in c#..

void ping(int,char* d[]);

    void ping(int a,char *b[])
    {
    int size;
    size=sizeof(b)/sizeof(int); // total size of array/size of array data type

    //cout<<size; 
        for(int i=0;i<=size;i++)
        cout<<"ping "<<a<<b[i]<<endl;
    }

下面是C ++

int _tmain(int argc, _TCHAR* argv[])
{
    void (*funcptr)(int,char* d[]);

    char* c[]={"a","b"};
    funcptr= ping;
    funcptr(10,c);

    return 0;
}

我如何在C#中实现相同的功能? 我怎样才能在C#中使用char指针数组?

您通常避免使用char*char[]来支持字符串类。 如果要使用字符串数组,则应使用string[] d而不是char* d[] ,如果要使用单个字符列表,则可以使用简单的string d

C ++和C#之间的互操作总是很棘手。 一些好的参考资料包括将C#字符串传递给C ++以及将C ++结果(字符串,char * ..等等)传递给C#以及在带有C DLL的C#中使用数组和指针

string是字符列表。 提到字符操作和使用循环时,我假设您的关注点是针对一个列表/数组中的特定字符-从这个意义上说,当您查询string特定字符时,您可以几乎相同地编码(就像它是一个string一样)。 char数组)。

例如:

string testString = "hello";
char testChar = testString[2];

在这种情况下,testChar将等于“ l”。

首先,您的“ C ++”代码实际上是C,而实际上是C,这根本无法正确执行。 sizeof(b) 不会给您数组或类似数组的大小,它会给您指针的大小。 在尝试转换为C#之前,请考虑编写一些正确的C ++。

template<int N> void ping(int a, std::array<std::string, N>& arr) {
    for(int i = 0; i < N; i++) std::cout << a <<  arr[i] << "\n";
}
int _tmain(int argc, _TCHAR* argv[]) {
     std::array<std::string, 2> c = { "a", "b" };
     ping(10, c);
    return 0;
}

C#没有静态大小的数组,但是其余的很容易完成

public static void ping(int a, string[] arr) {
    for(int i = 0; i < arr.Length; i++) {
         System.Console.Write(a);
         System.Console.Write(arr[i]);
    }
}
public static void Main(string[] args) {
    string[] arr = { "a", "b" };
    ping(10, arr);
}

这应该对您有帮助,尽管请注意输出缓冲区的大小是固定的,因此这不适用于动态大小的字符串,但您需要事先知道大小。

public unsafe static void Foo(char*[] input)
{
    foreach(var cptr in input)
    {
        IntPtr ptr = new IntPtr(cptr);
        char[] output = new char[5]; //NOTE: Size is fixed
        Marshal.Copy(ptr, output, 0, output.Length);

        Console.WriteLine(new string(output));
    }
}

请记住要允许使用不安全的代码 ,因为这是在C#中使用固定指针的唯一方法(右键单击项目,属性,构建,允许使用不安全的代码)。

下次,请更加明确和明确,并尝试对此处的人员采取更加尊重的态度,我们不会获得报酬来帮助您:-)

我们可以做到

在DLL中,我们将拥有

    extern "C" __declspec(dllexport) void  __stdcall Caller() 
        { 


        static char* myArray[3];

        myArray[0]="aasdasdasdasdas8888";

        myArray[1]="sssss";


        FunctionPtr1(2,myArray);


    } 





and in C# i just added following lines

 public static void ping(int a, IntPtr x)
        {

            Console.WriteLine("Entered Ping Command");
              //code to fetch char pointer array from DLL starts here
              IntPtr c = x;
               int j = 0;
               string[] s = new string[100]; //I want this to be dynamic 
               Console.WriteLine("content of array");
               Console.WriteLine("");

            do
            {
                s[j] = Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(c, 4 * j));
                Console.WriteLine(s[j]);
                j++;

            } while (s[j - 1] != null);
            //**********end****************

            Console.WriteLine("Integer value received from DLL is "+a);

        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM