[Mono-list] need some help with PInvoke..

David Jeske jeske@chat.net
Wed, 9 Jul 2003 00:37:36 -0700

I'm trying to write a PInvoke wrapper and I'm having trouble doing
something pretty basic. Perhaps someone can help me.

The C-function I'm trying to call has this prototype:

  NEOERR* hdf_init (HDF **hdf);

As far as C# is concerned, HDF and NEOERR are just opaque

When hdf_init runs, it allocates some datastructures, and passes back
the pointer through the externally allocated hdf pointer. I just want
to print the address of this structure so I can see that something is
working. Here is a C-program that does what I want..

#include <ClearSilver.h>

int main() {
  HDF *hdf;
  NEOERR *err;

  err = hdf_init(&hdf);

  if (err) {
      printf("error: %s\n", err->desc);
      return 1;

   printf("success: 0x%X\n", hdf);

I can't seem to get this to work through C# and PInvoke with
mono.. (All of this is with Linux. I can't try with csc right now
because I don't have the dll built for windows) My attempt is here:

using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential)] public class HDF {}
[StructLayout(LayoutKind.Sequential)] public class LPHDF {
  //public HDF hdf;
  public int hdf;

public class Hdf {
  // NEOERR* hdf_init (_HDF **hdf);

  [DllImport("libneo.so", EntryPoint="hdf_init")]
  [return: MarshalAs(UnmanagedType.LPStruct)]
  private static extern void hdf_init(
       [ MarshalAs(UnmanagedType.LPStruct)]
         LPHDF hdf);

public static int Main(string[] argv) {
   Console.WriteLine("start test");

    LPHDF lphdf = new LPHDF();

   return 0;


Whenever I run this, it just prints out "0" as the alleged value of
lphdf.hdf. This is no good. Any suggestions?

If you want to poke at the real stuff, you can download libneo.so here:


libneo is built out of the clearsilver package from here:

David Jeske (N9LCA) + http://www.chat.net/~jeske/ + jeske@chat.net