2012-04-11 4 views
1

Windows 용 자동 프린터 설치 프로그램을 만들려고합니다.Windows에서 프린터 이름 목록을 가져올 수 있습니까?

프린터 목록을 가져 오려면 어떻게해야합니까? 명령 줄에서 VB 스크립트를 사용하여 목록을 가져 오는 방법이 있다는 것을 알고 있지만 필자가 필요로하지 않는 추가 정보를 제공합니다. 파이썬으로 데이터를 가져올 좋은 방법이 없습니다.

이렇게하는 이유는 값을 가져 와서 목록에 넣은 다음 다른 목록에 대해 값을 확인하게하는 것입니다. 하나의 목록에있는 항목은 모두 제거됩니다. 이렇게하면 프로그램이 중복 프린터를 설치하지 않게됩니다.

답변

3

당신은 pywin32win32print.EnumPrinters() (편리), 또는이 ctypes 모듈 (낮은 의존성)를 통해 EnumPrinters() API를 호출 할 수 있습니다.

오류 확인 기능이없는 ctypes 버전이 있습니다.

# Use EnumPrintersW to list local printers with their names and descriptions. 
# Tested with CPython 2.7.10 and IronPython 2.7.5. 

import ctypes 
from ctypes.wintypes import BYTE, DWORD, LPCWSTR 

winspool = ctypes.WinDLL('winspool.drv') # for EnumPrintersW 
msvcrt = ctypes.cdll.msvcrt # for malloc, free 

# Parameters: modify as you need. See MSDN for detail. 
PRINTER_ENUM_LOCAL = 2 
Name = None # ignored for PRINTER_ENUM_LOCAL 
Level = 1 # or 2, 4, 5 

class PRINTER_INFO_1(ctypes.Structure): 
    _fields_ = [ 
     ("Flags", DWORD), 
     ("pDescription", LPCWSTR), 
     ("pName", LPCWSTR), 
     ("pComment", LPCWSTR), 
    ] 

# Invoke once with a NULL pointer to get buffer size. 
info = ctypes.POINTER(BYTE)() 
pcbNeeded = DWORD(0) 
pcReturned = DWORD(0) # the number of PRINTER_INFO_1 structures retrieved 
winspool.EnumPrintersW(PRINTER_ENUM_LOCAL, Name, Level, ctypes.byref(info), 0, 
     ctypes.byref(pcbNeeded), ctypes.byref(pcReturned)) 

bufsize = pcbNeeded.value 
buffer = msvcrt.malloc(bufsize) 
winspool.EnumPrintersW(PRINTER_ENUM_LOCAL, Name, Level, buffer, bufsize, 
     ctypes.byref(pcbNeeded), ctypes.byref(pcReturned)) 
info = ctypes.cast(buffer, ctypes.POINTER(PRINTER_INFO_1)) 
for i in range(pcReturned.value): 
    print info[i].pName, '=>', info[i].pDescription 
msvcrt.free(buffer) 
+0

IronPython 2.7에서 ctypes를 사용하여 EnumPrinters()를 호출하는 예제를 제공 할 수 있습니까? 나는 ctypes.EnumPrinters()를 호출 해보고 오류가 발생했습니다. 고맙습니다,. – konrad

+0

물론입니다. 'EnumPrintersA' 또는'EnumPrintersW'로 조건부로 정의 된'EnumPrinters'를 직접 호출 할 수 없다는 것에주의하십시오. 위의 편집 된 대답은'EnumPrintersW'를 사용합니다. – wdscxsj

+0

와우! 이것은 꽤 놀랍습니다. 이렇게 정교한 대답을 해주셔서 정말 고마워요. 그것은 위대한 작품. 한 번 더 질문 할 수 있으면 내가 갈 것입니다. 이제는 사용 가능한 프린터 정보가 생겼으니, ADOBE PDF 인쇄 환경 설정과 특히 모든 인쇄물이 덤프되는 Adobe PDF 출력 폴더를 변경하는 중 하나가 더 있습니다. MSDN에서 PrinterProperties() 메서드를 발견했습니다. 그게 내가 필요한 걸 얻을 수 있을까? – konrad

관련 문제