Mega Code Archive

 
Categories / Delphi / Examples
 

Printerdefault

Hi, Here's some code from a component I wrote.... FPrintDevice is the printer from the available printers retrieved using GetProfileString......not the printer.printers. I'll send the component directly to you... so you can get the full thing. The component uses the printer.printers to get the device, port and printer name from the getprofilestring (ini in Win95, registry in NT), then uses that information to set the default. I also made a showprinter procedure to lauch the printers window so you can verify that that printer did indeed change. procedure TbmPrinterInfo.Execute; var prnTemp: string; BufferVar: Pchar; BufSize, ReturnCode: integer; const cs1: pchar = 'Windows'; begin if Trim(FPrintDevice)<>'' then begin BufSize := 254; GetMem(BufferVar, BufSize); try ReturnCode := GetProfileString('Devices', pchar(FPrintDevice), #0, BufferVar, BufSize); if (ReturnCode > 0) and (trim(strpas(BufferVar)) <> '') then begin prnTemp := FPrintDevice + ',' + strpas(BufferVar); while GetProfileString('Device', 'Device', #0, BufferVar, BufSize) > 0 do WriteProfileString('Windows', 'Device', #0); WriteProfileString('Windows', 'Device', pchar(prnTemp)); case Win32Platform of VER_PLATFORM_WIN32_NT: SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, LongInt(cs1)); VER_PLATFORM_WIN32_WINDOWS: SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, LongInt(cs1)); end; { case } end; finally FreeMem(BufferVar); end; end else begin if csDesigning in ComponentState then ShowMessage('Select a printer before setting default.'); end; end; __________________________________________________________________________ bmclean@MNSi.net To me, there's no better symbol for the world than a grasshopper lying dead on a gravel road, and maybe there's a globe lying next to him. Deep Thoughts by Jack Handey _______________________________________________ Delphi mailing list -> Delphi@elists.org http://elists.org/mailman/listinfo/delphi ******************************************************************* Article explaining why using WriteProfileString( 'windows', 'device', Device ); works in NT... The traditional approach to setting the default printer, and one that should work according to documentation, is to enumerate the Printers collection until the desired printer is located, then assigning that to the Printer object. This doesn't work as advertised. The printer name will change, but output still goes to the original default device. Under Win9x, installed printers are listed in win.ini under the PrinterPorts key, with the system default printer listed under the Device key, and various documentation suggests that changing this key sets the default printer. But if you examine the win.ini file on a WinNT/2000 system, the file does not contain any printer port or default printer info. Not to worry. By default, using either GetProfileString / WriteProfileString or GetPrivateProfileString / WritePrivateProfileString on WinNT/2000, the APIs actually first look to the registry under the key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\IniFileMapping for the ini file name of interest. If its not found, then the physical disk ini file itself is checked. In the case of win.ini, if its found in the registry under the key above, it is further examined for the section name desired (PrinterPorts). The registry value assigned to this key point to the location within the registry of the actual installed printers data (which in my case this points to "USR:Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts"). Under the PrinterPorts key the installed printers are listed. As well, the default printer setting corresponding to the expected ini file "Device" entry is stored in the \windows section under that same parent key. When an operation has been mapped, the Get(Private)ProfileString functions retrieve information from the registry, not from the physical initialization file. This change in the storage location has no effect on the function's behavior and is transparent to the calling application. What this all means is simply that there is no need to resort to registry reading/writing calls - the familiar ini file APIs can be utilized to change the default printer....