0

次の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#で同じものを実装できますか?mはc#に新しいです。C#でcharポインタ配列を使用するにはどうすればよいですか?

4

5 に答える 5

3

通常、文字列クラスを避けるchar*char[]、または支持します。文字列の配列が必要な場合は、を使用するのではなく、文字の単一のリストが必要な場合は単純なを使用します。char* d[]string[] dstring d

C ++とC#の間の相互運用は常に注意が必要です。いくつかの良い参考資料には、C#文字列をC ++に渡し、C ++の結果(文字列、char * ..何でも)をC#に渡すこと、およびC#でCDLLを使用して配列とポインターを使用することが含まれます。

于 2012-09-20T09:28:56.180 に答える
0

まず、「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);
}
于 2012-09-20T09:44:19.460 に答える
0

Astringは文字のリストです。文字操作とループの使用についての言及から、1つのリスト/配列から特定の文字をターゲットにすることに関心があると思います-この意味で、 (配列stringであるかのように)から特定の文字を問い合わせるときにほぼ同じようにコーディングできますchar)。

例えば:

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

この場合、testCharは「l」と等しくなります。

于 2012-09-20T09:33:40.737 に答える
0

これは役立つはずですが、出力バッファのサイズは固定されているため、動的サイズの文字列では機能しませんが、事前にサイズを知っておく必要があります。

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#で固定ポインターを使用できる唯一の方法です(プロジェクト、プロパティ、ビルドを右クリックして、安全でないコードを許可する)。

次回はより具体的かつ明確にし、ここの人々に対してより敬意を持って行動するようにしてください、私たちはあなたが知っているのを助けるために報酬を得ていません:-)

于 2012-09-20T10:50:14.873 に答える
0

私たちはそれをすることができます

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);

        }
于 2012-09-21T11:17:56.560 に答える