static LPBYTE GetDriverInfo(HANDLE hPrinter, int nLevel) { LPBYTE pData = NULL; DWORD cbNeeded; /* // Make an initial call to see how much data we need. */ if (!GetPrinterDriver(hPrinter, NULL, nLevel, NULL, 0, &cbNeeded)) { /* // Allocate memory for the data. */ DWORD cbBuf = cbNeeded; pData = new BYTE[cbBuf]; /* // Get the data. */ if (!GetPrinterDriver(hPrinter, NULL, nLevel, pData, cbBuf, &cbNeeded)) { delete pData; pData = NULL; } } return pData; }
LPBYTE WrapGetPrinterDriver (HANDLE hHeap, HANDLE hPrinter, DWORD dwLevel) { // //Get the PrinterINFO so that we know where the driver help file is. // BOOL bGet = TRUE; DWORD dwNeeded = 0; LPBYTE pBuffer = NULL; bGet = GetPrinterDriver(hPrinter, NULL, dwLevel, pBuffer, 0, &dwNeeded); if(!bGet && GetLastError() == ERROR_INSUFFICIENT_BUFFER ) { SetLastError(ERROR_SUCCESS); // clear the previous result to prevent noise pBuffer = (LPBYTE)HeapAlloc (hHeap, HEAP_ZERO_MEMORY, dwNeeded); } if (pBuffer) { bGet = GetPrinterDriver(hPrinter, NULL, dwLevel, pBuffer, dwNeeded, &dwNeeded); } if(!bGet || !pBuffer) { if (pBuffer) { HeapFree(hHeap, 0, pBuffer); pBuffer = NULL; } ERR("GetPrinterDriver() failed."); } return pBuffer; }
static BOOL test_GetPrinterDriver(struct torture_context *tctx, LPSTR printername, LPSTR architecture, HANDLE handle) { DWORD levels[] = { 1, 2, 3, 4, 5, 6, 8 }; DWORD success[] = { 1, 1, 1, 1, 1, 1, 1 }; DWORD i; LPBYTE buffer = NULL; for (i=0; i < ARRAY_SIZE(levels); i++) { DWORD needed = 0; DWORD err = 0; char tmp[1024]; torture_comment(tctx, "Testing GetPrinterDriver level %d", levels[i]); GetPrinterDriver(handle, architecture, levels[i], NULL, 0, &needed); err = GetLastError(); if (err == ERROR_INSUFFICIENT_BUFFER) { err = 0; buffer = malloc(needed); torture_assert(tctx, buffer, "malloc failed"); if (!GetPrinterDriver(handle, architecture, levels[i], buffer, needed, &needed)) { err = GetLastError(); } } if (err) { sprintf(tmp, "GetPrinterDriver failed level %d on [%s] (buffer size = %d), error: %s\n", levels[i], printername, needed, errstr(err)); if (success[i]) { torture_fail(tctx, tmp); } else { torture_warning(tctx, tmp); } } if (tctx->print) { print_driver_info_bylevel(levels[i], buffer, 1); } free(buffer); buffer = NULL; } return TRUE; }
PVOID MyGetPrinterDriver( HANDLE hPrinter, DWORD level ) /*++ Routine Description: Wrapper function for GetPrinterDriver spooler API Arguments: hPrinter - Handle to the printer of interest level - Specifies the level of DRIVER_INFO_x structure Return Value: Pointer to a DRIVER_INFO_x structure, NULL if there is an error --*/ { PBYTE pDriverInfo = NULL; DWORD cbNeeded; if (!GetPrinterDriver(hPrinter, NULL, level, NULL, 0, &cbNeeded) && GetLastError() == ERROR_INSUFFICIENT_BUFFER && (pDriverInfo = MemAlloc(cbNeeded)) && GetPrinterDriver(hPrinter, NULL, level, pDriverInfo, cbNeeded, &cbNeeded)) { return pDriverInfo; } Error(("GetPrinterDriver failed\n")); MemFree(pDriverInfo); return NULL; }
LPWSTR GetPlotHelpFile( PPRINTERINFO pPI ) /*++ Routine Description: This function setup the directory path for the driver Help file Arguments: hPrinter - Handle to the printer Return Value: LPWSTR to the full path HelpFile, NULL if failed Development History: 01-Nov-1995 Wed 18:43:40 created Revision History: --*/ { PDRIVER_INFO_3 pDI3 = NULL; LPWSTR pHelpFile = NULL; WCHAR HelpFileName[MAX_HELPFILE_NAME]; DWORD cb; DWORD cb2; DWORD dwMemAllocSize = 0; HRESULT hr = E_FAIL; if (pPI->pHelpFile) { return(pPI->pHelpFile); } if ((!GetPrinterDriver(pPI->hPrinter, NULL, 3, NULL, 0, &cb)) && (GetLastError() == ERROR_INSUFFICIENT_BUFFER) && (pDI3 = (PDRIVER_INFO_3)LocalAlloc(LMEM_FIXED, cb)) && (GetPrinterDriver(pPI->hPrinter, NULL, 3, (LPBYTE)pDI3, cb, &cb)) && (pDI3->pHelpFile) && (SUCCEEDED(DWordAdd((DWORD)wcslen(pDI3->pHelpFile), 1, &dwMemAllocSize))) && (SUCCEEDED(DWordMult(dwMemAllocSize, sizeof(WCHAR), &dwMemAllocSize))) && (pHelpFile = (LPWSTR)LocalAlloc(LMEM_FIXED, dwMemAllocSize))) { hr = StringCchCopyW(pHelpFile, dwMemAllocSize / sizeof(WCHAR), (LPWSTR)pDI3->pHelpFile); } else if ((cb2 = LoadString(hPlotUIModule, IDS_HELP_FILENAME, &HelpFileName[1], COUNT_ARRAY(HelpFileName) - 1)) && (SUCCEEDED(DWordAdd(cb2, 1, &cb2))) && (SUCCEEDED(DWordMult(cb2, sizeof(WCHAR), &cb2))) && (!GetPrinterDriverDirectory(NULL, NULL, 1, NULL, 0, &cb)) && (GetLastError() == ERROR_INSUFFICIENT_BUFFER) && (SUCCEEDED(DWordAdd(cb, cb2, &dwMemAllocSize))) && (pHelpFile = (LPWSTR)LocalAlloc(LMEM_FIXED, dwMemAllocSize)) && (GetPrinterDriverDirectory(NULL, NULL, 1, (LPBYTE)pHelpFile, cb, &cb))) { HelpFileName[0] = L'\\'; hr = StringCchCatW(pHelpFile, dwMemAllocSize / sizeof(WCHAR), HelpFileName); } if (pDI3) { LocalFree((HLOCAL)pDI3); pDI3 = NULL; } if (pHelpFile && !SUCCEEDED(hr)) { LocalFree(pHelpFile); pHelpFile = NULL; } PLOTDBG(DBG_SHOW_HELP, ("GetlotHelpFile: '%ws", (pHelpFile) ? pHelpFile : L"Failed")); return(pPI->pHelpFile = pHelpFile); }
void vHelpInit( HANDLE hPrinter, BOOL bInitHelpFileOnly ) { DWORD dwSize; /* See how much storage is needed for file name */ DWORD cb; PDRIVER_INFO_3 pDriverInfo = NULL; PWSTR pDriverDirectory; /* Comes from the spooler */ if( pwszHelpFile == NULL ) { // Attempt to get help file name using the new DRIVER_INFO_3 if (! GetPrinterDriver(hPrinter, NULL, 3, NULL, 0, &cb) && GetLastError() == ERROR_INSUFFICIENT_BUFFER && (pDriverInfo = HeapAlloc( hHeap, 0, cb)) && GetPrinterDriver(hPrinter, NULL, 3, (PBYTE) pDriverInfo, cb, &cb) && pDriverInfo->pHelpFile != NULL) { pwszHelpFile = GetStringFromUnicode(pDriverInfo->pHelpFile, hHeap); } if (pDriverInfo != NULL) { HeapFree( hHeap, 0, pDriverInfo ); } // If DRIVER_INFO_3 isn't supported, get help file name // using the same old method as before if (pwszHelpFile == NULL) { pDriverDirectory = GetDriverDirectory( hHeap, hPrinter ); if( pDriverDirectory ) { /* Grab some memory & fill it in */ dwSize = (1 + wcslen( wcHelpFNM ) + wcslen( pDriverDirectory ) ) * sizeof( WCHAR ); if( pwszHelpFile = (WCHAR *)HeapAlloc( hHeap, 0, dwSize ) ) { wcscpy( pwszHelpFile, pDriverDirectory ); wcscat( pwszHelpFile, wcHelpFNM ); /* The name */ } else { HeapFree( hHeap, 0, (LPSTR)pDriverDirectory ); } } else { return ; /* Whatever it is, this should not happen */ } HeapFree( hHeap, 0, (LPSTR)pDriverDirectory ); } } /* * All we need to do (and only the first time!) is put a hook function * onto the message queue so that we can intercept the <F1> key and * transform it into a mouse click on the "Help" button. Then the * normal windowing code will display the help contents.This should be * done only for old type of help, like softfont in PrinterProperties. */ if( cHelpInit == 0 && !bInitHelpFileOnly ) { /* First time only */ hhookGetMsg = SetWindowsHookEx( WH_GETMESSAGE, GetMsgProc, hModule, GetCurrentThreadId() ); } ++cHelpInit; /* Only do it once */ return ; }
LPWSTR GetPlotHelpFile( PPRINTERINFO pPI ) /*++ Routine Description: This function setup the directory path for the driver Help file Arguments: hPrinter - Handle to the printer Return Value: LPWSTR to the full path HelpFile, NULL if failed Author: 01-Nov-1995 Wed 18:43:40 created -by- Daniel Chou (danielc) Revision History: --*/ { PDRIVER_INFO_3 pDI3 = NULL; LPWSTR pHelpFile = NULL; WCHAR HelpFileName[MAX_HELPFILE_NAME]; DWORD cb; DWORD cb2; if (pPI->pHelpFile) { return(pPI->pHelpFile); } if ((!GetPrinterDriver(pPI->hPrinter, NULL, 3, NULL, 0, &cb)) && (GetLastError() == ERROR_INSUFFICIENT_BUFFER) && (pDI3 = (PDRIVER_INFO_3)LocalAlloc(LMEM_FIXED, cb)) && (GetPrinterDriver(pPI->hPrinter, NULL, 3, (LPBYTE)pDI3, cb, &cb)) && (pDI3->pHelpFile) && (pHelpFile = (LPWSTR)LocalAlloc(LMEM_FIXED, cbWSTR(pDI3->pHelpFile)))) { wcscpy(pHelpFile, (LPWSTR)pDI3->pHelpFile); } else if ((cb2 = LoadString(hPlotUIModule, IDS_HELP_FILENAME, &HelpFileName[1], COUNT_ARRAY(HelpFileName) - 1)) && (cb2 = (cb2 + 1) * sizeof(WCHAR)) && (!GetPrinterDriverDirectory(NULL, NULL, 1, NULL, 0, &cb)) && (GetLastError() == ERROR_INSUFFICIENT_BUFFER) && (pHelpFile = (LPWSTR)LocalAlloc(LMEM_FIXED, cb + cb2)) && (GetPrinterDriverDirectory(NULL, NULL, 1, (LPBYTE)pHelpFile, cb, &cb))) { HelpFileName[0] = L'\\'; wcscat(pHelpFile, HelpFileName); } else if (pHelpFile) { LocalFree(pHelpFile); pHelpFile = NULL; } if (pDI3) { LocalFree((HLOCAL)pDI3); } PLOTDBG(DBG_SHOW_HELP, ("GetlotHelpFile: '%ws", (pHelpFile) ? pHelpFile : L"Failed")); return(pPI->pHelpFile = pHelpFile); }
BOOL DisplayPrinterDriverInfo (HWND hwnd) { HANDLE hPrinter; DWORD dwBytesNeeded; DRIVER_INFO_1 *pDriverInfo1; DRIVER_INFO_2 *pDriverInfo2; char buf[BUFSIZE]; char pEnvironment[BUFSIZE] = ""; BOOL bReturn = TRUE; // // open selected printer & alloc buffers & get sundry info, close printer // OpenPrinter (gszDeviceName, &hPrinter, NULL); if (!hPrinter) { char buf[BUFSIZE]; _snprintf_s (buf, BUFSIZE, _TRUNCATE, GetStringRes(IDS_FMT_OPNPRTFAIL), gszDeviceName); ErrMsgBox ((LPCSTR) buf, ERR_MOD_NAME); bReturn = FALSE; goto display_prt_drv_info_done1; } GetPrinterDriver (hPrinter, pEnvironment, 1, NULL, 0, &dwBytesNeeded); // // simple error checking, if these work assume rest will too // if (!(pDriverInfo1 = (DRIVER_INFO_1 *) LocalAlloc (LPTR, dwBytesNeeded))) { ErrMsgBox (GetStringRes(IDS_LALLOCFAIL), ERR_MOD_NAME); bReturn = FALSE; goto display_prt_drv_info_done1; } if (!GetPrinterDriver (hPrinter, pEnvironment, 1, (LPBYTE) pDriverInfo1, dwBytesNeeded, &dwBytesNeeded)) { ErrMsgBox (GetStringRes(IDS_GETPRTDRVFAIL), ERR_MOD_NAME); bReturn = FALSE; goto display_prt_drv_info_done2; } GetPrinterDriver (hPrinter, pEnvironment, 2, NULL, 0, &dwBytesNeeded); pDriverInfo2 = (DRIVER_INFO_2 *) LocalAlloc (LPTR, dwBytesNeeded); GetPrinterDriver (hPrinter, pEnvironment, 2, (LPBYTE) pDriverInfo2, dwBytesNeeded, &dwBytesNeeded); ClosePrinter (hPrinter); // // shove info in listbox // if ( _snprintf_s (buf, BUFSIZE, _TRUNCATE, gaDrvInfo[0]) > 0 ) outstr(); if ( _snprintf_s (buf, BUFSIZE, _TRUNCATE, gaDrvInfo[1], pDriverInfo1->pName) > 0 ) outstr(); if ( _snprintf_s (buf, BUFSIZE, _TRUNCATE, gaDrvInfo[2]) > 0 ) outstr(); if ( _snprintf_s (buf, BUFSIZE, _TRUNCATE, gaDrvInfo[3], pDriverInfo2->cVersion) > 0 ) outstr(); if ( _snprintf_s (buf, BUFSIZE, _TRUNCATE, gaDrvInfo[4], pDriverInfo2->pName) > 0 ) outstr(); if ( _snprintf_s (buf, BUFSIZE, _TRUNCATE, gaDrvInfo[5], pDriverInfo2->pEnvironment) > 0 ) outstr(); if ( _snprintf_s (buf, BUFSIZE, _TRUNCATE, gaDrvInfo[6], pDriverInfo2->pDriverPath) > 0 ) outstr(); if ( _snprintf_s (buf, BUFSIZE, _TRUNCATE, gaDrvInfo[7], pDriverInfo2->pDataFile) > 0 ) outstr(); if ( _snprintf_s (buf, BUFSIZE, _TRUNCATE, gaDrvInfo[8], pDriverInfo2->pConfigFile) > 0 ) outstr(); LocalFree (LocalHandle (pDriverInfo2)); display_prt_drv_info_done2: LocalFree (LocalHandle (pDriverInfo1)); display_prt_drv_info_done1: return bReturn; }