I have the following code that works fine on my Win 7 box - i.e lists the available printer and changes the selected to the default printer - moving the 'tick' the the correct printer. However when I run this on my terminal server box it list the printers okay but when I select the printer it removes the default printer completely (no tick against any printer) - any ideas ?
uses Printers;
function GetDefaultPrinter: string;
var
ResStr: array[0..255] of Char;
begin
GetProfileString('Windows', 'device', '', ResStr, 255);
Result := StrPas(ResStr);
end;
procedure SetDefaultPrinter(NewDefPrinter: string);
var
ResStr: array[0..255] of Char;
begin
StrPCopy(ResStr, NewdefPrinter);
WriteProfileString('windows', 'device', ResStr);
StrCopy(ResStr, 'windows');
SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, Longint(@ResStr));
end;
procedure TfrmMain.FormCreate(Sender: TObject);
begin
//Fill the combobox with all available printers
Combobox1.Items.Clear;
Combobox1.Items.AddStrings(Printer.Printers);
// Get the Default Printer:
label1.Caption := Format('Default Printer: %s',[GetDefaultPrinter]);
end;
procedure TfrmMain.Button2Click(Sender: TObject);
begin
//Set the selected printer in the combobox as default printer
if Combobox1.Text <> '' then
SetDefaultPrinter(Combobox1.Text)
else ShowMessage('Choose a Printer first !');
end;