HighTechTalks DotNet Forums  

Unable to get PrinterSettings GetHdevmode method to work

Dotnet Framework (Drawing) microsoft.public.dotnet.framework.drawing


Discuss Unable to get PrinterSettings GetHdevmode method to work in the Dotnet Framework (Drawing) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Darrel
 
Posts: n/a

Default Unable to get PrinterSettings GetHdevmode method to work - 07-01-2003 , 10:54 AM






I am trying to use the PrinterSettings.GetHdevmode method,
but I appear to get back garbage. Below I have included
my test program showing the declaration of a DEVMODE class
and using calls to both the PrinterSettings.GetHdevmode()
method and an unmanaged call to the WIN32 API
DocumentProperties that I believe shows the DEVMODE
structure/class is correctly defined. I am hoping that I
am just doing something stupid, but at this point I can't
determine what the problem is.

My assumption was/is that if I can get the DEVMODE
structure correctly from DocumentProperties, then my
DEVMODE class has the proper "marshalling" attributes and
I should be able to likewise populate it from the DEVMODE
structure returned by PrinterSetings.GetHdevmode.
However, I only get garbage back from GetHdevmode.

If I comment out the MarshalAs attributes for the DEVMODE
fields dmDeviceName and dmFormName (which doesn't seem
like the correct thing to do, but I did as a test), then
the dmDeviceName is filled in correctly but everything
else is still garbage.

If I step through the code and display the IntPtr's
returned from GetHdevmode and DocumentProperties in a
memory window, the GetHdevmode returned IntPtr doesn't
contain any recongizable data like the IntPtr returned
from DocumentProperties (you can see the device names and
form names in the memory as Unicode strings along with the
other values).

Any help/ideas would be greatly appreciated,

Darrel



using System;
using System.Drawing.Printing;
using System.Runtime.InteropServices;

namespace DevMode
{
class Class1
{
[DllImport("winspool.drv", SetLastError=true)]
public static extern int ClosePrinter(IntPtr
printer);

[DllImport("winspool.drv",
EntryPoint="DocumentPropertiesW", ExactSpelling=true,
CharSet=CharSet.Unicode, SetLastError=true)]
public static extern int DocumentProperties(IntPtr
hWnd, IntPtr hPrinter,
string pDeviceName, IntPtr pDevModeOutput, IntPtr
pDevModeInput,
uint fMode);

[DllImport("winspool.drv", SetLastError=true)]
public static extern int OpenPrinter(string
printerName, out IntPtr hPrinter,
IntPtr defaults);

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,
}

[StructLayout(LayoutKind.Sequential,
CharSet=CharSet.Unicode)]
public class DEVMODE
{
private const int CCHDEVICENAME = 32;
private const int CCHFORMNAME = 32;

[MarshalAs(UnmanagedType.ByValTStr,
SizeConst=CCHDEVICENAME)]
public string dmDeviceName;
public ushort dmSpecVersion;
public ushort dmDriverVersion;
public ushort dmSize;
public ushort dmDriverExtra;
public uint dmFields;
// Following four fields form union with
dmPositionX & dmPositionY, which
// are not necessary for printer devices.
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=CCHFORMNAME)]
public string dmFormName;
public ushort dmLogPixels;
public uint dmBitsPerPel;
public uint dmPelsWidth;
public uint dmPelsHeight;
// The following field forms a union with
dmDsiplayFlags, which is not
// necessary for printer devices.
public uint dmNup;
public uint dmDisplayFrequency;
public uint dmICMMethod;
public uint dmICMIntent;
public uint dmMediaType;
public uint dmDitherType;
public uint dmReserved1;
public uint dmReserved2;
public uint dmPanningWidth;
public uint dmPanningHeight;

}

/// <summary>
/// The main entry point for the
application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
PrinterSettings printerSettings;
IntPtr devModePtr;
DEVMODE devMode;
string printerName = "FilePr";

printerSettings = new PrinterSettings();
printerSettings.PrinterName = printerName;
if (! printerSettings.IsValid)
{
Console.WriteLine("Printer \"{0}\" is not
configured!",
printerSettings.PrinterName);
return;
}

devModePtr = printerSettings.GetHdevmode();
devMode = (DEVMODE) Marshal.PtrToStructure
(devModePtr, typeof(DEVMODE));

Console.WriteLine("DEVMODE field values from
PrinterSettings.GetHdevmode:\n");
Console.WriteLine("\tdmDeviceName:\t{0}",
devMode.dmDeviceName);
Console.WriteLine("\tdmFormName:\t{0}",
devMode.dmFormName);
Console.WriteLine("\tdmSpecVersion:\t{0}",
devMode.dmSpecVersion);

Marshal.FreeHGlobal(devModePtr);

IntPtr hPrinter;
IntPtr win32DevModePtr;
DEVMODE win32DevMode;

if (0 == OpenPrinter(printerName, out hPrinter,
IntPtr.Zero))
{
int error = Marshal.GetLastWin32Error();
Console.WriteLine("OpenPrinter failed with
Win32 error {0}", error);
return;
}

int bytesRequired = DocumentProperties
(IntPtr.Zero, hPrinter,
printerName, IntPtr.Zero, IntPtr.Zero, 0);
Console.WriteLine("Bytes required: {0}",
bytesRequired);
win32DevModePtr = Marshal.AllocCoTaskMem
(bytesRequired);
if (0 == DocumentProperties(IntPtr.Zero,
hPrinter, printerName, win32DevModePtr,
IntPtr.Zero, (uint)
DevModeOption.DM_OUT_BUFFER))
{
int error = Marshal.GetLastWin32Error();
Console.WriteLine("DocumentProperties failed
with Win32 error {0}", error);
ClosePrinter(hPrinter);
return;
}

win32DevMode = (DEVMODE) Marshal.PtrToStructure
(win32DevModePtr, typeof(DEVMODE));
Marshal.PtrToStructure(win32DevModePtr,
win32DevMode);

Console.WriteLine("\n\nDEVMODE field values from
DocumentProperties:\n");
Console.WriteLine("\tdmDeviceName:\t{0}",
win32DevMode.dmDeviceName);
Console.WriteLine("\tdmFormName:\t{0}",
win32DevMode.dmFormName);
Console.WriteLine("\tdmSpecVersion:\t{0}",
win32DevMode.dmSpecVersion);

Marshal.FreeCoTaskMem(win32DevModePtr);

ClosePrinter(hPrinter);

return;
}
}
}


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.