[Mono-dev] Calling unmanaged dll from C#

romyd misc romydmisc at gmail.com
Mon Jun 19 09:49:12 EDT 2006


Hi All,

I want to use DllImport to call a C function that allocates and
returns an array of strings. I'm not sure if this is the right place
to ask this question, but my sample code works with windows .NET
compiler, so either there is a different way to call unmanaged dlls in
mono or may be i'm not implementing it right?

I've a C function that converts a char * to wchar_t *

DllExport  Func(wchar_t** ipadds)
{
char mbBuf[BUF_SIZE] = "1.1.1.1";
char* s = mbBuf;
size_t len = strlen (mbBuf);
wchar_t *result = malloc ((len + 1) * sizeof (wchar_t));
wchar_t *wcp = result;
wchar_t tmp;
mbstate_t state;
size_t nbytes;

int i = 0;
memset (&state, '\0', sizeof (state));
while ((nbytes = mbrtowc (&tmp, s, len, &state)) > 0)
   {
     if (nbytes >= (size_t) -2)
       /* Invalid input string.  */
       return NULL;

       result[i] = tmp;
       i++;
       len -= nbytes;
       s += nbytes;
   }

   result[i] = '\0';
   *ipadds = result;
}

My C# code looks like this:

     [DllImport("KeyServerUsage.dll", CharSet = CharSet.Auto,
EntryPoint = "Func")]
       private static extern void Func([Out] string[] Names);

            public ArrayList GetIPAdd()
            {
                string[] ipadds = new string[2];
                Func(ipadds);
            }


Now i get a blank array returned in ipadds when i run this program
with mono. But when i run the same program on windows, i get "1.1.1.1"
in ipadds[0]. Any help would be greatly appreciated.

Thanks,
Romy



More information about the Mono-devel-list mailing list