PaeneInsula
PaeneInsula

Reputation: 2100

modify char array passed to C dll c#

I am simply trying to pass a buffer to a C dll from C#, have the C func fill the buffer in, and then do something with the buffer back in the C# code. But I'm getting garbage back in the buffer. Here's the C:

    extern "C" __declspec( dllexport )      
    int  cFunction(char *plotInfo, int bufferSize) 
    {
        strcpy(plotInfo, "text");
        return(0);
    }

c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
 [DllImport("mcDll.dll",
     CallingConvention = CallingConvention.Cdecl, CharSet=CharSet.Unicode)]

    public static extern int cFunction( StringBuilder theString, int bufferSize);
    static void Main(string[] args)
    {
        StringBuilder s = new StringBuilder(55);
        int result = cFunction( s, 55);
        Console.WriteLine(s);
    }
   }
}

Upvotes: 0

Views: 751

Answers (1)

Dennis
Dennis

Reputation: 37770

Native function operates with ANSI chars. Just remove CharSet.Unicode from your import definition.

Upvotes: 2

Related Questions