I am having difficulty with a C# wrapper for some unmanaged C code that allocates a list.  Unmanaged code snippet:
   typedef struct CupsPrinterListStruct
   {
       char printerUri[1024];
       char printerCupsUri[1024];
       char printerName[1024];
       char printerMakeAndModel[256];
       struct CupsPrinterListStruct *nextPtr;
   }CupsPrinterList;
   int ListLocalPrinters(CupsPrinterList **printerList);
   int FreeLocalPrinterList(CupsPrinterList *listHead);
 
Wrapper code:
   [ StructLayout(LayoutKind.Sequential) ]
      public struct PrinterList
      {
         [ MarshalAs(UnmanagedType.ByValTStr, SizeConst=1024) ]
         public string printerUri;
         [ MarshalAs(UnmanagedType.ByValTStr, SizeConst=1024) ]
         public string printerCupsUri;
         [ MarshalAs(UnmanagedType.ByValTStr, SizeConst=1024) ]
         public string printerName;
         [ MarshalAs(UnmanagedType.ByValTStr, SizeConst=256) ]
         public string printerMakeModel;
         public IntPtr nextElement;
    }
   public class PrintLibWrapper
   {
      [ DllImport("print") ]
      public static extern int ListLocalPrinters( ref PrinterList pl);
   }
 
Usage:
   PrinterList printers = new PrinterList();
   printers.nextElement = IntPtr.Zero;
   int ccode = PrintLibWrapper.ListLocalPrinters(ref printers);
   Console.WriteLine("printer name = " + printers.printerName); // printers.printerName is empty
 
I can successfully call ListLocalPrinters but unable to read even 1 result let alone iterate through the list.  Seems I need another level of indirection (perhaps an IntPtr) when calling PrintLibWrapper.ListLocalPrinters().  Didn't try this as I could not figure out how to describe "unmarshalling" of the returned data.   Any pointers to additional info/examples would be appreciated.  Was hoping to find an example in gtk-sharp (List.cs, ListBase.cs) but no marshalling of memory allocated by unmanaged code found there.
 
Thanks.
Jim

Reply via email to