winspool: Add a helper to open a printer's registry key.

This commit is contained in:
Huw Davies 2012-04-19 13:43:24 +01:00 committed by Alexandre Julliard
parent f6524a9f77
commit b9bc9a0cf2
1 changed files with 30 additions and 49 deletions

View File

@ -995,6 +995,21 @@ static LPCWSTR get_opened_printer_name(HANDLE hprn)
return printer->name; return printer->name;
} }
static DWORD open_printer_reg_key( const WCHAR *name, HKEY *key )
{
HKEY printers;
DWORD err;
*key = NULL;
err = RegCreateKeyW( HKEY_LOCAL_MACHINE, PrintersW, &printers );
if (err) return err;
err = RegOpenKeyW( printers, name, key );
if (err) err = ERROR_INVALID_PRINTER_NAME;
RegCloseKey( printers );
return err;
}
/****************************************************************** /******************************************************************
* WINSPOOL_GetOpenedPrinterRegKey * WINSPOOL_GetOpenedPrinterRegKey
* *
@ -1002,24 +1017,9 @@ static LPCWSTR get_opened_printer_name(HANDLE hprn)
static DWORD WINSPOOL_GetOpenedPrinterRegKey(HANDLE hPrinter, HKEY *phkey) static DWORD WINSPOOL_GetOpenedPrinterRegKey(HANDLE hPrinter, HKEY *phkey)
{ {
LPCWSTR name = get_opened_printer_name(hPrinter); LPCWSTR name = get_opened_printer_name(hPrinter);
DWORD ret;
HKEY hkeyPrinters;
if(!name) return ERROR_INVALID_HANDLE; if(!name) return ERROR_INVALID_HANDLE;
return open_printer_reg_key( name, phkey );
if((ret = RegCreateKeyW(HKEY_LOCAL_MACHINE, PrintersW, &hkeyPrinters)) !=
ERROR_SUCCESS)
return ret;
if(RegOpenKeyW(hkeyPrinters, name, phkey) != ERROR_SUCCESS)
{
ERR("Can't find opened printer %s in registry\n",
debugstr_w(name));
RegCloseKey(hkeyPrinters);
return ERROR_INVALID_PRINTER_NAME; /* ? */
}
RegCloseKey(hkeyPrinters);
return ERROR_SUCCESS;
} }
void WINSPOOL_LoadSystemPrinters(void) void WINSPOOL_LoadSystemPrinters(void)
@ -3872,29 +3872,17 @@ static BOOL WINSPOOL_GetPrinter_9(HKEY hkeyPrinter, PRINTER_INFO_9W *pi9, LPBYTE
BOOL WINAPI GetPrinterW(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, BOOL WINAPI GetPrinterW(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter,
DWORD cbBuf, LPDWORD pcbNeeded) DWORD cbBuf, LPDWORD pcbNeeded)
{ {
LPCWSTR name; DWORD size, needed = 0, err;
DWORD size, needed = 0;
LPBYTE ptr = NULL; LPBYTE ptr = NULL;
HKEY hkeyPrinter, hkeyPrinters; HKEY hkeyPrinter;
BOOL ret; BOOL ret;
TRACE("(%p,%d,%p,%d,%p)\n",hPrinter,Level,pPrinter,cbBuf, pcbNeeded); TRACE("(%p,%d,%p,%d,%p)\n",hPrinter,Level,pPrinter,cbBuf, pcbNeeded);
if (!(name = get_opened_printer_name(hPrinter))) { err = WINSPOOL_GetOpenedPrinterRegKey( hPrinter, &hkeyPrinter );
SetLastError(ERROR_INVALID_HANDLE); if (err)
return FALSE;
}
if(RegCreateKeyW(HKEY_LOCAL_MACHINE, PrintersW, &hkeyPrinters) !=
ERROR_SUCCESS) {
ERR("Can't create Printers key\n");
return FALSE;
}
if(RegOpenKeyW(hkeyPrinters, name, &hkeyPrinter) != ERROR_SUCCESS)
{ {
ERR("Can't find opened printer %s in registry\n", debugstr_w(name)); SetLastError( err );
RegCloseKey(hkeyPrinters);
SetLastError(ERROR_INVALID_PRINTER_NAME); /* ? */
return FALSE; return FALSE;
} }
@ -4016,13 +4004,11 @@ BOOL WINAPI GetPrinterW(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter,
default: default:
FIXME("Unimplemented level %d\n", Level); FIXME("Unimplemented level %d\n", Level);
SetLastError(ERROR_INVALID_LEVEL); SetLastError(ERROR_INVALID_LEVEL);
RegCloseKey(hkeyPrinters);
RegCloseKey(hkeyPrinter); RegCloseKey(hkeyPrinter);
return FALSE; return FALSE;
} }
RegCloseKey(hkeyPrinter); RegCloseKey(hkeyPrinter);
RegCloseKey(hkeyPrinters);
TRACE("returning %d needed = %d\n", ret, needed); TRACE("returning %d needed = %d\n", ret, needed);
if(pcbNeeded) *pcbNeeded = needed; if(pcbNeeded) *pcbNeeded = needed;
@ -4559,7 +4545,7 @@ BOOL WINAPI GetPrinterDriverW(HANDLE hPrinter, LPWSTR pEnvironment,
WCHAR DriverName[100]; WCHAR DriverName[100];
DWORD ret, type, size, needed = 0; DWORD ret, type, size, needed = 0;
LPBYTE ptr = NULL; LPBYTE ptr = NULL;
HKEY hkeyPrinter, hkeyPrinters, hkeyDrivers; HKEY hkeyPrinter, hkeyDrivers;
const printenv_t * env; const printenv_t * env;
TRACE("(%p,%s,%d,%p,%d,%p)\n",hPrinter,debugstr_w(pEnvironment), TRACE("(%p,%s,%d,%p,%d,%p)\n",hPrinter,debugstr_w(pEnvironment),
@ -4581,24 +4567,19 @@ BOOL WINAPI GetPrinterDriverW(HANDLE hPrinter, LPWSTR pEnvironment,
env = validate_envW(pEnvironment); env = validate_envW(pEnvironment);
if (!env) return FALSE; /* SetLastError() is in validate_envW */ if (!env) return FALSE; /* SetLastError() is in validate_envW */
if(RegCreateKeyW(HKEY_LOCAL_MACHINE, PrintersW, &hkeyPrinters) != ret = open_printer_reg_key( name, &hkeyPrinter );
ERROR_SUCCESS) { if (ret)
ERR("Can't create Printers key\n"); {
return FALSE;
}
if(RegOpenKeyW(hkeyPrinters, name, &hkeyPrinter)
!= ERROR_SUCCESS) {
ERR( "Can't find opened printer %s in registry\n", debugstr_w(name) ); ERR( "Can't find opened printer %s in registry\n", debugstr_w(name) );
RegCloseKey(hkeyPrinters); SetLastError( ret );
SetLastError(ERROR_INVALID_PRINTER_NAME); /* ? */
return FALSE; return FALSE;
} }
size = sizeof(DriverName); size = sizeof(DriverName);
DriverName[0] = 0; DriverName[0] = 0;
ret = RegQueryValueExW(hkeyPrinter, Printer_DriverW, 0, &type, ret = RegQueryValueExW(hkeyPrinter, Printer_DriverW, 0, &type,
(LPBYTE)DriverName, &size); (LPBYTE)DriverName, &size);
RegCloseKey(hkeyPrinter); RegCloseKey(hkeyPrinter);
RegCloseKey(hkeyPrinters);
if(ret != ERROR_SUCCESS) { if(ret != ERROR_SUCCESS) {
ERR("Can't get DriverName for printer %s\n", debugstr_w(name)); ERR("Can't get DriverName for printer %s\n", debugstr_w(name));
return FALSE; return FALSE;