473,396 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

DocumentProperties API

Hello csharp professionlals.
I have a problem: my application has to handle with DocumentProperties
API. The application is coded in C#.
It works fine, the DocumentProperties appears, after that the devmode
Structure seems corectly updated, but at last DocumentProperties call
(when it should update the setings), the DEVMODE structure has the
settings from the initial call (seems that DocumentProperties discards
the changes).
Please, if u could tell me what's wrong...
I will post the code too.
this is the class that handles the function ShowDocProps that should
change the document properties:
using System;
using System.Runtime.InteropServices;
using System.Drawing.Printing;
namespace Drag_drop
{
/// <summary>
/// Summary description for CPrinter.
/// </summary>
///
unsafe struct PRINTER_DEFAULTS
{
public void * pDatatype; // LPTSTR
public void * pDevMode; // LPDEVMODE
public uint DesiredAccess; // ACCESS_MASK
};


public enum DevModeOption
{
DM_UPDATE = 1,
DM_COPY = 2,
DM_PROMPT = 4,
DM_MODIFY = 8,
DM_IN_BUFFER = DM_MODIFY,
DM_IN_PROMPT = DM_PROMPT,
DM_OUT_BUFFER = DM_COPY,
DM_OUT_DEFAULT = DM_UPDATE,
}
public class CPrinter
{
PrintDocument pdoc = new PrintDocument();

#region DEVMODE
private const short CCDEVICENAME = 32;
private const short CCFORMNAME = 32;

[StructLayout(LayoutKind.Sequential)]
public struct DEVMODE
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCDEVICENAME)]
public string dmDeviceName;

public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public short dmOrientation;
public short dmPaperSize;
public short dmPaperLength;
public short dmPaperWidth;
public short dmScale;
public short dmCopies;
public short dmDefaultSource;
public short dmPrintQuality;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCFORMNAME)]
public string dmFormName;

public short dmUnusedPadding;
public short dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
}
#endregion
#region dllimport

[DllImport("kernel32.dll")]
private static extern IntPtr GlobalLock(IntPtr hMem);
[DllImport("kernel32.dll")]
private static extern bool GlobalUnlock(IntPtr hMem);
[DllImport("kernel32.dll")]
private static extern UIntPtr GlobalSize(IntPtr hMem);

