PrinterCE
- C++ Installation and Usage
Source Code for PrCEDemo
The source code for the C++ PrinterCE demo program
(PrCEDemo) can be downloaded
here. This ZIP file includes full source code shared by eVC project
files that support Pocket PC, HPC/Pro and 2000. For fully-compiled ready-to-run copies of PrCEDemo, download
here. Note that you will need to download and install an evaluation
version of PrinterCE prior to using PrCEDemo.
Using PrinterCE in a C++ program
These
instructions for installing and using PrinterCE assumes that you are
familiar with the steps for creating a C++ program
Microsoft's eVC Embedded Visual Tools. The snippet sample code below uses the #import
statement to import PrinterCE's type library (which you can download from table above).
The type library provides information needed to access PrinterCE. The project then
uses a "Smart Pointer" to handle some of the complexities of
working with an ActiveX control such as PrinterCE. The function
InitPrinterCE() below looks to see if PrinterCE has been installed and
registered. If so, an instance of PrinterCE is created and your app can
begin printing.
#include "stdafx.h"
#include <Atlbase.h>
//PrCEUser.h and PrEngineCE.tlb are available for download from the table above
#import "PrEngineCE.tlb" raw_interfaces_only no_namespace
#include "PrCEUser.h" //PrinterCE defs file that defines all user constants
CComQIPtr<IPrinterCE> spPrinterCE;
/*********************************************************
InitPrinterCE() - Create
instances of PrinterCE & BarcodeCE
*********************************************************/
BOOL InitPrinterCE()
{
CLSID clsid;
HRESULT hr;
CoInitializeEx(NULL, COINIT_MULTITHREADED);
hr=CLSIDFromProgID(_T("PrEngineCE.PrinterCE"),&clsid);
if (SUCCEEDED(hr)) {
hr = spPrinterCE.CoCreateInstance(clsid);
}
if (FAILED(hr)) {
MessageBox(NULL,_T("Failed to init PrinterCE"),_T("Init
Failed..."),MB_OK);
return FALSE;
}
return TRUE;
}
/*********************************************************
TestFn() - Simple "Hello World" test function
*********************************************************/
void TestFn()
{
if (InitPrinterCE()) {
/ Make sure we have an instance
of PrinterCE - "spPrinterCE" is our instance
spPrinterCE->SelectPrinter();
//Have PrinterCE pop up Select Printer dialog box
//Send out "Hello World" at
default upper left printing position of page
spPrinterCE->DrawText(_T("Hello
World"),NULL,NULL,NULL);
spPrinterCE->EndDoc();
//Done... print out our "Hello World"
}
}
C++ Issues
There are some issues that C++ developers must deal with that are unfortunate results of ActiveX controls
being designed to work with eVB.
Function Calls
The PrinterCE documentation follows
the eVB scheme that “subroutines” (functions that do NOT return a
value) do not place parameters within parentheses, while “functions”
(which DO return a value) do use parens. Of course, C++ always uses parens.
So when the documentation defines the DrawCircle method as: “object.DrawCircle
x, y, radius, [color], [ aspect]” the
C++ programmer must convert this to : spPrinterCE->DrawCircle(x,y,radius,color,aspect);
PrinterCE Properties
The PrinterCE properties must have
“get_” or "put_” pre-appended to C++ property functions. For
example, the FontBold property is specified in the documentation as: object.FontBold
[= Boolean]. An eVB program can set this property by
PrinterCE.FontBold=True. A C++ app must call spPrinterCE->put_FontBold(VARIANT_TRUE); Similarly, to get the current X
coordinate from PrinterCE, MFC calls xCoord=spPrinterCE->get_TextX();
VARIANT BOOL
Every C++ developer knows that the
Boolean value FALSE is 0 and TRUE is 1. But in eVB, TRUE is –1
(although FALSE is still 0). C++ defines the type VT_BOOL for this
situation with the values VARIANT_TRUE and VARIANT_FALSE. All PrinterCE
functions that expect a Boolean value look for either VARIANT_TRUE or
VARIANT_FALSE. Note that PrinterCE functions are designed to work fine
with the standard TRUE/FALSE as well as VARIANT_TRUE/VARIANT_FALSE, but
any PrinterCE functions that return Boolean values will return the VARIANT
versions.
VARIANT Data Types
Certain PrinterCE functions use
“optional” parameters. For example, the DrawText method is defined as:
“object.DrawText
string, [x], [y], [count]” where x, y and
count are “optional”. In C++, these sort of optional parameters are
handled by overloading functions so that you might have several variants
of DrawText() to deal with all possible calls. eVB cannot deal with
overloaded functions, so any “optional” parameters must be passed as
VARIANT data types. (Please check the Microsoft online documentation for more
information on VARIANTs).
Basically,
a VARIANT can represent a wide variety of data types including VT_EMPTY.
When an eVB program calls a PrinterCE function without one or more of the
optional parameters, it eVB automatically passes an “empty” VARIANT
for each “omitted” parameter. A C++ program can instead pass NULL (0)
for an empty parameter and PrinterCE will work fine. But when the C++ program actually wants to pass a value for one of these optional params, a
VARIANT value must be passed. Examples of using VARIANTS are found below.
Wrapping up –
a C++ Example
An full ready-to-compile example is worth its weight in gold. The
source code for the C++ PrinterCE demo program (PrCEDemo) can be downloaded
here. This ZIP file includes full source code shared by eVC project
files that support Pocket PC, HPC/Pro and 2000 projects. For fully-compiled ready-to-run copies of PrCEDemo, download
here.