0

consider the following c++ code

#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;
    }

and below part is in c++

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

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

    return 0;
}

how can i implement the same in c#.. m new to c#. how can i have char pointer array in c#?

Balwinder SIngh
  • 1,831
  • 2
  • 24
  • 28

5 Answers5

3

You usually avoid char* or char[] in favor of the string class. Rather than having a char* d[], you would have a string[] d instead, if you want an array of strings, or a simple string d if you want a single list of characters.

Interop between C++ and C# is always tricky. Some good references include Pass C# string to C++ and pass C++ result (string, char*.. whatever) to C# and Using arrays and pointers in C# with C DLL.

Community
  • 1
  • 1
akton
  • 14,148
  • 3
  • 43
  • 47
  • 1
    Surely string would replace char[], as it is after all an array of characters? I may very well be wrong by the way, I'm just asking. –  Sep 20 '12 at 09:29
  • @DeeMac It depends on whether james wants a list of strings or a single string. I have clarified the answer appropriately. – akton Sep 20 '12 at 09:31
  • i want list of all the content present in array – Balwinder SIngh Sep 20 '12 at 09:32
  • @James Are you trying to pass the `char*d[]` to a C# method (as in C++ to C# interop) or just implementing a similar concept in C#? – akton Sep 20 '12 at 09:34
  • yeah m trying to pass chaar*d[] from c++ dll to c# – Balwinder SIngh Sep 20 '12 at 09:36
  • 2
    @James Please restate the question specifically stating that. The question reads (to me) like you are trying to represent the same concept in C#. – akton Sep 20 '12 at 09:40
0

A string is a list of characters. With your mention of character manipulation and your use of loops I'm assuming your concern is with targetting particular characters from one list/array - and in this sense you can code almost identically when interrogating particular characters from a string (as if it were a char array).

For example:

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

testChar, in this case, will be equal to 'l'.

0

Firstly, your "C++" code is actually C and bad C at that- it won't execute correctly at all. sizeof(b) will not give you the size of the array or anything like it, it will give you the size of a pointer. Consider writing some correct C++ before attempting to convert to 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# doesn't have statically sized arrays, but the rest is easily done

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);
}
Puppy
  • 144,682
  • 38
  • 256
  • 465
0

This should help you, although note that the size of the output buffer is fixed so this won't work for dynamic sized strings, you need to know the size beforehand.

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

Remember to allow unsafe code, since that's the only way you can use fixed pointers in C# (Right-click project, properties, build, allow unsafe code).

Next time be more specific and clear, and try to act more respectfully towards people here, we're not getting paid to help you you know :-)

Shahin Dohan
  • 6,149
  • 3
  • 41
  • 58
0

we can do it as

in DLL we will have

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

        }
Balwinder SIngh
  • 1,831
  • 2
  • 24
  • 28