Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6989037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:09:36+00:00 2026-05-27T19:09:36+00:00

So far I’ve found two different ways to access what I believe are equivalent

  • 0

So far I’ve found two different ways to access what I believe are equivalent versions of the Printer DevMode from a wxPython User Interface:

window = wx.GetTopLevelWindows()[0].GetHandle()
name = self.itemMap['device'].GetValue() # returns a valid printer name.
handle = win32print.OpenPrinter(name)
dmin = None
dmout = pywintypes.DEVMODEType()
mode = DM_IN_BUFFER | DM_OUT_BUFFER | DM_IN_PROMPT

res = win32print.DocumentProperties(window, handle, name, dmout, dmin, mode)

if res == 1:
  print dmout.DriverData

and also

dlg = wx.PrintDialog(self, dgData)

res = dlg.ShowModal()

if res == wx.ID_OK:
  print dlg.GetPrintDialogData().PrintData.GetPrivData()

These to binary structures appear to contain the necessary information to control the device output behavior. This is fine and well, except that it can’t be directly used to reload the PrintSetup dialogs with this stored devmode data. In the first case the PyDEVMODE object contains dozens of individual properties that need to be manually set (PyDEVMODE Reference). In the second case there are a handful of Getter / Setter methods that control some of the properties, but not all of them (wxPrintData Reference). Is anyone aware of a way to create a Python Devmode Object (I’ll take either approach, the differences are trivial) from the actual DevMode (the binary data)? I’d like to avoid having to manually store / reset each individual attribute in order for the dialogs to re-open in the correct state every time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:09:37+00:00Added an answer on May 27, 2026 at 7:09 pm

    It appears that at this point there’s no elegant way to achieve this directly in Python. The closest I was able to come up with was a dll written in c++ that I’m able to call into. I end up with the full binary DevMode Structure, and can reload from it. The code for that c++ dll looks like this:

    #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;
    }
    

    In Python, it’s called into like so:

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So far I only found two ways: throw exception play nothing ad.2. Pretty recent
So far I know two ways to pass php variables to javascript. One is
As far as I know (which is very little) , there are two ways,
So far I found how to do it in Chrome, the DOMSubtreeModified event: Is
So far i wrote a code to download a file from ftp server then
Far I've found that giving the iframe a background colour will alter the timeline
So far i got this code: function toplist() {$sql = SELECT * FROM list
As far as I can tell, these two pieces of javascript behave the same
Far as best practices are concerned, which is better: public void SomeMethod(string str) {
As far as I know, foreign keys (FK) are used to aid the programmer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.