18.04.2015 Views

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

VISUAL C++<br />

}<br />

return TRUE;<br />

Boolean types<br />

Historically, ANSI C did not have a Boolean data type and used int value instead,<br />

where 0 represents false and nonzero represents true. However, the bool data<br />

type has now become part of ANSI C++. COM APIs are language independent<br />

and define a different Boolean type, VARIANT_BOOL. In addition, Win32 API<br />

uses a different bool type. It is important to use the correct type at the appropriate<br />

time. The following table summarizes their usage:<br />

Type True Value False Value Where Defined When to Use<br />

bool true (1) false (0)<br />

Defined by<br />

compiler<br />

BOOL (int) TRUE (1) FALSE (0) Windows Data<br />

Type (defined in<br />

windef.h)<br />

VARIANT_BOOL<br />

(16bit short)<br />

VARIANT_<br />

TRUE (-1)<br />

VARIANT_<br />

FALSE (0)<br />

COM boolean<br />

values (wtypes.h)<br />

This is an intrinsic compiler type so there is<br />

more potential for the compiler to optimize<br />

use. This type can also be promoted to<br />

an int value. Expressions (e.g., i!=0) return<br />

a type of Bool. Typically used for class<br />

member variables and local variables.<br />

Used with Windows API functions, often as<br />

a return value to indicate success or failure.<br />

Used in COM APIs for boolean values. Also<br />

used within VARIANT types, if the VARIANT<br />

type is VT_BOOL, then the VARIANT value<br />

(boolVal) is populated with a<br />

VARIANT_BOOL. Take care to convert a<br />

bool class member variable to the correct<br />

VARIANT_BOOL value. Often the<br />

conditional test "hook - colon" operator is<br />

used. For example where bRes is defined<br />

as a bool, then to set a result type:<br />

*pVal = bRes ? VARIANT_TRUE :<br />

VARIANT_FALSE;<br />

String types<br />

Considering that strings (sequences of text characters) are a simple concept, they<br />

have unfortunately become a complex and confusing topic in C++. The two main<br />

reasons for this confusion are the lack of C++ support for variable length strings<br />

combined with the requirement to support ANSI and Unicode character sets<br />

within the same code. As <strong>ArcGIS</strong> is only available on Unicode platforms, it may<br />

simplify development to remove the ANSI requirements.<br />

The C++ convention for strings is an array of characters terminated with a 0.<br />

This is not always good for performance when calculating lengths of large strings.<br />

To support variable length strings, the character arrays can be dynamically allocated<br />

and released on the heap, typically using malloc and free or new and delete.<br />

Consequently, a number of wrapper classes provide this support; CString defined<br />

in MFC and WTL is the most widely used. In addition, for COM usage the BSTR<br />

type is defined and the ATL wrapper class CComBSTR is available.<br />

To allow for international character sets, Microsoft Windows migrated from an<br />

8-bit ANSI character string (8-bit character) representation (found on<br />

Windows 95, Windows 98, and Windows Me platforms) to a 16-bit Unicode<br />

character string (16-bit unsigned short). Unicode is synonymous with wide characters<br />

(wchar_t). In COM APIs, OLECHAR is the type used and is defined to be<br />

wchar_t on Windows. Windows operating systems, such as Windows NT,<br />

Windows 2000, and Windows XP, natively support Unicode characters. To allow<br />

the same C++ code to be compiled for ANSI and Unicode platforms, compiler<br />

Chapter 4 • <strong>Developer</strong> environments • 105

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!