簡體   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