🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #1679

Re: More on Dialogs

#1679
From: Remy Lebeau (TeamB)
Date: Thu, 28 Jun 2007 23:07
62 lines
3258 bytes
"JD" <nospam@nospam.com> wrote in message
news:46842a5a@newsgroups.borland.com...

>           Printer()->GetPrinter(device, driver, port, (int)HMem);

That is retreiving the current information for the printer specified
by the TPrinter::PrinterIndex property.

However, the last paramter is wrong.  The HMem variable is declared as
a HANDLE.  GetPrinter() expects a reference to an int instead.  By
casting a HANDLE variable to an int like that, it is actually passing
a temporary variable to GetPrinter().  The compiler even warns as
much.  So GetPrinter() is going to assign a value to that temporary
instead of to the original HMem variable.  HMem will remain
uninitialized, which will be very bad when that variable is passed to
GlobalFree() later on.

>           Printer()->SetPrinter(device, driver, port,
(int)psd.hDevMode);

That is telling TPrinter to use the same printer device that it was
already set to, rather than using the actual printer that was selected
in the dialog.  So the code is basically just updating the
TPrinter::Capabilities property without updating the
TPrinter::PrinterIndex property.

>           GlobalFree(HMem);

That is undefined behavior since HMem is uninitialized.  However, even
if HMem had been filled in properly, that line would still be
problematic.  It is freeing the DEVMODE that TPrinter::SetPrinter()
had already freed internally.

> that wasn't working until I added setting the PrinterIndex.

It wasn't working because it was using TPrinter incorrectly to begin
with.  The code should look more like the following:

    if( ::PageSetupDlg(&psd) )
    {
        DEVNAMES *pDevNames = (DEVNAMES*) GlobalLock(psd.hDevNames);

        char *pDriver = ((char*)pDevNames) + pDevNames->wDriverOffset;
        char *pDevice = ((char*)pDevNames) + pDevNames->wDeviceOffset;
        char *pPort = ((char*)pDevNames) + pDevNames->wOutputOffset;

        Printer()->SetPrinter(pDevice, pDriver, pPort,
(int)psd.hDevMode);
        GlobalUnlock(psd.hDevNames);
        GlobalFree(psd.hDevNames);

        // do some printing...
    }

> The docs for DEVNAMES wDeviceOffset says (in part):

You are looking at old documentation  There is no size limitation
mentioned in the current documentation.


Gambit
Technical Details
Message-ID: <46844223$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <46825ff3@newsgroups.borland.com> <46829422$1@newsgroups.borland.com> <46836c01@newsgroups.borland.com> <46837feb$1@newsgroups.borland.com> <46842a5a@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,964b3d9a83e56201
X-Google-Attributes: gid10cc67,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <46825ff3@newsgroups.borland.com> <46829422$1@newsgroups.borland.com> <46836c01@newsgroups.borland.com> <46837feb$1@newsgroups.borland.com> <46842a5a@newsgroups.borland.com>
Subject: Re: More on Dialogs
Date: Thu, 28 Jun 2007 16:07:22 -0700
Lines: 64
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 75.82.252.126
Message-ID: <46844223$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1183072803 75.82.252.126 (28 Jun 2007 16:20:03 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail