[Mono-dev] Calling unmanaged dll from C#

romyd misc romydmisc at gmail.com
Mon Jun 19 16:41:33 EDT 2006


I tried with CharSet = CharSet.Unicode, i again got blank array. I
looked up online and found this link
http://lists.ximian.com/pipermail/mono-bugs/2003-March/004256.html
  which mentions about a bug in mono, but i don't know where to find
if someone closed this bug?

Thanks,
Romy

On 6/19/06, Rafael Teixeira <monoman at gmail.com> wrote:
> Did you try to use CharSet = CharSet.Unicode ? Because the
> CharSet.Auto setting is target OS dependent (Monodoc says "Specifies
> that strings will be automatically marshaled in the character set
> appropriate for the target system (either Unicode or ANSI).")
>
> And AFAIK, that is ANSI (in reality UTF-8) for Linux/Mono, so I think
> you have to be explicit that you are returning wide char strings, as
> that is not the norm on Linux and other non-MS OSes.
>
> Hope it helps,
>
> On 6/19/06, romyd misc <romydmisc at gmail.com> wrote:
> > 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
> > _______________________________________________
> > Mono-devel-list mailing list
> > Mono-devel-list at lists.ximian.com
> > http://lists.ximian.com/mailman/listinfo/mono-devel-list
> >
>
>
> --
> Rafael "Monoman" Teixeira
> ---------------------------------------
> "The reasonable man adapts himself to the world; the unreasonable one
> persists in trying to adapt the world to himself. Therefore all
> progress depends on the unreasonable man." George Bernard Shaw
>



More information about the Mono-devel-list mailing list