setting default printer: program hang

Just starting out? Need help? Post your questions and find answers here.
morosh
Enthusiast
Enthusiast
Posts: 291
Joined: Wed Aug 03, 2011 4:52 am
Location: Beirut, Lebanon

setting default printer: program hang

Post by morosh »

Hello:
In my program I use the following to set the default printer:

Code: Select all

Procedure Setdefaultprinter1(DeviceLine.s)
  WriteProfileString_("windows", "Device", DeviceLine)
  SendMessage_(#HWND_BROADCAST, #WM_WININICHANGE, 0, "windows")
EndProcedure
It worked before, but since some time (may be after a windows10 update) it hang at the line:
SendMessage_(#HWND_BROADCAST, #WM_WININICHANGE, 0, "windows")

I should kill the program to stop it.
has anyone experienced that? is there another way to set the default printer under windows10?
is there an external program (to use with runprogram) that can do the same job?

Thank you
PureBasic: Surprisingly simple, diabolically powerful
User avatar
spikey
Enthusiast
Enthusiast
Posts: 579
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: setting default printer: program hang

Post by spikey »

Basically other programs on the system can cause yours to lock up when you do this with #HWND_BROADCAST. See https://stackoverflow.com/questions/195 ... cast-hangs

You could try PostMessage or SendMessageTimeout instead, see https://learn.microsoft.com/en-us/windo ... stmessagew or https://learn.microsoft.com/en-us/windo ... getimeoutw.

Or there is SetDefaultPrinter see https://learn.microsoft.com/en-us/windo ... ultprinter
morosh
Enthusiast
Enthusiast
Posts: 291
Joined: Wed Aug 03, 2011 4:52 am
Location: Beirut, Lebanon

Re: setting default printer: program hang

Post by morosh »

Thank you Spikey

Meanwhile I resolved my problem in 2 ways:
1- looking at examples in C++ forums I noticed that they use SendNotifyMessage_ instead of SendMessage_, so changing my code to

Code: Select all

Procedure Setdefaultprinter1(DeviceLine.s)
  WriteProfileString_("windows", "Device", DeviceLine)
  SendNotifyMessage_(#HWND_BROADCAST, #WM_WININICHANGE, 0, "windows")
EndProcedure
works very well.

2- also trying

Code: Select all

Procedure Setdefaultprinter1(DeviceLine.s)
  RunProgram("RUNDLL32","PRINTUI.DLL,PrintUIEntry /y /n"+ #DQUOTE$+DeviceLine+#DQUOTE$,"") 
EndProcedure
works well also.

About using SetdefaultPrinter(), there is need to Winspool.lib.

Regards
PureBasic: Surprisingly simple, diabolically powerful
Post Reply