请直接给个代码吧,为什么调用setprinter返回错误值5(拒绝访问) 以下摘自MSDNSetPrinter The SetPrinter function sets the data for a specified printer or sets the state of the specified printer by pausing printing, resuming printing, or clearing all print jobs. BOOL SetPrinter( HANDLE hPrinter, // handle to printer object DWORD Level, // information level LPBYTE pPrinter, // printer data buffer DWORD Command // printer-state command );Parameters hPrinter [in] Handle to the printer. Use the OpenPrinter or AddPrinter function to retrieve a printer handle. Level [in] Specifies the type of data that the function stores into the buffer pointed to by pPrinter. If the Command parameter is not equal to zero, the Level parameter must be zero. Windows 95/98/Me: This value can be 0, 2, or 5. Windows NT/2000/XP: This value can be 0, 2, 3, 4, 5, 7, 8, or 9. pPrinter [in] Pointer to a buffer containing data to set for the printer, or containing information for the command specified by the Command parameter. The type of data in the buffer is determined by the value of Level. Level Structure 0 If the Command parameter is PRINTER_CONTROL_SET_STATUS, pPrinter must contain a DWORD value that specifies the new printer status to set. For a list of the possible status values, see the Status member of the PRINTER_INFO_2 structure. Note that PRINTER_STATUS_PAUSED and PRINTER_STATUS_PENDING_DELETION are not valid status values to set. If Level is 0, but the Command parameter is not PRINTER_CONTROL_SET_STATUS, pPrinter must be NULL.
2 A PRINTER_INFO_2 structure containing detailed information about the printer. 3 Windows NT/2000/XP: A PRINTER_INFO_3 structure containing the printer's security information. 4 Windows NT/2000/XP: A PRINTER_INFO_4 structure containing minimal printer information, including the name of the printer, the name of the server, and whether the printer is remote or local. 5 A PRINTER_INFO_5 structure containing printer information such as printer attributes and time-out settings. 7 Windows 2000/XP: A PRINTER_INFO_7 structure. The dwAction member of this structure indicates whether SetPrinter should publish, unpublish, re-publish, or update the printer's data in the directory service. 8 Windows 2000/XP: A PRINTER_INFO_8 structure specifying the global default printer settings. 9 Windows 2000/XP: A PRINTER_INFO_9 structure specifying the per-user default printer settings. Command [in] Specifies the action performed by the SetPrinter function. If the Level parameter is nonzero, Command must be zero. In this case, the printer retains its current state and the function reconfigures the printer data as specified by the Level and pPrinter parameters. If the Level parameter is zero, Command can be one of the following values to set the state of the printer. Value Meaning PRINTER_CONTROL_PAUSE Pauses the printer. PRINTER_CONTROL_PURGE Deletes all print jobs in the printer. PRINTER_CONTROL_RESUME Resumes a paused printer. PRINTER_CONTROL_SET_STATUS Sets the printer status. The pPrinter parameter is a pointer to a DWORD that specifies the new printer status. Return Values If the function succeeds, the return value is a nonzero value.If the function fails, the return value is zero. To get extended error information, call GetLastError. Windows 2000/XP: If Level is 7 and the publish action failed, SetPrinter returns ERROR_IO_PENDING and attempts to complete the action in the background. If Level is 7 and the update action failed, SetPrinter returns ERROR_FILE_NOT_FOUND.
你大概需要这些东西 Private Type PRINTER_INFO_1 flags As Long pDescription As String pName As String pComment As String End Type Private Type PRINTER_INFO_2 pServerName As String pPrinterName As String pShareName As String pPortName As String pDriverName As String pComment As String pLocation As String pDevMode As DEVMODE pSepFile As String pPrintProcessor As String pDatatype As String pParameters As String pSecurityDescriptor As SECURITY_DESCRIPTOR Attributes As Long Priority As Long DefaultPriority As Long StartTime As Long UntilTime As Long Status As Long cJobs As Long AveragePPM As Long End Type Private Type PRINTER_INFO_3 pSecurityDescriptor As SECURITY_DESCRIPTOR End Type Private Type PRINTER_INFO_4 pPrinterName As String pServerName As String Attributes As Long End Type Private Type PRINTER_INFO_5 pPrinterName As String pPortName As String Attributes As Long DeviceNotSelectedTimeout As Long TransmissionRetryTimeout As Long End Type
Public Declare Function SetPrinter Lib "winspool.drv" Alias "SetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pPrinter As Long, ByVal Command As Long) As Longret& = SetPrinter(hPrinter, 0, VBNull, PRINTER_CONTROL_PAUSE) 或 ret& = SetPrinter(hPrinter, 0, 0, PRINTER_CONTROL_PAUSE) 试试
As Long,以便传递零值)Command -------- Long,下述值之一:
零
根据PRINTER_INFO_x结构改变打印机
PRINTER_CONTROL_PAUSE
暂停打印机
PRINTER_CONTROL_PURGE
删除打印机的所有作业
PRINTER_CONTROL_RESUME
恢复一台暂停的打印机
PRINTER_CONTROL_SET_STATUS
载入打印机的PRINTER_CONTROL_STATUS结构(不可在NT 3.51下使用)
以下摘自MSDNSetPrinter
The SetPrinter function sets the data for a specified printer or sets the state of the specified printer by pausing printing, resuming printing, or clearing all print jobs. BOOL SetPrinter(
HANDLE hPrinter, // handle to printer object
DWORD Level, // information level
LPBYTE pPrinter, // printer data buffer
DWORD Command // printer-state command
);Parameters
hPrinter
[in] Handle to the printer. Use the OpenPrinter or AddPrinter function to retrieve a printer handle.
Level
[in] Specifies the type of data that the function stores into the buffer pointed to by pPrinter. If the Command parameter is not equal to zero, the Level parameter must be zero.
Windows 95/98/Me: This value can be 0, 2, or 5.
Windows NT/2000/XP: This value can be 0, 2, 3, 4, 5, 7, 8, or 9. pPrinter
[in] Pointer to a buffer containing data to set for the printer, or containing information for the command specified by the Command parameter. The type of data in the buffer is determined by the value of Level. Level Structure
0 If the Command parameter is PRINTER_CONTROL_SET_STATUS, pPrinter must contain a DWORD value that specifies the new printer status to set. For a list of the possible status values, see the Status member of the PRINTER_INFO_2 structure. Note that PRINTER_STATUS_PAUSED and PRINTER_STATUS_PENDING_DELETION are not valid status values to set.
If Level is 0, but the Command parameter is not PRINTER_CONTROL_SET_STATUS, pPrinter must be NULL.
2 A PRINTER_INFO_2 structure containing detailed information about the printer.
3 Windows NT/2000/XP: A PRINTER_INFO_3 structure containing the printer's security information.
4 Windows NT/2000/XP: A PRINTER_INFO_4 structure containing minimal printer information, including the name of the printer, the name of the server, and whether the printer is remote or local.
5 A PRINTER_INFO_5 structure containing printer information such as printer attributes and time-out settings.
7 Windows 2000/XP: A PRINTER_INFO_7 structure. The dwAction member of this structure indicates whether SetPrinter should publish, unpublish, re-publish, or update the printer's data in the directory service.
8 Windows 2000/XP: A PRINTER_INFO_8 structure specifying the global default printer settings.
9 Windows 2000/XP: A PRINTER_INFO_9 structure specifying the per-user default printer settings.
Command
[in] Specifies the action performed by the SetPrinter function.
If the Level parameter is nonzero, Command must be zero. In this case, the printer retains its current state and the function reconfigures the printer data as specified by the Level and pPrinter parameters. If the Level parameter is zero, Command can be one of the following values to set the state of the printer.
Value Meaning
PRINTER_CONTROL_PAUSE Pauses the printer.
PRINTER_CONTROL_PURGE Deletes all print jobs in the printer.
PRINTER_CONTROL_RESUME Resumes a paused printer.
PRINTER_CONTROL_SET_STATUS Sets the printer status. The pPrinter parameter is a pointer to a DWORD that specifies the new printer status.
Return Values
If the function succeeds, the return value is a nonzero value.If the function fails, the return value is zero. To get extended error information, call GetLastError. Windows 2000/XP: If Level is 7 and the publish action failed, SetPrinter returns ERROR_IO_PENDING and attempts to complete the action in the background. If Level is 7 and the update action failed, SetPrinter returns ERROR_FILE_NOT_FOUND.
例如暂停打印ret& = SetPrinter(hPrinter, byval 0&, byval 0&, PRINTER_CONTROL_PAUSE)
无论command的值为何ret总是为0.
Private Type PRINTER_INFO_1
flags As Long
pDescription As String
pName As String
pComment As String
End Type
Private Type PRINTER_INFO_2
pServerName As String
pPrinterName As String
pShareName As String
pPortName As String
pDriverName As String
pComment As String
pLocation As String
pDevMode As DEVMODE
pSepFile As String
pPrintProcessor As String
pDatatype As String
pParameters As String
pSecurityDescriptor As SECURITY_DESCRIPTOR
Attributes As Long
Priority As Long
DefaultPriority As Long
StartTime As Long
UntilTime As Long
Status As Long
cJobs As Long
AveragePPM As Long
End Type
Private Type PRINTER_INFO_3
pSecurityDescriptor As SECURITY_DESCRIPTOR
End Type
Private Type PRINTER_INFO_4
pPrinterName As String
pServerName As String
Attributes As Long
End Type
Private Type PRINTER_INFO_5
pPrinterName As String
pPortName As String
Attributes As Long
DeviceNotSelectedTimeout As Long
TransmissionRetryTimeout As Long
End Type
或
ret& = SetPrinter(hPrinter, 0, 0, PRINTER_CONTROL_PAUSE)
试试