So, I tried to simply write the SetDefaultPrinter as local function and then no hang occours.
HOW TO CHANGE DEFAULT PRINTER FOR WINDOWS 10 CODE
I also tried to use the "SendMessage( HWND_BROADCAST, WM_WININICHANGE, 0,LongInt(cs1))" code block and my program was hang. Writeln(Format('EOleException %s %x', )) SetDefaultPrinter('HP LaserJet') //here you must pass the DeviceID of one the printers listed above Result:=FWbemObject.SetDefaultPrinter()=0 Writeln(Format('DeviceID %s Name %s',)) įunction SetDefaultPrinter(const DeviceID:string):boolean įWbemObject := FWMIService.Get(Format('Win32_Printer.DeviceID="%s"',)) While oEnum.Next(1, FWbemObject, iValue) = 0 do OEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant įSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator') įWMIService := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', '') įWbemObjectSet:= FWMIService.ExecQuery('SELECT DeviceID, Name FROM Win32_Printer','WQL',wbemFlagForwardOnly) Try using the Win32_Printer WMI class to list the printers and the SetDefaultPrinter method to set the default printer.
HOW TO CHANGE DEFAULT PRINTER FOR WINDOWS 10 PDF
The goal is to change the default printer to the selected printer in order to print the pdf to the desired printer and then return the printer to the original default on exit of the application The print job is actually a PDF being printed using ShellExecute(Application.Handle, 'print', PChar(sPath), nil, nil, SW_HIDE) It's a piece of code I picked up while searching for a solution to my problem. SendMessage( HWND_BROADCAST, WM_SETTINGCHANGE, 0,LongInt(cs1)) Īnyone have any tips or a better way to do this?Īs a side note this is not my function. SendMessage( HWND_BROADCAST, WM_WININICHANGE, 0,LongInt(cs1)) WriteProfileString( cs1, cs2, pchar( s2 )) While GetProfileString( cs1, cs2, cs4, dum1, xx) > 0 do S2 := PrinterName + ',' + strpas( dum1 ) If you want to get into Advanced preferences, click on the 'Advanced' button on the previous screen and you will see the. Now, your printer is all set to print in Black & White.
If (qq > 0) and (trim( strpas( dum1 )) '') then Go to Paper/Quality and select 'Black & White' in the Color options. Qq := GetProfileString( cs3, pchar( PrinterName ), #0, dum1, xx)
Rather than adjust the security settings to allow right clicking, I found a cmd line command that would set the default printer under the restricted user account. Printername is bv: '\\MYPRINTER\HP5-k' Also, since setting default printers are user profile specific, it was not as easy as setting the default printer with an admin account and that carryover to the regular user account. function TMainFrm.SetDefaultPrinter(const PrinterName: string): boolean However the program becomes unresponsive without any errors or program not responding messages when this line of code executes: SendMessage( HWND_BROADCAST, WM_WININICHANGE, 0,LongInt(cs1)) I am trying to use the below code to change the default printer and then my print code will print to that printer. I use "Printers" to get the printer index but the actual printing is done with proprietary print code that allows for direct to pdf printing and easier page layout. I am attempting to change the default printer in the on change event of a combo box that list the printer index.