设置打印机

void WINAPI PrintSet(LPCTSTR PrinterName, DWORD PaperSize, DWORD Height, DWORD Width, LPDWORD ret_code, LPTSTR errortext) { DEVMODE* lv_devmode; DEVMODE* lv_devmode_2; PRINTER_INFO_2* lv_printer_info; LPTSTR lv_str, pDeviceName; HANDLE phPrinter; DWORD pcbNeeded, lv_dword; lv_printer_info = malloc( 500 ); if (!OpenPrinter(PrinterName, &phPrinter, NULL)) { free(lv_printer_info); *ret_code = GetLastError(); lv_str = "打开打印机失败 !"; strcpy(errortext, lv_str); return; } if (!GetPrinter(phPrinter, 2, lv_printer_info, 500, &pcbNeeded )) { free(lv_printer_info); *ret_code = GetLastError(); ClosePrinter(phPrinter); lv_str = "无法得到打印机参数 !"; strcpy(errortext, lv_str); return; } lv_devmode = lv_printer_info->pDevMode; pDeviceName = lv_devmode->dmDeviceName; lv_dword = DocumentProperties(0, phPrinter, pDeviceName, lv_devmode, 0, DM_OUT_BUFFER); if (lv_dword<0) { free(lv_printer_info); *ret_code = GetLastError(); ClosePrinter(phPrinter); lv_str = "无法取得打印机参数 !"; strcpy(errortext, lv_str); return; } // 修改DEVMODE结构, 设置纸张大小及其高度和宽度 lv_devmode->dmFields = lv_devmode->dmFields|DM_ORIENTATION|DM_PAPERLENGTH|DM_PAPERWIDTH|DM_PAPERSIZE; lv_devmode->dmOrientation = DMORIENT_PORTRAIT ; lv_devmode->dmPaperSize = PaperSize ; lv_devmode->dmPaperLength = Height; lv_devmode->dmPaperWidth = Width; // 通过调用DOCUMENTPROPERTIES函数传会修改的DEVMODE结构, // 在调用时指定DM_IN_BUFFER|DM_OUT_BUFFER lv_devmode_2 = malloc(500); lv_dword = DocumentProperties(0, phPrinter, pDeviceName, lv_devmode_2, lv_devmode, DM_IN_BUFFER|DM_OUT_BUFFER); if (lv_dword<0) { free(lv_devmode_2); free(lv_printer_info); *ret_code = GetLastError(); ClosePrinter(phPrinter); lv_str = "无法设置打印机参数 !"; strcpy(errortext, lv_str); return; } if (!SetPrinter(phPrinter, 2, lv_printer_info, NULL)) { free(lv_printer_info); *ret_code = GetLastError(); ClosePrinter(phPrinter); lv_str = "无法设置打印机参数 !"; strcpy(errortext, lv_str); return; } free(lv_devmode_2); free(lv_printer_info); ClosePrinter(phPrinter); lv_str = "设置打印机参数成功 !"; strcpy(errortext, lv_str); *ret_code = 1; return ; } 

你可能感兴趣的:(null,buffer,winapi)