[Mono-list] Memory Allocation in unmanaged code
Jim Fehlig
jfehlig@novell.com
Fri, 03 Sep 2004 18:10:32 -0600
This is a MIME message. If you are reading this text, you may want to
consider changing to a mail reader or gateway that understands how to
properly handle MIME multipart messages.
--=__PartFFDF8AE8.0__=
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
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
--=__PartFFDF8AE8.0__=
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<html>
<head>
<style type=3D"text/css">
<!--
body { font-variant: normal; margin-right: 4px; margin-bottom: =
1px; margin-top: 4px; margin-left: 4px; line-height: normal }
-->
</style>
=20
</head>
<body>
<DIV>
I am having difficulty with a C# wrapper for some unmanaged C code =
that allocates a list. Unmanaged code snippet:
</DIV>
<DIV>
typedef struct CupsPrinterListStruct
</DIV>
<DIV>
{
</DIV>
<DIV>
char printerUri[1024];
</DIV>
<DIV>
char printerCupsUri[1024];
</DIV>
<DIV>
char printerName[1024];
</DIV>
<DIV>
char printerMakeAndModel[25=
6];
</DIV>
<DIV>
struct CupsPrinterListStruc=
t *nextPtr;
</DIV>
<DIV>
}CupsPrinterList;
</DIV>
<DIV>
int ListLocalPrinters(CupsPrinterList **printerList=
);
</DIV>
<DIV>
int FreeLocalPrinterList(CupsPrinterList *listHead)=
;
</DIV>
<DIV> </DIV>
<DIV>
Wrapper code:
</DIV>
<DIV>
[ StructLayout(LayoutKind.Sequential) ]
</DIV>
<DIV>
public struct PrinterList
</DIV>
<DIV>
{
</DIV>
<DIV>
[ MarshalAs(Unm=
anagedType.ByValTStr, SizeConst=3D1024) ]
</DIV>
<DIV>
public string =
printerUri;
</DIV>
<DIV>
[ MarshalAs(Unm=
anagedType.ByValTStr, SizeConst=3D1024) ]
</DIV>
<DIV>
public string =
printerCupsUri;
</DIV>
<DIV>
[ MarshalAs(Unm=
anagedType.ByValTStr, SizeConst=3D1024) ]
</DIV>
<DIV>
public string =
printerName;
</DIV>
<DIV>
[ MarshalAs(Unm=
anagedType.ByValTStr, SizeConst=3D256) ]
</DIV>
<DIV>
public string =
printerMakeModel;
</DIV>
<DIV>
public IntPtr =
nextElement;
</DIV>
<DIV>
}
</DIV>
<DIV>
public class PrintLibWrapper
</DIV>
<DIV>
{
</DIV>
<DIV>
[ DllImport("print") ]
</DIV>
<DIV>
public static extern int =
ListLocalPrinters( ref PrinterList pl);
</DIV>
<DIV>
}
</DIV>
<DIV> </DIV>
<DIV>
Usage:
</DIV>
<DIV>
PrinterList printers =3D new PrinterList();
</DIV>
<DIV>
printers.nextElement =3D IntPtr.Zero;
</DIV>
<DIV>
int ccode =3D PrintLibWrapper.ListLocalPrinters(ref=
printers);
</DIV>
<DIV>
Console.WriteLine("printer name =3D " + =
printers.printerName); // printers.printerName is empty
</DIV>
<DIV> </DIV>
<DIV>
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.ListL=
ocalPrinters(). 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.
</DIV>
<DIV> </DIV>
<DIV>
Thanks.
</DIV>
<DIV>
Jim
</DIV>
</body>
</html>
--=__PartFFDF8AE8.0__=--