似乎目前还没有优雅的方法可以直接在 Python 中实现这一点。我能想出的最接近的是一个用 c++ 编写的 dll,我可以调用它。我最终得到了完整的二进制 DevMode 结构,并且可以从中重新加载。该 c++ dll 的代码如下所示:
#include "stdafx.h"
#include <iobind/base64_policy.hpp>
#include <string>
#ifdef _UNICODE
typedef std::wstring string_t;
#else
typedef std::string string_t;
#endif
typedef std::string cstring;
extern "C" BOOL WINAPI DllMain( HMODULE hModule, DWORD dwReason, LPVOID lpReserved ) {
switch ( dwReason ){
case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls( hModule );
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
extern "C" DOEXPORT int CleanupA( char *output ) {
if ( output ) {
free( output );
output = NULL;
}
return 0;
}
extern "C" DOEXPORT int CleanupW( wchar_t *output ) {
if ( output ) {
free( output );
output = NULL;
}
return 0;
}
extern "C" DOEXPORT int printer_setup(
void *handle, const TCHAR *printer_in, const char *input,
int local_only, TCHAR **printer, char **output )
{
HWND hwnd = (HWND)handle;
HRESULT hResult = 0;
LPPRINTDLG pPD = NULL;
LPPRINTPAGERANGE pPageRanges = NULL;
// Allocate structure.
pPD = (LPPRINTDLG)GlobalAlloc(GPTR, sizeof(PRINTDLG));
if (!pPD) return E_OUTOFMEMORY;
// Initialize structure.
pPD->lStructSize = sizeof(PRINTDLG);
pPD->hwndOwner = hwnd;
pPD->hDevMode = NULL;
if ( input ){
std::string dec = iobind::encode( input, iobind::from_base64_p );
if ( !dec.empty() ) {
HGLOBAL devmode = pPD->hDevMode = ::GlobalAlloc(GPTR, dec.size());
if ( devmode ){
LPDEVMODE src = (LPDEVMODE)&dec[0];
memcpy( devmode, src, dec.size() );
}
}
}
pPD->hDevNames = NULL;
if ( printer_in ){
HGLOBAL printer = pPD->hDevNames = ::GlobalAlloc(GPTR, sizeof(DEVNAMES)+_tcslen(printer_in)*sizeof(TCHAR)+sizeof(TCHAR));
if ( printer ){
LPDEVNAMES dv = (LPDEVNAMES)printer;
dv->wDefault = 0;
dv->wDriverOffset = 0;
dv->wOutputOffset = 0;
dv->wDeviceOffset = sizeof(DEVNAMES)/sizeof(TCHAR);
TCHAR *dest = (TCHAR *)(unsigned long)dv + dv->wDeviceOffset;
_tcscpy( dest, printer_in );
}
}
pPD->hDC = NULL;
pPD->Flags = PD_PRINTSETUP;
if ( local_only ) {
pPD->Flags |= /*PD_ENABLESETUPHOOK |*/ PD_NONETWORKBUTTON;
}
pPD->nMinPage = 1;
pPD->nMaxPage = 1000;
pPD->nCopies = 1;
pPD->hInstance = 0;
pPD->lpPrintTemplateName = NULL;
// Invoke the Print property sheet.
hResult = PrintDlg(pPD);
if ( hResult != 0 ) {
if ( pPD->hDevMode ) {
LPDEVMODE devmode = (LPDEVMODE)::GlobalLock( pPD->hDevMode );
size_t size = devmode->dmSize + devmode->dmDriverExtra;
if ( output ) {
std::string tmp;
tmp.resize( size );
memcpy( &tmp[0], devmode, tmp.size() );
std::string enc = iobind::encode( tmp, iobind::to_base64_p );
*output = _strdup( enc.c_str() );
}
::GlobalUnlock( pPD->hDevMode );
}
if ( pPD->hDevNames ) {
LPDEVNAMES devnames = (LPDEVNAMES)::GlobalLock( pPD->hDevNames );
TCHAR *device = (TCHAR *)(unsigned long)devnames + devnames->wDeviceOffset;
*printer = _tcsdup(device);
::GlobalUnlock( pPD->hDevNames );
}
}
else {
DWORD dlgerr = ::CommDlgExtendedError();
hResult = dlgerr;
}
if (pPD->hDC != NULL) {
DeleteDC( pPD->hDC );
}
if (pPD->hDevMode != NULL) {
GlobalFree( pPD->hDevMode );
}
if (pPD->hDevNames != NULL) {
GlobalFree( pPD->hDevNames );
}
return hResult;
}
在 Python 中,它是这样调用的:
client = ctypes.cdll.LoadLibrary(os.path.join(myDir, 'rpmclient.dll'))
client.printer_setup.argtypes = [ctypes.c_void_p,
ctypes.c_wchar_p,
ctypes.c_char_p,
ctypes.c_int32,
ctypes.POINTER(ctypes.c_wchar_p),
ctypes.POINTER(ctypes.c_char_p)]
client.printer_setup.restype = ctypes.c_int32
client.CleanupA.argtypes = [ctypes.c_char_p]
client.CleanupA.restype = ctypes.c_int32
client.CleanupW.argtypes = [ctypes.c_wchar_p]
client.CleanupW.restype = ctypes.c_int32
p_in = ctypes.c_wchar_p(self.itemMap['device'].GetValue())
p_out = ctypes.c_wchar_p()
d_in = ctypes.c_char_p(getattr(self, 'devmode', ''))
d_out = ctypes.c_char_p()
res = client.printer_setup(self.GetHandle(),
p_in,
d_in,
False,
p_out,
d_out)
if res == 0:
return
if res > 1:
# Error display code here.
return
self.devmode = d_out.value