Page 1 of 1

DeletePrinter()

Posted: Sat Oct 21, 2006 2:53 pm
by Davide
Hello all.

I'm trying to automatically delete a printer by the source below, but I always receive Error 5 and I don't find where's the error.
Do you see something wrong ?

Thanks,
Davide.

Code: Select all

#include "FiveWin.ch"
#include "Struct.ch"
// #include "WinSpool.h"
#define PRINTER_ACCESS_ADMINISTER   0x00000004

function DelPrn( cName )

Local Result:=.f.,oPrnDefaults,hDC 

DEFAULT cName := "Test"

  STRUCT oPrnDefaults
     MEMBER pDatatype           AS LPSTR
     MEMBER pDevMode            AS LPSTR  // LPDEVMODE 
     MEMBER DesiredAccess       AS DWORD  // ACCESS_MASK
  ENDSTRUCT

  oPrnDefaults:DesiredAccess    = PRINTER_ACCESS_ADMINISTER

  IF ( ASCAN(aDevName(),{|x| Upper(x) == Upper(cName)}) >0 )    

    If OpenPrinter(cName , @hDC , oPrnDefaults:cBuffer )
      If hDC # 0
        Result := DelPrinter( hDC ) 
        If !Result ; MsgAlert( 'DelPrn Error ' + Str( GetLastError() ) ) ; Endif
        //
        // --- Always receive Error 5 here !!! ---
        // 
        ClosePrinter( hDC )
      Endif
    Endif

  Endif

Return Result

//------------------------------------------------------------------------//

DLL32 FUNCTION ClosePrinter( pPrinter AS LONG ) AS BOOL PASCAL ;
                             FROM "ClosePrinterA" LIB "winspool.drv"

//------------------------------------------------------------------------//

DLL32 FUNCTION DelPrinter( pPrinter AS LONG ) AS BOOL PASCAL ;
                             FROM "DeletePrinter" LIB "winspool.drv"

//------------------------------------------------------------------------//

DLL32 FUNCTION OpenPrinter( pName AS LPSTR, @pPrinter AS LONG, pDef AS LPSTR ) ;
                             AS BOOL PASCAL ;
                             FROM "OpenPrinterA" LIB "winspool.drv"

//------------------------------------------------------------------------//

Posted: Sat Oct 21, 2006 6:06 pm
by Antonio Linares
Davide,

#define EACCES 5 /* Permission denied */

Do you have enough privileges to delete it ?

Posted: Sat Oct 21, 2006 11:59 pm
by Davide
Antonio,

thank you for your reply.
Antonio Linares wrote: Do you have enough privileges to delete it ?
Yes, I can delete it either by the GUI dialog or by RUNDLL32 PRINTUI.DLL,PrintUIEntry .... (which would not work on Win9x - that's why I'm trying to do it with the APIs, even on XP)

Perhaps the problem may be in the data type of DesiredAccess ? (I already tried to pass NIL instead of the PrinterDefaults structure in OpenPrinter(), with the same results, so I though that probably I have to open the printer with administrative permissions to be allowed to delete it)

Thank you
Davide