5

I have this in my dll created in c++

extern "C" __declspec(dllexport)
    char*  __stdcall hh()
{
    char a[2];
    a[0]='a';
         a[1]='b';
    return(a);
}

And this is how I am trying to handle code in c#

[DllImport(@"mydll.dll",CharSet = CharSet.Ansi,CallingConvention = CallingConvention.StdCall)]     
       public static extern IntPtr hh();
       static void Main(string[] args)
        {
            IntPtr a = hh();
           //How to proceed here???
        }


    }

Help in proceeding further.

2
  • What exactly is your question? Commented Apr 10, 2012 at 8:42
  • I want to print the array returend from c++ code in c# Commented Apr 10, 2012 at 8:45

4 Answers 4

3

There is no way to handle such arrays. char a[2] is allocated on the stack in your C++ function and is destroyed as soon as you return from it. You should either pass an array from C# and fill it in the C++ code or allocate array in the heap and provide some means for freeing it.

When you have it correct the handling will depend on how you return the data from C++ code. If it's still IntPtr you could use Marshal.ReadByte methods to read characters from memory and use Encoding methods to convert those bytes into string if necessary.


const int bufferSize = 2; // suppose it's some well-known value.
IntPtr p = ...; // get this pointer somehow.
for (var i = 0; i != bufferSize; ++i)
{
  var b = Marshal.ReadByte(p, i);
  Console.WriteLine(b);
}

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

Comments

2

I got a solution as follows::

OUR C++ code goes as follows

extern "C" __declspec(dllexport)
    char**  __stdcall hh()
{
 static char* myArray[3] = {"A1", "BB2", "CC3",};
    return myArray;
}

And C# goes as follows

[DllImport(@"ourdll.dll",CharSet = CharSet.Ansi,CallingConvention = CallingConvention.StdCall)]     
      public static extern IntPtr hh();
       static void Main(string[] args)
        {
            IntPtr a = hh();
            int j = 0;
            string[] s=new string[100];
           do
           {
               s[j] = Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(a,4*j));
               j++;
           }
           while(s[j-1] != null);
        }

The only problem now faced is that how can we know size of the array so that in this statement string[] s=new string[100]; we neednot waste our memory.

Comments

1

The answer would be

 string stra = Marshal.PtrToStringAnsi(a);

But you also have the problem that the dll returns garbage per your code as char* is a local c style string. Would be ok if you would return something like:

 const char* str = "Hello from DLL.";

2 Comments

I want to pass array of strings not a single string
Ok. Misunderstood that bit. Hope helps anyway.
-2

Try to use not empty StringBuilder as the return value.

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.