Win98banter

Win98banter (http://www.win98banter.com/index.php)
-   Printing (http://www.win98banter.com/forumdisplay.php?f=13)
-   -   SetPrinter() requires reboot. I need an alternative method. (http://www.win98banter.com/showthread.php?t=29820)

#2pencil July 25th 05 06:53 PM

SetPrinter() requires reboot. I need an alternative method.
 
Using the following method I make a successful change to the printer
settings. Every change is reflected under start/settings/printers
until I actually print a page. Then no changes are possible until I
reboot. After the reboot, my attempted settings are reflected under
start/settings/printers.
Checking the msdn tells this:

Remarks
Registry changes are flushed to disk by the registry using its lazy
flusher. Lazy flushing occurs automatically and regularly after a
system-specified interval of time. Registry changes are also flushed to
disk at system shutdown.

Is it possible that my changes are waiting until the reboot? If so, I
can't have users rebooting everytime they want to change labels. Then
the time that my program saves has gone to waist as they reboot. What
other methods could be used to update the print driver?

GetPrinter(); // get settings
make changes
DocumentProperties(); // update driver
SetPrinter(); // set settings

I'm not asking for someone to debug my code, just wondering what other
API calls could be used to update printer settings since my chosen
method isn't working without a reboot.

-#2pencil-

/* Change the form size by modifying DEVMODE */
// Open printer handle
// On Windows NT, you need full-access because you will eventually
use SetPrinter...
ZeroMemory(&pd, sizeof(pd));
if(OS==8)pd.DesiredAccess = PRINTER_ALL_ACCESS;
//PRINTER_ACCESS_ADMINISTER;
bFlag=OpenPrinter(pPrinterName,&hPrinter,&pd);
if (!bFlag || (hPrinter == NULL)) {
ErrorExit("OpenPrinter");
return FALSE;
}
// The first GetPrinter tells you how big the buffer should be in
// order to hold all of PRINTER_INFO_2. Note that this should fail
with
// ERROR_INSUFFICIENT_BUFFER. If GetPrinter fails for any other
reason
// or dwNeeded isn't set for some reason, then there is a problem...
SetLastError(0);
bFlag = GetPrinter(hPrinter,OS,0,0,&dwNeeded);
if ((!bFlag) && (GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
(dwNeeded == 0))
{
ClosePrinter(hPrinter);
ErrorExit("GetPrinter pt1");
return FALSE;
}
// Allocate enough space for PRINTER_INFO_2...
switch(OS) {
case 2:
pi2 = (PRINTER_INFO_2 *)GlobalAlloc(GPTR, dwNeeded);
break;
case 8:
pi8 = (PRINTER_INFO_8 *)GlobalAlloc(GPTR, dwNeeded);
break;
}
if(OS==2 && pi2 == NULL || (hPrinter == NULL))
{
ClosePrinter(hPrinter);
ErrorExit("GlobalAlloc");
return FALSE;
}
if(OS==8 && pi8==NULL)
{
ClosePrinter(hPrinter);
ErrorExit("GlobalAlloc");
return FALSE;
}
// The second GetPrinter fills in all the current settings, so all you
// need to do is modify what you're interested in...
if(OS==2)bFlag =
GetPrinter(hPrinter,OS,(LPBYTE)pi2,dwNeeded,&dwNee ded);
if(OS==8)bFlag =
GetPrinter(hPrinter,OS,(LPBYTE)pi8,dwNeeded,&dwNee ded);
if (!bFlag || (hPrinter == NULL))
{
if(OS==2)GlobalFree(pi2);
if(OS==8)GlobalFree(pi8);
ClosePrinter(hPrinter);
ErrorExit("GetPrinter pi");
return FALSE;
}
// If GetPrinter didn't fill in the DEVMODE, try to get it by calling
// DocumentProperties...
if(OS==2 && pi2-pDevMode == NULL)
{
dwNeeded=DocumentProperties(NULL,hPrinter,pPrinter Name,NULL,NULL,0);
if (dwNeeded = 0)
{
GlobalFree(pi2);
ClosePrinter(hPrinter);
ErrorExit("Document Properties");
return FALSE;
}
pDM = (PSDEVMODE *)GlobalAlloc(GPTR,dwNeeded);
if(pDM==NULL)
{
GlobalFree(pDM);
ClosePrinter(hPrinter);
ErrorExit("GlobalAlloc dmPrivate");
return FALSE;
}
pDevMode = (DEVMODE *)GlobalAlloc(GPTR, dwNeeded);
if (pDevMode == NULL)
{
GlobalFree(pi2);
ClosePrinter(hPrinter);
ErrorExit("GlobalAlloc pDevMode");
return FALSE;
}
lFlag = DocumentProperties(NULL, hPrinter,
pPrinterName,
pDevMode, NULL,
DM_OUT_BUFFER);
if (lFlag != IDOK || pDevMode == NULL)
{
GlobalFree(pDevMode);
GlobalFree(pi2);
ClosePrinter(hPrinter);
ErrorExit("GlobalAlloc");
return FALSE;
}
pi2-pDevMode = pDevMode;
}
if(OS==8 && pi8-pDevMode == NULL)
{
dwNeeded=DocumentProperties(NULL,hPrinter,pPrinter Name,NULL,NULL,0);
if (dwNeeded = 0)
{
GlobalFree(pi8);
ClosePrinter(hPrinter);
ErrorExit("Document Properties");
return FALSE;
}
pDM = (PSDEVMODE *)GlobalAlloc(GPTR,dwNeeded);
if(pDM==NULL)
{
GlobalFree(pDM);
ClosePrinter(hPrinter);
ErrorExit("GlobalAlloc dmPrivate");
return FALSE;
}
pDevMode = (DEVMODE *)GlobalAlloc(GPTR, dwNeeded);
if (pDevMode == NULL)
{
GlobalFree(pi8);
ClosePrinter(hPrinter);
ErrorExit("GlobalAlloc pDevMode");
return FALSE;
}
lFlag = DocumentProperties(NULL, hPrinter,pPrinterName,
pDevMode, NULL,DM_OUT_BUFFER);
if (lFlag != IDOK || pDevMode == NULL || (hPrinter == NULL))
{
GlobalFree(pDevMode);
GlobalFree(pi8);
ClosePrinter(hPrinter);
ErrorExit("GlobalAlloc");
return FALSE;
}
pi8-pDevMode = pDevMode;
}
if(OS==2)pDM = (PSDEVMODE *)(pi2-pDevMode);
if(OS==8)pDM = (PSDEVMODE *)(pi8-pDevMode);
// Specify exactly what we are attempting to change...
pDM-dmPrivate.lgap=Setup.gap;
pDM-dmPublic.dmFields |= DM_PAPERLENGTH;
pDM-dmPublic.dmPaperLength=Setup.height; //*254
pDM-dmPublic.dmFields |= DM_ORIENTATION;
pDM-dmPublic.dmOrientation=DMORIENT_LANDSCAPE;
pDM-dmPublic.dmFields |= DM_SPEED;
pDM-dmPrivate.speed=Setup.speed;
pDM-dmPublic.dmFields |= DM_HEAT;
pDM-dmPrivate.heat=Setup.heat;
pDM-dmPrivate.cut=0;
pDM-dmPublic.dmFields |= DM_FLIP;
pDM-dmPrivate.flip=Setup.flip;
pDM-dmPublic.dmFields |= DM_RBPAPER;
pDM-dmPrivate.labelsensor=Setup.labelsensor;
pDM-dmPublic.dmFields |= DM_THERMALTRANSFER;
pDM-dmPrivate.thermaltran=1;
// pDM-dmPrivate.reverseim=0;
// pDM-dmPrivate.mirrorim=0;
pDM-dmPublic.dmFields |= DM_INTRPSTARTSTOP;
pDM-dmPrivate.intrpstartstop=0;
pDM-dmPrivate.barintrp=0;
pDM-dmPrivate.demandoffset=0;
pDM-dmPrivate.demandmode=0;
pDM-dmPrivate.feed=0;
pDM-dmPrivate.memorymodule=0;
pDM-dmPrivate.columnoffset=0;
pDM-dmPrivate.rowoffset=0;
pDM-dmPrivate.cellsize=4;
pDM-dmPrivate.errlevel=1;
pDM-dmPrivate.pdfrowsize=10;
pDM-dmPrivate.pdfsecurity=1;
pDM-dmPublic.dmFields |= DM_CONTINUOUS;
pDM-dmPrivate.continuous=1;
// Make sure the driver-dependent part of devmode is updated...
if(OS==2) {
lFlag = DocumentProperties(NULL, hPrinter,
pPrinterName,pi2-pDevMode, pi2-pDevMode,
DM_IN_BUFFER | DM_OUT_BUFFER);
if (lFlag != IDOK)
{
GlobalFree(pi2);
ClosePrinter(hPrinter);
if (pDevMode)GlobalFree(pDevMode);
ErrorExit("Document Properties pDevMode");
return FALSE;
}
}
if(OS==8) {
lFlag = DocumentProperties(NULL, hPrinter,
pPrinterName,pi8-pDevMode, pi8-pDevMode,
DM_IN_BUFFER | DM_OUT_BUFFER);
if (lFlag != IDOK)
{
GlobalFree(pi8);
ClosePrinter(hPrinter);
if (pDevMode)GlobalFree(pDevMode);
ErrorExit("Document Properties pDevMode");
return FALSE;
}
}
if(pDM-dmPrivate.lgap!=Setup.gap){
// sprintf(szBuffer,"ini: %i; Memory:
%i",Setup.gap,pDM-dmPrivate.lgap);
// MessageBox(hDlg,szBuffer,"A 8 Error",MB_OK);
pDM-dmPrivate.lgap=Setup.gap;
// MessageBox(hDlg,"lgap Corrected","A 8 Config",MB_OK);
}
if(pDM-dmPrivate.labelsensor!=Setup.labelsensor){
// sprintf(szBuffer,"ini: %i; Memory:
%i",Setup.labelsensor,pDM-dmPrivate.labelsensor);
// MessageBox(hDlg,szBuffer,"A 8 Error",MB_OK);
pDM-dmPrivate.labelsensor=Setup.labelsensor;
// MessageBox(hDlg,"Sensor Corrected","A 8 Config",MB_OK);
}
if(OS==2)bFlag = SetPrinter(hPrinter,OS,(LPBYTE)pi2,0);
if(OS==8)bFlag = SetPrinter(hPrinter,OS,(LPBYTE)pi8,0);
if (!bFlag)pi2-pPrintProcessor=gszPrintProc;
if(OS==2)bFlag = SetPrinter(hPrinter,OS,(LPBYTE)pi2,0);
if(!bFlag) {
if(OS==2)GlobalFree(pi2);
if(OS==8)GlobalFree(pi8);
ClosePrinter(hPrinter);
if (pDevMode)GlobalFree(pDevMode);
ErrorExit("SetPrinter");
return FALSE;
}
// Update printer information...
if(OS==2)bFlag = SetPrinter(hPrinter,OS,(LPBYTE)pi2,0);
if(OS==8)bFlag = SetPrinter(hPrinter,OS,(LPBYTE)pi8,0);
// Begin Cut
if (!bFlag)pi2-pPrintProcessor=gszPrintProc;
// Make sure the driver-dependent part of devmode is updated...
if(OS==2)lFlag = DocumentProperties(NULL, hPrinter,pPrinterName,
pi2-pDevMode,pi2-pDevMode,DM_IN_BUFFER | DM_OUT_BUFFER);
if(OS==8)lFlag = DocumentProperties(NULL, hPrinter,pPrinterName,
pi8-pDevMode,pi8-pDevMode,DM_IN_BUFFER | DM_OUT_BUFFER);
if (lFlag != IDOK)
{
if(OS==2)GlobalFree(pi2);
if(OS==8)GlobalFree(pi8);
ErrorExit("Document Properties pDevMode update");
ClosePrinter(hPrinter);
if (pDevMode)
GlobalFree(pDevMode);
return FALSE;
}
if(OS==2)bFlag = SetPrinter(hPrinter,OS,(LPBYTE)pi2,0);
//End Cut
if (!bFlag)
// The driver doesn't support, or it is unable to make the change...
{
//ErrorExit("SetPrinter");
if(OS==2)GlobalFree(pi2);
if(OS==8)GlobalFree(pi8);
ClosePrinter(hPrinter);
if (pDevMode)GlobalFree(pDevMode);
ErrorExit("SetPrinter");
return FALSE;
}
// Tell other apps that there was a change...
SendMessageTimeout(HWND_BROADCAST, WM_DEVMODECHANGE, 0L,
(LPARAM)(LPCSTR)pPrinterName,SMTO_NORMAL, 1000, NULL);
// Clean up...
if (pi2)GlobalFree(pi2);
if (pi8)GlobalFree(pi8);
if (pDM)GlobalFree(pDM);
if (hPrinter)bFlag=ClosePrinter(hPrinter);
if (!bFlag)MessageBox(hDlg,"Problem closing printer","A8",MB_OK |
MB_ICONEXCLAMATION);
if (pDevMode)GlobalFree(pDevMode);

// HKEY_CURRENT_CONFIG\System\CurrentControlSet\Contr ol\Print\Printers
HKEY hKey;
TCHAR lpSubKey[] =
TEXT("System\\CurrentControlSet\\Control\\Print\\P rinters\\");
DWORD value_type, value_size;
char *value_data;

RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpSubKey, 0, KEY_QUERY_VALUE,
&hKey);
RegFlushKey(hKey);
RegCloseKey(hKey);

sprintf(ConfigText, "A8 Setup: %s", ButtonText);
SetWindowText(hDlg, ConfigText);
sprintf(szBuffer, "Settings changed to %s", ButtonText);
MessageBox(hDlg, szBuffer, "A8 Configuration", MB_OK |
MB_ICONEXCLAMATION);
sprintf(Mes, "Current Setup: %s", ButtonText);
InvalidateRect(hWndMain,NULL,FALSE); /* Force a repaint */
UpdateWindow(hWndMain); /* Force a repaint */
return TRUE;



All times are GMT +1. The time now is 07:51 AM.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Win98Banter.com