void RenamePrinter(const std::tstring& currentPrinterName, const std::tstring& newPrinterName) { HANDLE printer = 0; PRINTER_INFO_2 *printerInfo = 0; DEVMODE *pDevMode = 0; try { // get the printer and it's info structure... PRINTER_DEFAULTS printerDefaults; ZeroMemory(&printerDefaults, sizeof(printerDefaults)); printerDefaults.DesiredAccess = PRINTER_ALL_ACCESS; BOOL result = OpenPrinter((LPTSTR)currentPrinterName.c_str(), &printer, &printerDefaults); if (!result || (0 == printer)) throw Workshare::Exception(_T("Failed to open printer.")); DWORD bufferSize = 0; SetLastError(0); result = GetPrinter(printer, 2, 0, 0, &bufferSize); if ((!result) && (GetLastError() != ERROR_INSUFFICIENT_BUFFER) || (bufferSize == 0)) throw Workshare::Exception(_T("Failed to get printer info buffer size.")); printerInfo = (PRINTER_INFO_2 *)GlobalAlloc(GPTR, bufferSize); if (0 == printerInfo) throw Workshare::System::SystemException(_T("Failed to allocate printer info buffer.")); result = GetPrinter(printer, 2, (LPBYTE)printerInfo, bufferSize, &bufferSize); if (!result) throw Workshare::Exception(_T("Failed to get printer info.")); // If GetPrinter didn't fill in the DEVMODE, try to get it by calling DocumentProperties... LONG lFlag; if (0 == printerInfo->pDevMode) { bufferSize = DocumentProperties(0, printer, (LPTSTR)currentPrinterName.c_str(), 0, 0, 0); if (bufferSize <= 0) throw Workshare::Exception(_T("Failed to get document properties buffer size.")); pDevMode = (DEVMODE *)GlobalAlloc(GPTR, bufferSize); if (0 == pDevMode) throw Workshare::System::SystemException(_T("Failed to allocate document properties buffer.")); lFlag = DocumentProperties(0, printer, (LPTSTR)currentPrinterName.c_str(), pDevMode, 0, DM_OUT_BUFFER); if (lFlag != IDOK || 0 == pDevMode) throw Workshare::Exception(_T("Failed to get document properties.")); printerInfo->pDevMode = pDevMode; } if (!(printerInfo->pDevMode->dmFields & DM_ORIENTATION)) throw Workshare::Exception(_T("Printer does not support the required name change.")); // Specify the new printer name... printerInfo->pPrinterName = (LPTSTR)newPrinterName.c_str() ; // Do not attempt to set security descriptor... printerInfo->pSecurityDescriptor = 0; lFlag = DocumentProperties(0, printer, (LPTSTR)currentPrinterName.c_str(), printerInfo->pDevMode, printerInfo->pDevMode, DM_IN_BUFFER | DM_OUT_BUFFER); if (IDOK != lFlag) throw Workshare::Exception(_T("Failed to update the devmode part of document properties.")); result = SetPrinter(printer, 2, (LPBYTE)printerInfo, 0); if (!result) throw Workshare::Exception(_T("Failed to update printer information.")); // Tell other apps that there was a change... SendMessageTimeout(HWND_BROADCAST, WM_DEVMODECHANGE, 0L, (LPARAM)(LPCTSTR)(LPTSTR)currentPrinterName.c_str(), SMTO_NORMAL, 1000, 0); return; } catch(...) { if (printerInfo) GlobalFree(printerInfo); if (printer) ClosePrinter(printer); if (pDevMode) GlobalFree(pDevMode); throw; } }
static HB_BOOL hb_SetDefaultPrinter( LPCTSTR lpPrinterName ) { #if ! defined( HB_OS_WIN_CE ) BOOL bFlag; DWORD dwNeeded = 0; HANDLE hPrinter = NULL; PRINTER_INFO_2 * ppi2 = NULL; LPTSTR pBuffer = NULL; /* If Windows 95 or 98, use SetPrinter. */ if( hb_iswin9x() ) { /* Open this printer so you can get information about it. */ bFlag = OpenPrinter( ( LPTSTR ) lpPrinterName, &hPrinter, NULL ); if( ! bFlag || ! hPrinter ) return HB_FALSE; /* The first GetPrinter() tells you how big our buffer must be to hold ALL of PRINTER_INFO_2. Note that this will typically return FALSE. This only means that the buffer (the 3rd parameter) was not filled in. You do not want it filled in here. */ SetLastError( 0 ); bFlag = GetPrinter( hPrinter, 2, 0, 0, &dwNeeded ); if( ! bFlag ) { if( ( GetLastError() != ERROR_INSUFFICIENT_BUFFER ) || ( dwNeeded == 0 ) ) { ClosePrinter( hPrinter ); return HB_FALSE; } } /* Allocate enough space for PRINTER_INFO_2. */ ppi2 = ( PRINTER_INFO_2 * ) hb_xgrab( dwNeeded ); /* The second GetPrinter() will fill in all the current information so that all you have to do is modify what you are interested in. */ bFlag = GetPrinter( hPrinter, 2, ( LPBYTE ) ppi2, dwNeeded, &dwNeeded ); if( ! bFlag ) { ClosePrinter( hPrinter ); hb_xfree( ppi2 ); return HB_FALSE; } /* Set default printer attribute for this printer. */ ppi2->Attributes |= PRINTER_ATTRIBUTE_DEFAULT; bFlag = SetPrinter( hPrinter, 2, ( LPBYTE ) ppi2, 0 ); if( ! bFlag ) { ClosePrinter( hPrinter ); hb_xfree( ppi2 ); return HB_FALSE; } /* Tell all open programs that this change occurred. Allow each program 1 second to handle this message. */ SendMessageTimeout( HWND_BROADCAST, WM_SETTINGCHANGE, 0L, ( LPARAM ) ( LPCTSTR ) TEXT( "windows" ), SMTO_NORMAL, 1000, NULL ); } /* If Windows NT, use the SetDefaultPrinter API for Windows 2000, or WriteProfileString for version 4.0 and earlier. */ else if( hb_iswinnt() ) { if( hb_iswin2k() ) /* Windows 2000 or later (use explicit call) */ { HMODULE hWinSpool; typedef BOOL ( WINAPI * DEFPRINTER )( LPCTSTR ); /* stops warnings */ DEFPRINTER fnSetDefaultPrinter; hWinSpool = hbwapi_LoadLibrarySystem( TEXT( "winspool.drv" ) ); if( ! hWinSpool ) return HB_FALSE; fnSetDefaultPrinter = ( DEFPRINTER ) HB_WINAPI_GETPROCADDRESST( hWinSpool, "SetDefaultPrinter" ); if( ! fnSetDefaultPrinter ) { FreeLibrary( hWinSpool ); return HB_FALSE; } bFlag = ( *fnSetDefaultPrinter )( lpPrinterName ); FreeLibrary( hWinSpool ); if( ! bFlag ) return HB_FALSE; } else /* NT4.0 or earlier */ { HB_ISIZ nStrLen; /* Open this printer so you can get information about it. */ bFlag = OpenPrinter( ( LPTSTR ) lpPrinterName, &hPrinter, NULL ); if( ! bFlag || ! hPrinter ) return HB_FALSE; /* The first GetPrinter() tells you how big our buffer must be to hold ALL of PRINTER_INFO_2. Note that this will typically return FALSE. This only means that the buffer (the 3rd parameter) was not filled in. You do not want it filled in here. */ SetLastError( 0 ); bFlag = GetPrinter( hPrinter, 2, 0, 0, &dwNeeded ); if( ! bFlag ) { if( ( GetLastError() != ERROR_INSUFFICIENT_BUFFER ) || ( dwNeeded == 0 ) ) { ClosePrinter( hPrinter ); return HB_FALSE; } } /* Allocate enough space for PRINTER_INFO_2. */ ppi2 = ( PRINTER_INFO_2 * ) hb_xgrab( dwNeeded ); /* The second GetPrinter() fills in all the current information. */ bFlag = GetPrinter( hPrinter, 2, ( LPBYTE ) ppi2, dwNeeded, &dwNeeded ); if( ( ! bFlag ) || ( ! ppi2->pDriverName ) || ( ! ppi2->pPortName ) ) { ClosePrinter( hPrinter ); hb_xfree( ppi2 ); return HB_FALSE; } nStrLen = hbwapi_tstrlen( lpPrinterName ) + hbwapi_tstrlen( ppi2->pDriverName ) + hbwapi_tstrlen( ppi2->pPortName ) + 2; /* Allocate buffer big enough for concatenated string. String will be in form "printername,drivername,portname". */ pBuffer = ( LPTSTR ) hb_xgrab( ( nStrLen + 1 ) * sizeof( TCHAR ) ); pBuffer[ 0 ] = TEXT( '\0' ); /* Build string in form "printername,drivername,portname". */ hbwapi_tstrncat( pBuffer, lpPrinterName, nStrLen ); hbwapi_tstrncat( pBuffer, TEXT( "," ), nStrLen ); hbwapi_tstrncat( pBuffer, ppi2->pDriverName, nStrLen ); hbwapi_tstrncat( pBuffer, TEXT( "," ), nStrLen ); hbwapi_tstrncat( pBuffer, ppi2->pPortName, nStrLen ); /* Set the default printer in win.ini and registry. */ bFlag = WriteProfileString( TEXT( "windows" ), TEXT( "device" ), pBuffer ); if( ! bFlag ) { ClosePrinter( hPrinter ); hb_xfree( ppi2 ); hb_xfree( pBuffer ); return HB_FALSE; } } /* Tell all open programs that this change occurred. Allow each app 1 second to handle this message. */ SendMessageTimeout( HWND_BROADCAST, WM_SETTINGCHANGE, 0L, 0L, SMTO_NORMAL, 1000, NULL ); } /* Clean up. */ if( hPrinter ) ClosePrinter( hPrinter ); if( ppi2 ) hb_xfree( ppi2 ); if( pBuffer ) hb_xfree( pBuffer ); return HB_TRUE; #else HB_SYMBOL_UNUSED( lpPrinterName ); return HB_FALSE; #endif }
static void InitPrintCut(void) { HWND Window,Mid; int xw,yw,i,j; char p[40]; int SaveColor; struct viewporttype SaveViewPort; BEGINOK; //WaitMessageEmpty(); MouseHidden(); getviewsettings(&SaveViewPort); SaveColor=getcolor(); SetPrinter(-CurrentPrinter); Window=PrintCutWin[wDISPWIN]; Mid=WindowGetFather(Window); ScreenDispWidth=WindowGetWidth(Window)-10; ScreenDispHeight=WindowGetHeight(Window)-10; WinX=ScreenX=WindowGetLeft(Window)+WindowGetLeft(Mid); WinY=ScreenY=WindowGetTop(Window)+WindowGetTop(Mid); setviewport(WinX,WinY,WinX+ScreenDispWidth+9,WinY+ScreenDispHeight+9,1); setwritemode(COPY_PUT); setcolor(EGA_WHITE); //setcolor(11); bar(0,0,ScreenDispWidth+9,ScreenDispHeight+9); PaperW=(float)(printer->xpixel)*25.4/PrinterDPI; PaperH=(float)(printer->ypixel)*25.4/PrinterDPI; SCRscaleX=PaperW/ScreenDispWidth; SCRscaleY=PaperH/ScreenDispHeight; if (SCRscaleX>SCRscaleY) SCRscaleY=SCRscaleX; else SCRscaleX=SCRscaleY; xw=mXw=PaperW/SCRscaleX; yw=mYw=PaperH/SCRscaleY; ScreenX=ScreenX+5+(ScreenDispWidth-mXw)/2; ScreenY=ScreenY+5+(ScreenDispHeight-mYw)/2; //setwritemode(COPY_PUT); setcolor(EGA_DARKGRAY); //gray setviewport(0,0,getmaxx(),getmaxy(),1); rectangle(ScreenX-1,ScreenY-1,ScreenX+xw,ScreenY+yw); //setviewport(ScreenX-1,ScreenY-1,ScreenX+xw,ScreenY+yw,1); //rectangle(ScreenX-1,ScreenY-1,ScreenX+xw,ScreenY+yw); //setviewport(ScreenX,ScreenY,ScreenX+mXw-1,ScreenY+mYw-1,1); GetUserFrame(0,&xw,&yw); PageWI=xw; PageHI=yw; PageW=xw*25.4/1000; PageH=yw*25.4/1000; sprintf(p,"%4d",(int)(PaperW+.5)); GetXY(3,&i,&j); DispXY(i-10,j,p,COLORP); sprintf(p,"%4d",(int)(PaperH+.5)); GetXY(4,&i,&j); DispXY(i-10,j,p,COLORP); setwritemode(COPY_PUT); setcolor(SaveColor); setviewport(SaveViewPort.left,SaveViewPort.top,SaveViewPort.right, SaveViewPort.bottom,SaveViewPort.clip); MouseShow(); }