data.fileName = output;
if (data.printToFile) data.fileName = output;
public PrinterData open() {	PRINTDLG pd = new PRINTDLG();	pd.lStructSize = PRINTDLG.sizeof;	pd.Flags = OS.PD_USEDEVMODECOPIESANDCOLLATE;	if (printToFile) pd.Flags |= OS.PD_PRINTTOFILE;	switch (scope) {		case PrinterData.PAGE_RANGE: pd.Flags |= OS.PD_PAGENUMS; break;		case PrinterData.SELECTION: pd.Flags |= OS.PD_SELECTION; break;		default: pd.Flags |= OS.PD_ALLPAGES;	}	pd.nMinPage = 1;	pd.nMaxPage = -1;	pd.nFromPage = (short) startPage;	pd.nToPage = (short) endPage;	if (OS.PrintDlg(pd)) {		/* Get driver and device from the DEVNAMES struct */		int size = OS.GlobalSize(pd.hDevNames);		int ptr = OS.GlobalLock(pd.hDevNames);		short[] offsets = new short[4];		OS.MoveMemory(offsets, ptr, 2 * offsets.length);		TCHAR buffer = new TCHAR(0, size);		OS.MoveMemory(buffer, ptr, size);			OS.GlobalUnlock(ptr);		int driverOffset = offsets[0];		int i = 0;		while (driverOffset + i < size) {			if (buffer.tcharAt(driverOffset + i) == 0) break;			else i++;		}		String driver = buffer.toString(driverOffset, i);		int deviceOffset = offsets[1];		i = 0;		while (deviceOffset + i < size) {			if (buffer.tcharAt(deviceOffset + i) == 0) break;			else i++;		}		String device = buffer.toString(deviceOffset, i);			int outputOffset = offsets[2];		i = 0;		while (outputOffset + i < size) {			if (buffer.tcharAt(outputOffset + i) == 0) break;			else i++;		}		String output = buffer.toString(outputOffset, i);				/* Create PrinterData object and set fields from PRINTDLG */		PrinterData data = new PrinterData(driver, device);		if ((pd.Flags & OS.PD_PAGENUMS) != 0) {			data.scope = PrinterData.PAGE_RANGE;			data.startPage = pd.nFromPage;			data.endPage = pd.nToPage;		} else if ((pd.Flags & OS.PD_SELECTION) != 0) {			data.scope = PrinterData.SELECTION;		}		data.printToFile = (pd.Flags & OS.PD_PRINTTOFILE) != 0;		data.fileName = output;		data.copyCount = pd.nCopies;		data.collate = (pd.Flags & OS.PD_COLLATE) != 0;		/* Bulk-save the printer-specific settings in the DEVMODE struct */		ptr = OS.GlobalLock(pd.hDevMode);		size = OS.GlobalSize(ptr);		data.otherData = new byte[size];		OS.MoveMemory(data.otherData, ptr, size);		OS.GlobalUnlock(ptr);		return data;	}	return null;}