[Mono-dev] Calling unmanaged dll from C#

Martin Baulig martin at ximian.com
Thu Jun 22 12:09:02 EDT 2006


Hello,

I'm currently using this in the Debugger:

[DllImport("monodebuggerserver")]
> static extern TargetError mono_debugger_server_get_application (IntPtr handle, out string exe_file,
>                                                                 out string cwd, out int nargs, out IntPtr data);

On the unmanaged side, I declared the function as:

> static ServerCommandError
> server_ptrace_get_application (ServerHandle *handle, gchar **exe_file, gchar **cwd,
>                                guint32 *nargs, gchar **cmdline_args)
> {
>     gchar **ptr;
>     GPtrArray *array;
>     ...
>     cwd = g_strdup (...);
>     ...
>     *cmdline_args = ptr = g_new0 (gchar *, array->len + 1);
> 
>     for (i = 0; i < array->len; i++)
>             ptr  [i] = g_ptr_array_index (array, i);
> 
>     ...
> }

and on the C# side:

> protected string GetApplication (out string cwd, out string[] cmdline_args)
> {
>     IntPtr data = IntPtr.Zero;
>     try {
>         int count;
>         string exe_file;
>         check_error (mono_debugger_server_get_application (
>             server_handle, out exe_file, out cwd, out count, out data));
> 
>         cmdline_args = new string [count];
> 
>         for (int i = 0; i < count; i++) {
>             IntPtr ptr = Marshal.ReadIntPtr (data, i * IntPtr.Size);
>             cmdline_args [i] = Marshal.PtrToStringAuto (ptr);
>         }
> 
>         return exe_file;
>     } finally {
>         g_free (data);
>     }
> }

Of course, that doesn't handle unicode - the returned strings are all
ASCII-clean.

Martin

On Mon, 2006-06-19 at 09:49 -0400, romyd misc 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
> 




More information about the Mono-devel-list mailing list