[DllImport("winspool.Drv", EntryPoint="GetPrinterA",
SetLastError=true, CharSet=CharSet.Ansi,ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern bool GetPrinter(int hPrinter, Int32 dwLevel,
IntPtr pPrinter, Int32 dwBuf, out Int32 dwNeeded);

[DllImport("kernel32", SetLastError=true)]
static extern int GetLastError();

[DllImport("WinSpool.drv", SetLastError=true)]
static extern unsafe bool OpenPrinter
(string pPrinterName, int * phPrinter, void * pDefault);

[DllImport("WinSpool.drv", SetLastError=true)]
static extern bool ClosePrinter(int hPrinter);

[DllImport("WinSpool.drv",SetLastError=true)]
static extern unsafe bool SetPrinter
(int hPrinter, uint Level, void * pPrinter, uint Command);
[DllImport("WinSpool.drv",SetLastError=true)]
static extern unsafe bool PrinterProperties
(IntPtr hWnd, int hPrinter);
[DllImport("WinSpool.drv",SetLastError=true)]
static extern unsafe int DocumentProperties
(IntPtr hWnd, int hPrinter, string pDeviceName,
IntPtr pDevModeOutput, IntPtr pDevModeInput,
int fMode );

#endregion

#region ForUnsafeTypes
const int STANDARD_RIGHTS_REQUIRED = 0xF0000;
const uint PRINTER_ACCESS_USE = 0x00000008;
const uint PRINTER_ACCESS_ADMINISTER = 0x00000004;
const uint PRINTER_STATUS_OFFLINE = 0x00000080;
const uint PRINTER_CONTROL_PAUSE = 1;
const uint PRINTER_CONTROL_RESUME = 2;
const uint PRINTER_CONTROL_PURGE = 3;
const uint PRINTER_CONTROL_SET_STATUS = 4;

#endregion

private Form1 owner;
public CPrinter( Form1 frm )
{
this.owner = frm;
//
// TODO: Add constructor logic here
//
}
public static unsafe int CSOpenPrinter(string printerName)
{
bool bResult;
int hPrinter2;
PRINTER_DEFAULTS pd;

pd.pDatatype = null;
pd.pDevMode = null;
pd.DesiredAccess = PRINTER_ACCESS_USE;

bResult = OpenPrinter(printerName, &hPrinter2, &pd);
if (!bResult)
{
throw new ApplicationException("Cannot open printer '" +
printerName + "' " +
Marshal.GetLastWin32Error());
}
return hPrinter2;
}
// Close the printer.
static void CSClosePrinter(int hPrinter)
{
if (!ClosePrinter(hPrinter))
{
throw new ApplicationException("Cannot close printer " +
Marshal.GetLastWin32Error());
}
}
public unsafe void ShowDocProps(string PrinterName)
{
DEVMODE dm = new DEVMODE();
Int32 level = 2;
Int32 Needed = 0;

IntPtr pPrinter = new IntPtr(0);
PRINTER_INFO_2 pi = new PRINTER_INFO_2();

PRINTER_DEFAULTS pd;
IntPtr iOldDev = pdoc.PrinterSettings.GetHdevmode();
pd.pDatatype = null;
pd.pDevMode = null;
pd.DesiredAccess = STANDARD_RIGHTS_REQUIRED;

iOldDev = pdoc.PrinterSettings.GetHdevmode();

int hPrinter;
PRINTER_INFO_2 pLocal = new PRINTER_INFO_2();
if( OpenPrinter( PrinterName, &hPrinter, &pd ) )
{
// Get printer level page size needed - first call
if (GetPrinter(hPrinter, level, pPrinter, 0, out Needed) == false)
{
IntPtr pBytes = Marshal.AllocCoTaskMem(Needed); // Alloc
GetPrinter(hPrinter, level, pBytes, Needed, out Needed);
Marshal.PtrToStructure(pBytes, pi);
IntPtr iDevModeNew = GlobalLock(iOldDev);
int xSet = 0;

int x = DocumentProperties(owner.Handle, hPrinter, PrinterName,
iDevModeNew, iDevModeNew, (int)DevModeOption.DM_OUT_BUFFER
|(int)DevModeOption.DM_PROMPT);
DEVMODE d2 = (DEVMODE) Marshal.PtrToStructure(iDevModeNew,
typeof(DEVMODE));
if (x != 0)
{
//iDevModeNew = IntPtr.Zero;
Marshal.StructureToPtr(d2, iDevModeNew, true);
d2 = (DEVMODE) Marshal.PtrToStructure(iDevModeNew, typeof(DEVMODE));
pdoc.PrinterSettings.SetHdevmode(iDevModeNew);
x = DocumentProperties(IntPtr.Zero, hPrinter, PrinterName,
iDevModeNew, iDevModeNew, (int)DevModeOption.DM_IN_BUFFER |
(int)DevModeOption.DM_OUT_BUFFER);
//it seems that the changes are discarded
d2 = (DEVMODE) Marshal.PtrToStructure(iDevModeNew, typeof(DEVMODE));

}
GlobalUnlock(iOldDev);
CSClosePrinter(hPrinter);

// Marshal.FreeCoTaskMem(iOldDev);
}
}

}

}
}
Nov 16 '05 #1
0 2146

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michael TEpperis | last post by:
hello, my xml files starts with +++++++ <?xml version='1.0' encoding='utf-8' ?> <?integrity app='Visio' version='10.0' buildnum='525' metric='1'...
4
by: Umesh | last post by:
Hi all I am trying to change the printer(like paper source ,etc) setting in C#. But i am unable to do it. I am not sure how to do it. should i have to use WIN API like Openprinter , getprinter ,...
0
by: sparton | last post by:
Good day!! Is there a way to retrieve values / changes from documentproperties? documentproperties is the screen is shown when you press 'properties' on the 'Print' window. This can be derived from...
6
by: Stephan Perschke | last post by:
Hi, I try to change some printer settings using the win32 api OpenPrinter, GetPrinter and SetPrinter. I copied most of the code from this or related newsgroups. However, OpenPrinter, GetPrinter...
10
by: Joe M | last post by:
I was wondering if someone could lend me a hand with a C# problem I am having I am trying to use the “setPrinter” api to change the duplex setting (under printing preferences on printer...
0
by: Benoit Courchesne | last post by:
Hi, I'm trying to build my custom Printer Dialog box. The following code is supposed to Pop up the printer properties form and edit the PrinterSettings. My problem is that I'm able to show...
0
by: sparton | last post by:
good day!! is there a way to retrieve values from documentproperties in winspool.drv?? sample values are the number of pages, and orientation.. many thanks!!!
2
by: Flavio Zanellato | last post by:
Hello, I've a problem calling the above function in VB.NET : nSize = DocumentProperties(hWnd, hPrinter, szPrinterName, oDevMode, pDevMode, DM_OUT_BUFFER Or DM_PROMPT) I seems that the...
0
by: Calum | last post by:
I have created a custom print dialog and its fully working expcept for the printer preferences. The code below displays the printer preferences dialog for the selected printer fine, the devmode...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.