And, finally, set the system printer. Broadcast the change to force windows to reload the registry or the change will have no immediate effect. As the function name indicates, this will not work on Win9x. I have another function for that if you need it. I hope you find this useful.
#define HWND_BROADCAST 0x0000FFFF
#define WM_WININICHANGE 0x0000001A
declare integer WriteProfileString in Win32API ;
string @lpszSection,; && pointer to section name
string @lpszKeyName,; && pointer to key name
string @lpszString && pointer to string to write
declare integer SendMessage in Win32API ;
integer hWnd,; && handle of destination window
integer wMsg,; && message to send
integer wParam,; && first message parameter
string lParam && second message parameter
Function SetDefaultPrinterNT(cPrinterName)
local lpAppName,lpKeyName,lpDefault,lpReturnedString,nSize,nResult,cPrinterDriver,cPrinterPort,cLine,lResult
lResult = .F.
lpAppName = "PrinterPorts" + chr(0)
lpKeyName = cPrinterName + chr(0)
lpDefault = "Failed" + chr(0)
store space(1024) to lpReturnedString
nSize = len(lpReturnedString)
nResult = GetProfileString(lpAppName,lpKeyName,lpDefault,lpReturnedString,nSize)
if nResult > 0
cPrinterDriver = substr(lpReturnedString,1,at(",",lpReturnedString)-1)
cPrinterPort = substr(lpReturnedString,at(",",lpReturnedString)+1,at(",",lpReturnedString,2)-1)
if not ((cPrinterDriver == ""

and (cPrinterPort == ""

)
cLine = PrinterName + "," + DriverName + "," + PrinterPort
nResult = WriteProfileString("windows","device",cLine)
if nResult <> 0
nResult = SendMessage(HWND_BROADCAST,WM_WININICHANGE,0,"Windows"

lResult = .T.
else
#ifdef _DEBUG
messageBox("WriteProfileString returned 0 in SetDefaultPrinterNT()",48,"Function Error"

#endif
endif
endif
else
#ifdef _DEBUG
messageBox("GetProfileString returned 0 in SetDefaultPrinterNT()",48,"Function Error"

#endif
endif
return lResult