How to use EnumPrinters function to enum all printers
-
What's wrong with my code ? CString PrintName; PRINTER_INFO_1 PrintInfo[3]; LPDWORD pNeedSize; LPDWORD pPrinters; DWORD cbSize; cbSize = sizeof(PrintInfo); if ( EnumPrinters(PRINTER_ENUM_NAME,(LPTSTR)&PrintName,1,(LPBYTE)PrintInfo,cbSize,pNeedSize,pPrinters) != 0 ) { this->MessageBox("SUCC"); } else { int err = ::GetLastError(); this->MessageBox("fail"); } d
-
What's wrong with my code ? CString PrintName; PRINTER_INFO_1 PrintInfo[3]; LPDWORD pNeedSize; LPDWORD pPrinters; DWORD cbSize; cbSize = sizeof(PrintInfo); if ( EnumPrinters(PRINTER_ENUM_NAME,(LPTSTR)&PrintName,1,(LPBYTE)PrintInfo,cbSize,pNeedSize,pPrinters) != 0 ) { this->MessageBox("SUCC"); } else { int err = ::GetLastError(); this->MessageBox("fail"); } d
One thing i can see is
CString PrintName;
...
EnumPrinters(PRINTER_ENUM_NAME,(LPTSTR)&PrintName,1,(LPBYTE)PrintInfo,cbSize,pNeedSize,pPrinters)it should be
EnumPrinters(PRINTER_ENUM_NAME,PrintName.GetBuffer(100),1,(LPBYTE)PrintInfo,cbSize,pNeedSize,pPrinters)
Is there anyother error that you get ?
MSN Messenger. prakashnadar@msn.com Tip of the day of visual C++ IDE. "We use it before you do! Visual C++ was developed using Visual C++"
-
What's wrong with my code ? CString PrintName; PRINTER_INFO_1 PrintInfo[3]; LPDWORD pNeedSize; LPDWORD pPrinters; DWORD cbSize; cbSize = sizeof(PrintInfo); if ( EnumPrinters(PRINTER_ENUM_NAME,(LPTSTR)&PrintName,1,(LPBYTE)PrintInfo,cbSize,pNeedSize,pPrinters) != 0 ) { this->MessageBox("SUCC"); } else { int err = ::GetLastError(); this->MessageBox("fail"); } d
I think you need to call EnumPrinters twice, the first time to find out how much memory is required to store the information returned, and the second time to actually get that information. It probably doesn't work unless you allocate enough memory for the information returned. Also, to enumerate all printers, use PRINTER_ENUM_LOCAL refer MSDN for details http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/prntspol_9fjn.asp[^] sample code
DWORD dwSizeNeeded = 0; DWORD dwPrinterCount = 0; BOOL bEnumResult; CString strErr; HANDLE hProcessHeap = GetProcessHeap(); LPPRINTER_INFO_2 lpPrinterData; // call once to find buffer size EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &dwSizeNeeded, &dwPrinterCount); // allocate buffer memory if ((lpPrinterData = (LPPRINTER_INFO_2)HeapAlloc(hProcessHeap, HEAP_ZERO_MEMORY, dwSizeNeeded)) == NULL) { // error return; } // call again with new buffer size bEnumResult = EnumPrinters (PRINTER_ENUM_LOCAL, NULL, 2, (LPBYTE)lpPrinterData, dwSizeNeeded, &dwSizeNeeded, &dwPrinterCount); if (bEnumResult) { if (dwPrinterCount == 0) { // no printers installed HeapFree(hProcessHeap, 0, lpPrinterData); return; } // got data, process it here } // clean up and return HeapFree(hProcessHeap, 0, lpPrinterData); return;