Modifying printer properties with local system account

  • Thread starter Thread starter Marcel
  • Start date Start date
M

Marcel

Hi,

I have a problem using a printer driver with a service running as
local system account.

The program I wrote (look code snippet without error handling below)
first modifies the Printing preferences of a printer and then does a
ShellExecute with "printto" command on that printer. When I run this
program as a user other than system, the file is printed using the new
resolution settings. When I run as system account, every operation
seems to go well, no error codes and the file gets printed, but the
resolution to which it's printed use old settings of the printer.

If any of you has an idea of how I could manage to make the system
account modify the printing preferences and print using these, It
would save me.

Thanks a lot
Marcel

Here is the code snippet:

In the program, I do the following:
// Step 1 - Change printer properties
OpenPrinter("MyPrinterName", &handle, &defaults); // with
PRINTER_ALL_ACCESS
DWORD dwNeeded = 0;
GetPrinter(handle, // handle to printer
2, // information level
NULL, // printer information buffer
0, // size of buffer
&dwNeeded // bytes received or required
);
[...allocated buffer...]
GetPrinter(handle, 2, buffer, dwNeeded, &dwNeeded))
LPDEVMODE pDevMode =
(reinterpret_cast<PRINTER_INFO_2*>(buffer))->pDevMode;
// Change Resolution
pDevMode->dmPrintQuality = 200; //or any value
pDevMode->dmYResolution = 100; //or any value
SetPrinter(handle, 2, buffer, 0))

// Step 2 - Print using printto
SHELLEXECUTEINFO info;
ZeroMemory(&info, sizeof(SHELLEXECUTEINFO) );
info.cbSize = sizeof(SHELLEXECUTEINFO);
info.hwnd = GetDesktopWindow();
info.lpVerb = L"printto";
info.lpFile = path_to_file;
info.lpParameters = L"MyPrinterName WINSPOOL MyPrinterPort";
info.lpDirectory = NULL;
info.nShow = SW_SHOWMINNOACTIVE;
info.fMask = SEE_MASK_FLAG_NO_UI | SEE_MASK_FLAG_DDEWAIT;

SetLastError(0);
isOk = ShellExecuteEx(&info);
 
Marcel,

You might want to try using the DocumentProperties API call rather than
GetPrinter/SetPrinter.
 
Thanks for your suggestion but unfortunately, it doesn't work either.

I'm not sure if the problem is that the SetPrinter (or
DocumentProperties with DM_IN_BUFFER) does not save or that the
launched executable somehow doesn't retrieve the same printer settings
as the application who has set it.

I'll look at that and get back.
If you have any other suggestions, please feel free to post them (and
email me at the same time please, so that I don't have to wait after
it appears on google since I don't receive the newsgroup posts from
where I am. Thanks)

Marcel

Tony Edgecombe said:
Marcel,

You might want to try using the DocumentProperties API call rather than
GetPrinter/SetPrinter.

--
Tony Edgecombe
www.frogmorecs.com/ng
Software for printing


Marcel said:
Hi,

I have a problem using a printer driver with a service running as
local system account.

The program I wrote (look code snippet without error handling below)
first modifies the Printing preferences of a printer and then does a
ShellExecute with "printto" command on that printer. When I run this
program as a user other than system, the file is printed using the new
resolution settings. When I run as system account, every operation
seems to go well, no error codes and the file gets printed, but the
resolution to which it's printed use old settings of the printer.

If any of you has an idea of how I could manage to make the system
account modify the printing preferences and print using these, It
would save me.

Thanks a lot
Marcel

Here is the code snippet:

In the program, I do the following:
// Step 1 - Change printer properties
OpenPrinter("MyPrinterName", &handle, &defaults); // with
PRINTER_ALL_ACCESS
DWORD dwNeeded = 0;
GetPrinter(handle, // handle to printer
2, // information level
NULL, // printer information buffer
0, // size of buffer
&dwNeeded // bytes received or required
);
[...allocated buffer...]
GetPrinter(handle, 2, buffer, dwNeeded, &dwNeeded))
LPDEVMODE pDevMode =
(reinterpret_cast<PRINTER_INFO_2*>(buffer))->pDevMode;
// Change Resolution
pDevMode->dmPrintQuality = 200; //or any value
pDevMode->dmYResolution = 100; //or any value
SetPrinter(handle, 2, buffer, 0))

// Step 2 - Print using printto
SHELLEXECUTEINFO info;
ZeroMemory(&info, sizeof(SHELLEXECUTEINFO) );
info.cbSize = sizeof(SHELLEXECUTEINFO);
info.hwnd = GetDesktopWindow();
info.lpVerb = L"printto";
info.lpFile = path_to_file;
info.lpParameters = L"MyPrinterName WINSPOOL MyPrinterPort";
info.lpDirectory = NULL;
info.nShow = SW_SHOWMINNOACTIVE;
info.fMask = SEE_MASK_FLAG_NO_UI | SEE_MASK_FLAG_DDEWAIT;

SetLastError(0);
isOk = ShellExecuteEx(&info);
 
Back
Top