🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: More on Dialogs

#1672
From: Remy Lebeau (TeamB)
Date: Thu, 28 Jun 2007 09:23
106 lines
4814 bytes
"JD" <nospam@nospam.com> wrote in message
news:46836c01@newsgroups.borland.com...

> It is my current understanding that one must first set the
> PrinterIndex before one calls SetPrinter.

Then your understanding has been misled.  TPrinter::SetPrinter()
assigns the PrinterIndex (amongst other things) based on the
structures that are passed to it.  The current TPrinter::PrinterIndex
value has no effect whatsoever on TPrinter::SetPrinter().

> Simple enough if one uses Printers::IndexOf method but
> that will only work if the string is an exact match

TPrinter::SetPrinter() performs a comparison of the original printer
information that is provided by the OS.  It does not perform a
comparison of the string values that are stored (and potentially
reformatted) in the TPrinter::Printers list.  On the other hand, the
TPrinter::Printers list is a plain TStringList, and thus its IndexOf()
method does an exact string match on the TStringList values only, not
the original printer information.  They are not the same search
operation.

> Now, the docs for DEVNAMES says it's 32 bytes and it
> must match DEVMODE exactly but I just found this in the
> docs for PRINTDLGEX:

The VCL dialogs do not use PRINTDLGEX.

>     Note that the dmDeviceName member of the DEVMODE structure
>     also specifies a printer name. However, dmDeviceName is
>     limited to 32 characters, and the wDeviceOffset name is
>     not.
>
> Obviousely there's an error there

There is not.  The dmDeviceName member will contain the truncated copy
of a name if the original is longer than 32 characters.  Your original
post in this thread even quoted as much:

    "Note that this name may be truncated to fit in the dmDeviceName
array."

The wDeviceOffset name, on the other hand, will be the full name.

> I assume that the error is that dmDeviceName and the string pointed
> to by wDeviceOffset need not match exactly

Yes, they do match - but only up to 32 characters.  But TPrinter never
uses dmDeviceName to begin with, so it should never be limited to 32
characters.

> so I can use wDeviceOffset with IndexOf.

Only on NT-based systems.  On Win9x-based systems, the
TPrinter::Printers list does NOT reflect the wDeviceOffset name by
itself.  On those systems, TPrinter::SetPrinter() merges the device
and port names together into a single string, and then stores that
into the TPrinters::Printers list.  That entire string is what
IndexOf() will be comparing.  You are not taking that into account
when you use IndexOf().  If you really want to know the index of a
given printer name regardless of the OS version, then you would have
to ignore IndexOf() altogether and do your own substring scanning of
the list manually, ie:

    #include <StrUtils.hpp>

    AnsiString device = ((char*)pDevNames) + pDevNames->wDeviceOffset;
    TStrings *list = Printer()->Printers;
    int index = -1;

    for(int i = 0; i < list->Count; ++i)
    {
        if( AnsiStartsText(device, list->Strings[i]) )
        {
            index = i;
            break;
        }
    }

> I just don't have a printer installed that's longer than 32
> bytes to test it. Is there a way for me to lengthen a printer's
> name?

Have you tried simply renaming it in the Control Panel's Printers
applet?  Depending on your OS version, it may or may not allow you to
do that directly.

> Don't I need to dispose of the TPrinter's current DEVMODE?

No.  That is handled automatically inside of TPrinter::SetPrinter().

>           Printer()->PrinterIndex = Printer()->Printers->IndexOf(
> (char*)pDevNames + pDevNames->wDeviceOffset );

Use SetPrinter() instead of assigning PrinterIndex manually.  Let
TPrinter do all the work for you.

>           GlobalFree( (void*)hDevMode );

DO NOT DO THAT!!!! You are freeing memory that TPrinter owns and uses
internally.


Gambit
Technical Details
Message-ID: <46837feb$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>

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>
Subject: Re: More on Dialogs
Date: Thu, 28 Jun 2007 02:23:22 -0700
Lines: 108
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: <46837feb$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1183023083 75.82.252.126 (28 Jun 2007 02:31:23 -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