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#?

5
  • This will help: http://msdn.microsoft.com/en-us/library/ezftk57x.aspx Commented Sep 20, 2012 at 9:36
  • sizeof(b)/sizeof(int) - it is not very good C++ code. Don't use it as an example. Commented Sep 20, 2012 at 9:41
  • @HenkHolterman yes you're correct, I was in the wrong way :) Thank you! Commented Sep 20, 2012 at 9:47
  • @HenkHolterman thank you another time Commented Sep 20, 2012 at 10:02
  • -1 - your comments throughout this thread imply a completely different topic to that featured in your OP. Commented Sep 20, 2012 at 10:50

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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.
@DeeMac It depends on whether james wants a list of strings or a single string. I have clarified the answer appropriately.
i want list of all the content present in array
@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#?
@James Please restate the question specifically stating that. The question reads (to me) like you are trying to represent the same concept in C#.
|
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'.

Comments

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

3 Comments

u didnt get my question.. i want copy char *arr[] of c++ to string arr[] of c#
@james: Edit your question then, it's very poorly written.
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 :-)

Comments

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

        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.