omikron1 has asked for the wisdom of the Perl Monks concerning the following question:

hello monks
There is the GetPrinter Win32 api function (which retrieves information on a printer)
that requires a pointer to a PRINTER_INFO_2 structure.

The structure is defined as :
typedef struct _PRINTER_INFO_2 { LPTSTR pServerName; LPTSTR pPrinterName; LPTSTR pShareName; LPTSTR pPortName; LPTSTR pDriverName; LPTSTR pComment; LPTSTR pLocation; LPDEVMODE pDevMode; LPTSTR pSepFile; LPTSTR pPrintProcessor; LPTSTR pDatatype; LPTSTR pParameters; PSECURITY_DESCRIPTOR pSecurityDescriptor; DWORD Attributes; DWORD Priority; DWORD DefaultPriority; DWORD StartTime; DWORD UntilTime; DWORD Status; DWORD cJobs; DWORD AveragePPM; } PRINTER_INFO_2, *PPRINTER_INFO_2;
which I packed it into $printer_info_2 variable as :
$a="\0"; $printer_info_2=pack('PPPPPPPPPPPPPLLLLLLLL',$a,$a,$a,$a,$a,$a,$a,$a,$ +a,$a,$a,$a,$a,0,0,0,0,0,0,0,0);
then I load and call the function :
$GetPrinter=new Win32::API('winspool.drv','GetPrinterA','NNPNP','N'); $GetPrinter->Call($handle,2,$printer_info_2,10000,$bytesgot);
and then unpack as :
@a=unpack('P32P32P32P32P32P32P32P32P32P32P32P32P32LLLLLLLL',$printer_i +nfo_2);

I do get data,but they are mixed or overlap hence they cannot lead to a solid view of the printer's details

data output :
\\http://192.168.4.98\Lexmark T4 \\http://192.168.4.98\Lexmark http://192.168.4.98 . 1 6 8 . 4 Lexmark T430 T 4 3 0 http:/ comments go here g o h e r e comments go here g o h e r \\http://192.168.4.98\Lexmark comments go here g o h e WinPrint i n t comments g RAW W WinPrint i n t co RAW W WinPrint i n t  € \ \ h t t p 2128 1 0 1380 1380 0 0 0
So this raises the question:
Is that the correct way to define and unpack pointers to strings? I don't know the length of the string that is why I use a number like 32.
I tried to pack and unpack using 'p' instead of 'P' but it makes the application crash!
Plus,how do I implement a pointer to a structure,where the pointer itself is part of another structure? (such as LPDEVMODE)
During my research I've seen pointers to strings being packed as 'L' and unpacked as 'p'.But it is all so confusing!

Any answers or any hints to get me started are gladly appreciated!Thanks!

Replies are listed 'Best First'.
Re: Packing a c structure when using the win32api
by vkon (Curate) on Apr 14, 2006 at 11:01 UTC
    Convert::Binary::C seemingly perfectly fits your purposes, yet it is highly efficient and understands all types of C structs;

    In essence, it does corret "unpack"-s, but includes very deep and precise C structure analyzis.

    I did used it, and found it extremely useful!

Re: Packing a c structure when using the win32api
by omikron1 (Initiate) on Apr 15, 2006 at 12:08 UTC
    Thank you! I will give it a try