A
Andrew Hatch
Can someone tell me why SetHdevmode doesn't work here?
I have no problem displaying the printer's property dialogs, and if I
alter copies or landscape options, I see the change reflected in the
unmanaged memory I allocated. However, using SetHdevmode to change
the settings in PrintDocument is not working. As far as I can tell,
SetHdevmode is expecting a handle to unmanaged memory, which is what
I'm providing here.
Do I need to use Marshal:trToStructure somehow?
I'm posting this to framework and framework.interop since I'm unclear
whether the problem is with interop or with PrinterSettings class.
Thanks much,
Andrew
----------------------
// C++ example code
void TestPrinterProperties()
{
HANDLE printerHandle;
HGLOBAL hMem;
PDEVMODE pDm;
PrintDocument* pDoc = new PrintDocument();
LONG docPropertiesResult;
if (OpenPrinter("\\\\SHMIFS1\\lw51", &printerHandle, NULL))
{
// Figure out size to allocate
docPropertiesResult = DocumentProperties(NULL, printerHandle,
"\\\\SHMIFS1\\lw51", NULL, NULL, 0);
hMem = GlobalAlloc(GPTR, docPropertiesResult);
// Get current printer settings
docPropertiesResult = DocumentProperties(NULL, printerHandle,
"\\\\SHMIFS1\\lw51", (PDEVMODE)hMem, (PDEVMODE)hMem,
DM_OUT_BUFFER);
pDm = (PDEVMODE)hMem;
Console::WriteLine("Before DEVMODE: copies: {0}",
pDm->dmCopies.ToString());
Console::WriteLine("Before DEVMODE: landscape: {0}",
pDm->dmOrientation.ToString());
// Allow user to change printer settings via this this
// printer's property pages.
docPropertiesResult = DocumentProperties(NULL, printerHandle,
"\\\\SHMIFS1\\lw51", (PDEVMODE)hMem, (PDEVMODE)hMem,
DM_IN_BUFFER | DM_IN_PROMPT | DM_OUT_BUFFER);
// Verify that changes made in the dialog are made in pDm
Console::WriteLine("After DEVMODE: copies: {0}",
pDm->dmCopies.ToString());
Console::WriteLine("After DEVMODE: landscape: {0}",
pDm->dmOrientation.ToString());
if (docPropertiesResult >= 0)
{
// Try to make the settings into PrintDocument
// This results in incorrect data.
pDoc->PrinterSettings->SetHdevmode((IntPtr)hMem);
pDoc->DefaultPageSettings->SetHdevmode((IntPtr)hMem);
Console::WriteLine("PrintDocument copies: {0}",
pDoc->PrinterSettings->Copies.Landscape.ToString());
Console::WriteLine("PrintDocument landscape: {0}",
pDoc->DefaultPageSettings->Landscape.ToString());
}
GlobalFree(hMem);
ClosePrinter(printerHandle);
}
}
I have no problem displaying the printer's property dialogs, and if I
alter copies or landscape options, I see the change reflected in the
unmanaged memory I allocated. However, using SetHdevmode to change
the settings in PrintDocument is not working. As far as I can tell,
SetHdevmode is expecting a handle to unmanaged memory, which is what
I'm providing here.
Do I need to use Marshal:trToStructure somehow?
I'm posting this to framework and framework.interop since I'm unclear
whether the problem is with interop or with PrinterSettings class.
Thanks much,
Andrew
----------------------
// C++ example code
void TestPrinterProperties()
{
HANDLE printerHandle;
HGLOBAL hMem;
PDEVMODE pDm;
PrintDocument* pDoc = new PrintDocument();
LONG docPropertiesResult;
if (OpenPrinter("\\\\SHMIFS1\\lw51", &printerHandle, NULL))
{
// Figure out size to allocate
docPropertiesResult = DocumentProperties(NULL, printerHandle,
"\\\\SHMIFS1\\lw51", NULL, NULL, 0);
hMem = GlobalAlloc(GPTR, docPropertiesResult);
// Get current printer settings
docPropertiesResult = DocumentProperties(NULL, printerHandle,
"\\\\SHMIFS1\\lw51", (PDEVMODE)hMem, (PDEVMODE)hMem,
DM_OUT_BUFFER);
pDm = (PDEVMODE)hMem;
Console::WriteLine("Before DEVMODE: copies: {0}",
pDm->dmCopies.ToString());
Console::WriteLine("Before DEVMODE: landscape: {0}",
pDm->dmOrientation.ToString());
// Allow user to change printer settings via this this
// printer's property pages.
docPropertiesResult = DocumentProperties(NULL, printerHandle,
"\\\\SHMIFS1\\lw51", (PDEVMODE)hMem, (PDEVMODE)hMem,
DM_IN_BUFFER | DM_IN_PROMPT | DM_OUT_BUFFER);
// Verify that changes made in the dialog are made in pDm
Console::WriteLine("After DEVMODE: copies: {0}",
pDm->dmCopies.ToString());
Console::WriteLine("After DEVMODE: landscape: {0}",
pDm->dmOrientation.ToString());
if (docPropertiesResult >= 0)
{
// Try to make the settings into PrintDocument
// This results in incorrect data.
pDoc->PrinterSettings->SetHdevmode((IntPtr)hMem);
pDoc->DefaultPageSettings->SetHdevmode((IntPtr)hMem);
Console::WriteLine("PrintDocument copies: {0}",
pDoc->PrinterSettings->Copies.Landscape.ToString());
Console::WriteLine("PrintDocument landscape: {0}",
pDoc->DefaultPageSettings->Landscape.ToString());
}
GlobalFree(hMem);
ClosePrinter(printerHandle);
}
}