SetDefaultPrinter not working on fb 32bits, need help

Windows specific questions.
Post Reply
marpon
Posts: 342
Joined: Dec 28, 2012 13:31
Location: Paris - France

SetDefaultPrinter not working on fb 32bits, need help

Post by marpon »

I am trying to change the default printer via freebasic, I"ve used a very simple function SetDefaultPrinter

it works only with freebasic 64bits not with freebasic 32bits

i've tested on different os 32 bits , xp , vista , 7 it does not compile (tested with versions .24 ; 1.05 ; last 1.06)

on 64 bits os win 10 using fb32 : it also does not compile
but on 64 bits os win 10 using fb64 : it compiles fine (tested with versions 1.05 and last 1.06)

i even tested with -gen gcc without success

code to test

Code: Select all

#include once "windows.bi"
#include once "win\winspool.bi"

Function get_defp()as string
	dim szPrinter0 AS zSTRING * 250
	dim cchBuffer AS DWORD
	dim retp as long
	dim sret as string
	cchBuffer = 250
	retp = GetDefaultPrinter( @szPrinter0, @cchBuffer)
	if retp THEN sret = left(szPrinter0, cchBuffer -1)
	return sret
END FUNCTION

Function set_defp(ByRef sp as string )as long
	dim retp as long = SetDefaultPrinter(strptr(sp))
	return retp
END function



dim as string init0


init0 = get_defp() 'store current default printer
MessageBox 0, "Current default printer : " & init0 , "Printer", MB_SETFOREGROUND



if init0 <> "Microsoft Print to PDF" THEN
	set_defp("Microsoft Print to PDF")		' change default printer
	MessageBox 0, "Changed default printer to : " & get_defp() , "Printer", MB_SETFOREGROUND
	sleep 150
	set_defp(init0)								' restore current default printer
	MessageBox 0, "Restored default printer to : " & get_defp() , "Printer", MB_SETFOREGROUND
	sleep 150
END IF


sleep 400
result when not compiling

Code: Select all

"D:\freebasic\FreeBASIC-1.05.0-win32\fbc.exe" -x "D:\freebasic\print_pdf\select_printer0.exe" -s console -v select_printer0.bas > select_printer0.log 2>&1


FreeBASIC Compiler - Version 1.05.0 (01-31-2016), built for win32 (32bit)
Copyright (C) 2004-2016 The FreeBASIC development team.
standalone
target:       win32, 486, 32bit
compiling:    select_printer0.bas -o select_printer0.asm (main module)
assembling:   D:\freebasic\FreeBASIC-1.05.0-win32\bin\win32\as.exe --32 --strip-local-absolute "select_printer0.asm" -o "select_printer0.o"
linking:      D:\freebasic\FreeBASIC-1.05.0-win32\bin\win32\ld.exe -m i386pe -o "D:\freebasic\print_pdf\select_printer0.exe" -subsystem console "D:\freebasic\FreeBASIC-1.05.0-win32\lib\win32\fbextra.x" --stack 1048576,1048576 -s -L "D:\freebasic\FreeBASIC-1.05.0-win32\lib\win32" -L "." "D:\freebasic\FreeBASIC-1.05.0-win32\lib\win32\crt2.o" "D:\freebasic\FreeBASIC-1.05.0-win32\lib\win32\crtbegin.o" "D:\freebasic\FreeBASIC-1.05.0-win32\lib\win32\fbrt0.o" "select_printer0.o" "-(" -lkernel32 -lgdi32 -lmsimg32 -luser32 -lversion -ladvapi32 -limm32 -lwinspool -lfb -lgcc -lmsvcrt -lmingw32 -lmingwex -lmoldname -lgcc_eh "-)" "D:\freebasic\FreeBASIC-1.05.0-win32\lib\win32\crtend.o" 
select_printer0.o:fake:(.text+0x123): undefined reference to `SetDefaultPrinterA@4'
linking failed: 'D:\freebasic\FreeBASIC-1.05.0-win32\bin\win32\ld.exe' terminated with exit code 1

is it a bug , or what am doing wrong...

comments welcome
marpon
Posts: 342
Joined: Dec 28, 2012 13:31
Location: Paris - France

Re: SetDefaultPrinter not working on fb 32bits, SOLVED

Post by marpon »

Thank's to a nice friend...

The answer is : it is a bug on the libwinspool.dll.a (the lib interface to winspool.drv)
these functions SetDefaultPrinterA and SetDefaultPrinterW are missing (why?) on the 32bit version of libwinspool.dll.a

so, you have 2 choices as workarround
recreate your own specific lib from winspool.drv
or use the DyLibLoad("winspool.drv") ; DyLibSymbol(pLib, "SetDefaultPrinterA") without forgetting DyLibFree

i've just checked both solutions , it works now on 32bits also with the 2 solutions...
marpon
Posts: 342
Joined: Dec 28, 2012 13:31
Location: Paris - France

Re: SetDefaultPrinter not working on fb 32bits, SOLVED

Post by marpon »

the function using DyLibLoad

Code: Select all

Function set_defp(ByRef sp as string )as long
	DIM AS ANY PTR pLib = DyLibLoad("winspool.drv")
	FUNCTION = 0
   IF pLib = NULL THEN EXIT FUNCTION
	DIM pSetDefaultPrinter AS FUNCTION (byval pszPrinter as LPCSTR) AS LONG
   pSetDefaultPrinter = DyLibSymbol(pLib, "SetDefaultPrinterA")
   IF pSetDefaultPrinter = NULL THEN 
		DyLibFree(pLib)
		EXIT FUNCTION
	end if
   FUNCTION = pSetDefaultPrinter(strptr(sp))
   DyLibFree(pLib)
END function
Post Reply