/************************************************************************** * CPrintersEnum::CreatePrintersEnumList() */ BOOL CPrintersEnum::CreatePrintersEnumList(DWORD dwFlags) { BOOL ret = TRUE; TRACE("(%p)->(flags=0x%08lx) \n", this, dwFlags); /* enumerate the folders */ if (dwFlags & SHCONTF_NONFOLDERS) { DWORD needed = 0, num = 0, i; PRINTER_INFO_4W *pi; EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, 4, NULL, 0, &needed, &num); if (!needed) return ret; pi = (PRINTER_INFO_4W *)HeapAlloc(GetProcessHeap(), 0, needed); if(!EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, 4, (LPBYTE)pi, needed, &needed, &num)) { HeapFree(GetProcessHeap(), 0, pi); return FALSE; } for(i = 0; i < num; i++) { LPITEMIDLIST pidl = _ILCreatePrinterItem(&pi[i]); if (pidl) { if (!AddToEnumList(pidl)) SHFree(pidl); } } HeapFree(GetProcessHeap(), 0, pi); } return ret; }
QStringList PrinterUtil::getPrinterNames() { QString printerName; QStringList printerNames; #if defined (HAVE_CUPS) cups_dest_t *dests; int num_dests = cupsGetDests(&dests); for (int pr = 0; pr < num_dests; ++pr) { printerName = QString(dests[pr].name); printerNames.append(printerName); } cupsFreeDests(num_dests, dests); #elif defined(_WIN32) DWORD size; DWORD numPrinters; PRINTER_INFO_2W* printerInfos = NULL; EnumPrintersW ( PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS , NULL, 2, NULL, 0, &size, &numPrinters ); printerInfos = (PRINTER_INFO_2W*) malloc(size); if ( EnumPrintersW ( PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, NULL, 2, (LPBYTE) printerInfos, size, &size, &numPrinters ) ) { for ( uint i = 0; i < numPrinters; i++) { printerName = QString::fromUtf16( (const ushort*) printerInfos[i].pPrinterName ); printerNames.append(printerName); } printerNames.sort(); } if ( printerInfos) free(printerInfos); #else QString tmp; QString Pcap; QStringList wt; if (loadText("/etc/printcap", &Pcap)) { QDataStream ts(&Pcap, QIODevice::ReadOnly); while(!ts.atEnd()) { tmp = readLinefromDataStream(ts); if (tmp.isEmpty()) continue; if ((tmp[0] != '#') && (tmp[0] != ' ') && (tmp[0] != '\n') && (tmp[0] != '\t')) { tmp = tmp.trimmed(); tmp = tmp.left(tmp.length() - (tmp.right(2) == ":\\" ? 2 : 1)); wt = tmp.split("|", QString::SkipEmptyParts); printerName = wt[0]; printerNames.append(printerName); } } } #endif return printerNames; }
DWORD _RpcEnumPrinters(DWORD Flags, WINSPOOL_HANDLE Name, DWORD Level, BYTE* pPrinterEnum, DWORD cbBuf, DWORD* pcbNeeded, DWORD* pcReturned) { DWORD dwErrorCode; PBYTE pPrinterEnumAligned; dwErrorCode = RpcImpersonateClient(NULL); if (dwErrorCode != ERROR_SUCCESS) { ERR("RpcImpersonateClient failed with error %lu!\n", dwErrorCode); return dwErrorCode; } pPrinterEnumAligned = AlignRpcPtr(pPrinterEnum, &cbBuf); if (EnumPrintersW(Flags, Name, Level, pPrinterEnumAligned, cbBuf, pcbNeeded, pcReturned)) { // Replace absolute pointer addresses in the output by relative offsets. ASSERT(Level <= 9); MarshallDownStructuresArray(pPrinterEnumAligned, *pcReturned, pPrinterInfoMarshalling[Level]->pInfo, pPrinterInfoMarshalling[Level]->cbStructureSize, TRUE); } else { dwErrorCode = GetLastError(); } RpcRevertToSelf(); UndoAlignRpcPtr(pPrinterEnum, pPrinterEnumAligned, cbBuf, pcbNeeded); return dwErrorCode; }
uint printer_start_enum(void) { DWORD size = 0, num = 0; while (!EnumPrintersW(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS, 0, 4, (LPBYTE)printer_info, size, &size, &num) && GetLastError() == ERROR_INSUFFICIENT_BUFFER) printer_info = _realloc(printer_info, size); return num; }
int testPrinters(void) { DWORD size,ret,err; ret=EnumPrintersW ( PRINTER_ENUM_NAME, NULL, 1, NULL, 0, &size, &size ); if ( ret==0 ) { err=GetLastError(); if (err!=122) { //size error printf("[-] Printer Service not available - Error: %d\n",err ); exit(-1); } } return(1); }
bool MCScreenDC::listprinters(MCStringRef& r_printers) { MCAutoListRef t_list; if (!MCListCreateMutable('\n', &t_list)) return false; DWORD t_flags; DWORD t_level; t_flags = PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS; t_level = 4; DWORD t_printer_count; DWORD t_bytes_needed; t_printer_count = 0; t_bytes_needed = 0; if (EnumPrintersW(t_flags, NULL, t_level, NULL, 0, &t_bytes_needed, &t_printer_count) != 0 || GetLastError() == ERROR_INSUFFICIENT_BUFFER) { MCAutoPointer<byte_t> t_printers; if (!MCMemoryNewArray(t_bytes_needed, &t_printers)) return false; if (EnumPrintersW(t_flags, NULL, t_level, (LPBYTE)*t_printers, t_bytes_needed, &t_bytes_needed, &t_printer_count) != 0) { for(uint4 i = 0; i < t_printer_count; ++i) { MCAutoStringRef t_printer_name; if (!MCStringCreateWithWString(((PRINTER_INFO_4W *)*t_printers)[i] . pPrinterName, &t_printer_name)) return false; if (!MCListAppend(*t_list, *t_printer_name)) return false; } } } return MCListCopyAsString(*t_list, r_printers); }
wchar_t * UT_GetDefaultPrinterName() { UT_uint32 iBufferSize = 128; // will become 2x bigger immediately in the loop wchar_t * pPrinterName = NULL; DWORD rc; do { iBufferSize *= 2; if(pPrinterName) g_free(pPrinterName); pPrinterName = (wchar_t *) UT_calloc(sizeof(wchar_t),iBufferSize); UT_return_val_if_fail( pPrinterName, NULL ); // the method of obtaining the name is version specific ... OSVERSIONINFOW osvi; DWORD iNeeded, iReturned, iBuffSize; LPPRINTER_INFO_5W pPrinterInfo; wchar_t* p; osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW); GetVersionExW(&osvi); if (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) { // get size of the buffer needed to call enum printers if (!EnumPrintersW(PRINTER_ENUM_DEFAULT,NULL,5,NULL,0,&iNeeded,&iReturned)) { if ((rc = GetLastError()) != ERROR_INSUFFICIENT_BUFFER) { return NULL; } } // allocate the buffer if ((pPrinterInfo = (LPPRINTER_INFO_5W)LocalAlloc(LPTR,iNeeded)) == NULL) { rc = GetLastError(); } else { // now get the default printer if (!EnumPrintersW(PRINTER_ENUM_DEFAULT,NULL,5, (LPBYTE) pPrinterInfo,iNeeded,&iNeeded,&iReturned)) { rc = GetLastError(); } else { if (iReturned > 0) { // here we copy the name to our own buffer if ((DWORD) wcslen(pPrinterInfo->pPrinterName) > iBufferSize-1) { rc = ERROR_INSUFFICIENT_BUFFER; } else { wcscpy(pPrinterName,pPrinterInfo->pPrinterName); rc = ERROR_SUCCESS; } } else { *pPrinterName = '0'; rc = ERROR_SUCCESS; } } LocalFree(pPrinterInfo); } } else if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) { if (osvi.dwMajorVersion >= 5) /* Windows 2000 or later */ { iBuffSize = iBufferSize; HMODULE hWinSpool = LoadLibraryW(L"winspool.drv"); if (!hWinSpool) return NULL; HRESULT (WINAPI * fnGetDefaultPrinter)(LPWSTR, LPDWORD) = (HRESULT (WINAPI * )(LPWSTR, LPDWORD)) GetProcAddress(hWinSpool, GETDEFAULTPRINTER); if (!fnGetDefaultPrinter) { FreeLibrary(hWinSpool); return NULL; } bool i =false; if (!fnGetDefaultPrinter(pPrinterName,&iBuffSize)) i = true; if(i) rc = GetLastError(); else rc = ERROR_SUCCESS; FreeLibrary(hWinSpool); } else /* Windows NT 4.0 or earlier */ { if (GetProfileStringW(L"windows",L"device",L"",pPrinterName,iBufferSize) == iBufferSize-1) { rc = ERROR_INSUFFICIENT_BUFFER; } else { p = pPrinterName; while (*p != '0' && *p !=L',') ++p; *p = '0'; rc = ERROR_SUCCESS; } } } } while (rc == ERROR_INSUFFICIENT_BUFFER); return pPrinterName; }
DWORD ShowPrinterInfo(wchar_t *lpName, int level, int opt, char *padding) { unsigned char *lpInfo; int i,j; DWORD n; DWORD dwSizeNeeded=0; char newpadding[50]; DWORD ret; if (opt & OPT_UNICODE) { EnumPrintersW ( PRINTER_ENUM_NAME, (wchar_t* )lpName, level, NULL, 0, &dwSizeNeeded, &n ); } else { EnumPrintersA ( PRINTER_ENUM_NAME, (char *)lpName, level, NULL, 0, &dwSizeNeeded, &n ); } if (dwSizeNeeded==0) { #ifdef _DBG_ printf ( "EnumPrintersX() Invalid. Error: %d \n",GetLastError() ); #endif return(-1); } lpInfo = (void *)HeapAlloc ( GetProcessHeap (), HEAP_ZERO_MEMORY, dwSizeNeeded ); if ( lpInfo != NULL ) { if (opt & OPT_UNICODE) { ret=EnumPrintersW ( PRINTER_ENUM_NAME,(wchar_t *)lpName,level,(LPBYTE)lpInfo,dwSizeNeeded,&dwSizeNeeded,&n); } else { ret=EnumPrintersA ( PRINTER_ENUM_NAME,(char *)lpName,level,(LPBYTE)lpInfo,dwSizeNeeded,&dwSizeNeeded,&n); } if ( ret== 0 ) { #ifdef _DBG_ printf ( "EnumPrintersX() Failed. Error: %d ( %i)\n",GetLastError(),dwSizeNeeded ); #endif HeapFree ( GetProcessHeap (), 0, lpInfo ); return 0; } else { PRINTER_INFO_1 *dataI; PRINTER_INFO_2 *dataII; for ( i=0; i < n; i++ ) { dataI=(PRINTER_INFO_1*)lpInfo; printf("%s",padding); if (opt & OPT_UNICODE) { if (dataI[i].pName) printf(" %S - ",(dataI[i].pName)); if (dataI[i].pDescription) printf(" %S ",(dataI[i].pDescription)); //if (dataI[i].pComment) printf(" %S - ",(dataI[i].pComment)); } else { if (dataI[i].pName) printf(" %s - ",(dataI[i].pName)); if (dataI[i].pDescription) printf(" %s ",(dataI[i].pDescription)); //if (dataI[i].pComment) printf(" %s - ",(dataI[i].pComment)); } printf("\n"); for(j=0;j<sizeof(CustomFuzzSize)/sizeof(int);j++) { if (opt & OPT_UNICODE) { Fuzzer( (wchar_t *) dataI[0].pName, j,opt & OPT_UNICODE); ShowPrinterInfo((wchar_t*)dst,level, OPT_UNICODE, newpadding); } else { Fuzzer( (wchar_t *) dataI[0].pName, j,opt & OPT_UNICODE); ShowPrinterInfo((wchar_t*)dst,level, 0, newpadding); } } if (opt & RECURSIVE ) { strcpy (newpadding,padding); strcat(newpadding,"---"); newpadding[1]='+'; ShowPrinterInfo(dataI[i].pName,level, opt, newpadding); } printf("\n"); } HeapFree ( GetProcessHeap (), 0, lpInfo ); } } return(1); }
static void _RunRemoteTest(const char* szTestName) { BOOL bSuccessful = FALSE; char szBuffer[1024]; DWORD cbRead; DWORD cbWritten; HANDLE hCommandPipe = INVALID_HANDLE_VALUE; HANDLE hFind = INVALID_HANDLE_VALUE; HANDLE hOutputPipe = INVALID_HANDLE_VALUE; PWSTR p; SC_HANDLE hSC = NULL; SC_HANDLE hService = NULL; SERVICE_STATUS ServiceStatus; WCHAR wszFilePath[MAX_PATH + 20]; WIN32_FIND_DATAW fd; // Do a dummy EnumPrintersW call. // This guarantees that the Spooler Service has actually loaded localspl.dll, which is a requirement for our injected DLL to work properly. EnumPrintersW(PRINTER_ENUM_LOCAL | PRINTER_ENUM_NAME, NULL, 1, NULL, 0, &cbRead, &cbWritten); // Get the full path to our EXE file. if (!GetModuleFileNameW(NULL, wszFilePath, MAX_PATH)) { skip("GetModuleFileNameW failed with error %lu!\n", GetLastError()); goto Cleanup; } // Replace the extension. p = wcsrchr(wszFilePath, L'.'); if (!p) { skip("File path has no file extension: %S\n", wszFilePath); goto Cleanup; } wcscpy(p, L".dll"); // Check if the corresponding DLL file exists. hFind = FindFirstFileW(wszFilePath, &fd); if (hFind == INVALID_HANDLE_VALUE) { skip("My DLL file \"%S\" does not exist!\n", wszFilePath); goto Cleanup; } // Change the extension back to .exe and add the parameters. wcscpy(p, L".exe service dummy"); // Open a handle to the service manager. hSC = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (!hSC) { skip("OpenSCManagerW failed with error %lu!\n", GetLastError()); goto Cleanup; } // Ensure that the spooler service is running. hService = OpenServiceW(hSC, L"spooler", SERVICE_QUERY_STATUS); if (!hService) { skip("OpenServiceW failed for the spooler service with error %lu!\n", GetLastError()); goto Cleanup; } if (!QueryServiceStatus(hService, &ServiceStatus)) { skip("QueryServiceStatus failed for the spooler service with error %lu!\n", GetLastError()); goto Cleanup; } if (ServiceStatus.dwCurrentState != SERVICE_RUNNING) { skip("Spooler Service is not running!\n"); goto Cleanup; } CloseServiceHandle(hService); // Try to open the service if we've created it in a previous run. hService = OpenServiceW(hSC, SERVICE_NAME, SERVICE_ALL_ACCESS); if (!hService) { if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST) { // Create the service. hService = CreateServiceW(hSC, SERVICE_NAME, NULL, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_DEMAND_START, SERVICE_ERROR_IGNORE, wszFilePath, NULL, NULL, NULL, NULL, NULL); if (!hService) { skip("CreateServiceW failed with error %lu!\n", GetLastError()); goto Cleanup; } } else { skip("OpenServiceW failed with error %lu!\n", GetLastError()); goto Cleanup; } } // Create pipes for the communication with the injected DLL. hCommandPipe = CreateNamedPipeW(COMMAND_PIPE_NAME, PIPE_ACCESS_OUTBOUND, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 1, 1024, 1024, 10000, NULL); if (hCommandPipe == INVALID_HANDLE_VALUE) { skip("CreateNamedPipeW failed for the command pipe with error %lu!\n", GetLastError()); goto Cleanup; } hOutputPipe = CreateNamedPipeW(OUTPUT_PIPE_NAME, PIPE_ACCESS_INBOUND, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, 1, 1024, 1024, 10000, NULL); if (hOutputPipe == INVALID_HANDLE_VALUE) { skip("CreateNamedPipeW failed for the output pipe with error %lu!\n", GetLastError()); goto Cleanup; } // Start the service with "service" and a dummy parameter (to distinguish it from a call by rosautotest to localspl_apitest:service) if (!StartServiceW(hService, 0, NULL)) { skip("StartServiceW failed with error %lu!\n", GetLastError()); goto Cleanup; } // Wait till it has injected the DLL and the DLL expects its test name. if (!ConnectNamedPipe(hCommandPipe, NULL) && GetLastError() != ERROR_PIPE_CONNECTED) { skip("ConnectNamedPipe failed for the command pipe with error %lu!\n", GetLastError()); goto Cleanup; } // Send the test name. if (!WriteFile(hCommandPipe, szTestName, strlen(szTestName) + sizeof(char), &cbWritten, NULL)) { skip("WriteFile failed with error %lu!\n", GetLastError()); goto Cleanup; } // Now wait for the DLL to connect to the output pipe. if (!ConnectNamedPipe(hOutputPipe, NULL)) { skip("ConnectNamedPipe failed for the output pipe with error %lu!\n", GetLastError()); goto Cleanup; } // Get all testing messages from the pipe and output them on stdout. while (ReadFile(hOutputPipe, szBuffer, sizeof(szBuffer), &cbRead, NULL) && cbRead) fwrite(szBuffer, sizeof(char), cbRead, stdout); bSuccessful = TRUE; Cleanup: if (hCommandPipe != INVALID_HANDLE_VALUE) CloseHandle(hCommandPipe); if (hOutputPipe != INVALID_HANDLE_VALUE) CloseHandle(hOutputPipe); if (hFind != INVALID_HANDLE_VALUE) FindClose(hFind); if (hService) CloseServiceHandle(hService); if (hSC) CloseServiceHandle(hSC); // If we successfully received test output through the named pipe, we have also output a summary line already. // Prevent the testing framework from outputting another "0 tests executed" line in this case. if (bSuccessful) ExitProcess(0); }