title: Bring up the printer settings dialog and have the changes saved
body: <p>This is the same as <a href="https://stackoverflow.com/questions/2437337/how-to-show-printer-properties-preferences-dialog-and-save-changes">this</a> question, with one remark:
  We manage to get the printerSettings modified, <strong>but how can we save them back as the default printer settings</strong>? (the original question does not post/answer this)</p>
  
  <p>The code I'm using right now:</p>
  
  <pre><code>&lt;DllImport("winspool.Drv", EntryPoint:="DocumentPropertiesW", SetLastError:=True, _
      ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)&gt; _
    Shared Function DocumentProperties(ByVal hwnd As IntPtr, ByVal hPrinter As IntPtr, _
        &lt;MarshalAs(UnmanagedType.LPWStr)&gt; ByVal pDeviceName As String, _
        ByVal pDevModeOutput As IntPtr, ByVal pDevModeInput As IntPtr, ByVal fMode As Integer) As Integer
  
     End Function
  
     &lt;DllImport("kernel32.dll")&gt; _
     Shared Function GlobalLock(ByVal hMem As IntPtr) As IntPtr
  
     End Function
  
     &lt;DllImport("kernel32.dll")&gt; _
     Shared Function GlobalUnlock(ByVal hMem As IntPtr) As Boolean
  
     End Function
     &lt;DllImport("kernel32.dll")&gt; _
     Shared Function GlobalFree(ByVal hMem As IntPtr) As Boolean
  
     End Function
  
     &lt;DllImport("winspool.drv")&gt; _
     Shared Function OpenPrinter(ByVal pPrinterName As String, ByRef hPrinter As IntPtr, ByVal pDefault As IntPtr) As Integer
  
     End Function
  
     &lt;DllImport("winspool.drv")&gt; _
     Private Shared Function ClosePrinter(ByVal phPrinter As IntPtr) As Integer
  
     End Function
  </code></pre>
  
  <p>(for <code>DocumentProperties</code> I've tried also making <code>pDevModeInput</code> a <code>ByRef</code> parameter)</p>
  
  <p>And inside OpenPrinterPropertiesDialog I have tried:</p>
  
  <p><strong>Variant 1:</strong></p>
  
  <pre><code>Public Sub OpenPrinterPropertiesDialog(ByVal iPrinterSettings As PrinterSettings)
        Dim hDevMode As IntPtr = iPrinterSettings.GetHdevmode()
        Dim pDevMode As IntPtr = GlobalLock(hDevMode)
        Dim sizeNeeded As Integer = DocumentProperties(mHandle, IntPtr.Zero, iPrinterSettings.PrinterName, pDevMode, pDevMode, 0)
        Dim devModeData As IntPtr = Marshal.AllocHGlobal(sizeNeeded)
        Dim fMode As Integer = 14
        Dim returnCode As Integer = DocumentProperties(mHandle, IntPtr.Zero, iPrinterSettings.PrinterName, devModeData, pDevMode, fMode)
  
        GlobalUnlock(hDevMode)
        iPrinterSettings.SetHdevmode(devModeData)
        iPrinterSettings.DefaultPageSettings.SetHdevmode(devModeData)
  
        GlobalFree(hDevMode)
        Marshal.FreeHGlobal(devModeData)
     End Sub
  </code></pre>
  
  <p><strong>Variant 2:</strong></p>
  
  <pre><code>Public Sub OpenPrinterPropertiesDialog(ByVal iPrinterSettings As PrinterSettings)
        Dim printerName As String = iPrinterSettings.PrinterName
        Dim handle As IntPtr
        OpenPrinter(printerName, handle, IntPtr.Zero)
  
        Dim hDevMode As IntPtr = iPrinterSettings.GetHdevmode(iPrinterSettings.DefaultPageSettings)
        Dim pDevMode As IntPtr = GlobalLock(hDevMode)
        Dim sizeNeeded As Integer = DocumentProperties(mHandle, IntPtr.Zero, iPrinterSettings.PrinterName, pDevMode, pDevMode, 0)
        Dim devModeData As IntPtr = Marshal.AllocHGlobal(sizeNeeded)
        Dim fMode As Integer = 14
        Dim returnCode As Integer = DocumentProperties(mHandle, IntPtr.Zero, iPrinterSettings.PrinterName, devModeData, pDevMode, fMode)
  
        GlobalUnlock(hDevMode)
        iPrinterSettings.SetHdevmode(devModeData)
        iPrinterSettings.DefaultPageSettings.SetHdevmode(devModeData)
        returnCode = DocumentProperties(IntPtr.Zero, IntPtr.Zero, iPrinterSettings.PrinterName, devModeData, pDevMode, 2)
  
        ClosePrinter(handle)
        GlobalFree(hDevMode)
        Marshal.FreeHGlobal(devModeData)
     End Sub
  </code></pre>
  
  <p><strong>Any ideas of why the changes are not saved back?</strong></p>