473,387 Members | 1,517 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,387 software developers and data experts.

SetHdevmode and printer configuration

Hello,
I need to implement a custom print dialog, and I'm trying to show a printers
configuration box, like when you click Properties on the windows forms
PrintDialog. I've gotten as far as showing the dialog. Now what do I do with
the devmode pointer? I've tried using SetHdevmode on the printer setting,
and it's default page setting, but I'm getting a System.EngineExecution
error.

Here's the declarations,
[DllImport("winspool.drv", EntryPoint="DocumentProperties",
SetLastError=true,
CallingConvention=CallingConvention.StdCall)]
private static extern int DocumentProperties(IntPtr hwnd, int hPrinter,
string pDeviceName, IntPtr pDevModeOutput, IntPtr pDevModeInput, int fMode);

[DllImport("winspool.drv", EntryPoint="OpenPrinter", SetLastError=true,
CallingConvention=CallingConvention.StdCall)]
private static extern int OpenPrinter(string pPrinterName, ref int
phPrinter, PRINTER_DEFAULTS pDefault);

[DllImport("winspool.drv", EntryPoint="ClosePrinter", SetLastError=true,
CallingConvention=CallingConvention.StdCall)]
private static extern int ClosePrinter(int hPrinter);

Here's the code to show the dialog:

string printerName = cboPrinter.PrinterName; // user selected printer
PRINTER_DEFAULTS pd = new PRINTER_DEFAULTS();
int handle = 0;
int ret = OpenPrinter(printerName, ref handle, pd);

IntPtr devOut = cboPrinter.Printer.GetHdevmode();
IntPtr devIn = cboPrinter.Printer.GetHdevmode();

int propSetRet = DocumentProperties(this.Handle, handle, printerName,
devOut, devIn, DM_IN_PROMPT | DM_OUT_BUFFER);
int closeRet = ClosePrinter(handle);

cboPrinter.Printer.DefaultPageSettings.SetHdevmode (devOut); //<-- Error here
cboPrinter.Printer.SetHdevmode(devOut);

All the API calls return 1 so that part seems to be working. So how do I
make use of the changes the user made in the printer's dialog? And is there
any way I can use the IntPtr to devOut and get a structure that I can look
at the values? I don't think I really need that for my program, but it might
help me debug.

TIA,
Eric Eggermann
Nov 15 '05 #1
3 6107
Eric,
IntPtr devOut = cboPrinter.Printer.GetHdevmode();
IntPtr devIn = cboPrinter.Printer.GetHdevmode();
GetHdevmode returns a memory object *handle*. You should use
GlobalLock to get a pointer to the DEVMODE.

I also believe you have to set CharSet=CharSet.Auto in your DllImport
attributes to get this to work.

And is there
any way I can use the IntPtr to devOut and get a structure that I can look
at the values?


Yes, you can dereference the pointer with Marshal.PtrToStructure().

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #2

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:e%****************@TK2MSFTNGP09.phx.gbl...
Eric,
IntPtr devOut = cboPrinter.Printer.GetHdevmode();
IntPtr devIn = cboPrinter.Printer.GetHdevmode();


GetHdevmode returns a memory object *handle*. You should use
GlobalLock to get a pointer to the DEVMODE.

I also believe you have to set CharSet=CharSet.Auto in your DllImport
attributes to get this to work.


Thanks Matthias,
I'm going to try that right now. But, what is the difference between the
handle and the address to the memory? What is in the handle?

And I'm sort of guessing here, but do you mean that, I should use
GetHdevmode to get handles to two devmode structures for the printer, then
use GlobalLock on them, then on success pass the returns from that to the
DocumentProperties call? Anyway, that's what I'm going to do for starters.

Thanks again,
Eric
Nov 15 '05 #3
Eric,
I'm going to try that right now. But, what is the difference between the
handle and the address to the memory? What is in the handle?
The address is just that, an address to a place in the process'
virtual memory.

The handle represents a "memory object", which is created when you
allocate movable memory with the Global or Local memory APIs. Here are
some quotes from the docs about this that may help

"GMEM_MOVEABLE
Allocates movable memory. Memory blocks are never moved in physical
memory, but they can be moved within the default heap.
The return value is a handle to the memory object. To translate the
handle into a pointer, use the GlobalLock function."

"When you allocate fixed memory, GlobalAlloc and LocalAlloc return a
pointer that the calling process can immediately use to access the
memory. When you allocate moveable memory, the return value is a
handle. To get a pointer to a movable memory object, use the
GlobalLock and LocalLock functions."

And I'm sort of guessing here, but do you mean that, I should use
GetHdevmode to get handles to two devmode structures for the printer, then
use GlobalLock on them, then on success pass the returns from that to the
DocumentProperties call? Anyway, that's what I'm going to do for starters.


Exactly. Also make sure you free (GlobalFree) devIn and devOut when
you're done, since each call to GetHdevnode returns newly allocated
memory.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #4

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

Similar topics

2
by: GMane Python | last post by:
Anyone know if there's a module which will allow me to 'create' windows printer definitions? Not from a Windows domain network, but just to add a printer that sends to a jet-direct-attached...
7
by: Steve M | last post by:
Hello, I'm having problems sending information from a python script to a printer. I was wondering if someone might send me in the right direction. I wasn't able to find much by Google TIA...
3
by: 2D Rick | last post by:
I have multiple reports that use a "specific printer" because the default printer is tied up doing other chores. This is a machine shop environment using Dells' running Win2000 and XP with...
0
by: Raul | last post by:
I need to define a papersize 8.5'', 15'. I define it using propertysettings and papersize collection in C# but the printer don't print when the datas is below to 11''. My printer is HP Laserjet...
0
by: heino | last post by:
Hello I am trying to understand how to use GetHdevmode/SetHdevmode to set public and private portions of DEVMODE structure. Is there any example in C# .Net framework that shows how to...
0
by: heino | last post by:
Hello I am trying to use GetHdevmode/SetHdevmode to set public and private portions of DEVMODE structure. Can any one post an example in C# .Net framework that shows how to change...
0
by: Tessa | last post by:
Is there any security reason why you cannot print to a network printer from ASP.NET under IIS6 on Windows 2003 server? I'm using ASP.NET code to print to a server print queue using...
1
by: =?Utf-8?B?RGF2aWQ=?= | last post by:
We have a c# application that sends documents directly to the printer using PrinterSettings() and PrintDocument(). These functions do much of the printer and document configuration work needed, but...
2
by: deric | last post by:
Hi I am trying to change the DevMode structure to change the paper source to be used by the PrintDocument. I know that there is a PaperSource provided by the PrintDocument.PrinterSettings but the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.