* AnsiCompare
현재 문자열과 파라미터로 지정한 문자열의 크기를 비교하여 결과를 되돌려 줍니다.
이때 파라미터로 지정된 문자열은 AnsiString 문자열과 비교하여 AnsiString 문자열보다 크면 0보다 큰수를 반환하고, 작을경우에는 0보다 작은수를 , 같으면 0을 반환합니다. 원형
int __fastcall AnsiCompare(const AnsiString & rhs) const;
Ex
AnsiString strCompare = "AAA";
int nReturn;
nReturn = strCompare.AnsiCompare("BBB");
if (nReturn > 0) ShowMessage("값이 큽니다.\n");
else if (nReturn > 0) ShowMessage("값이 작습니다.\n");
else ShowMessage("값이 같습니다.\n");
Result
값이 작습니다.
...............
[참고] http://bcbjournal.org/articles/vol1/9708/An_AnsiString_class_reference.htm
아래 부분 참고...
August 1997
An AnsiString class reference
by Kent Reisdorph
The AnsiString class (or String, for short) that comes with C++Builder is designed to work and play well with
VCL. However, the documentation for the String class is somewhat lacking--OK, it's terrible. We'll do our best to
remedy that situation in this article by describing the most commonly used String class operators, constructors,
and functions. (We won't deal with wide-character support functions, however.)
Constructors
The String class includes constructors to let you create a String object from different data types. Table A shows
the constructors and provides an example of each constructor's use.
Table A:String class constructors
Constructor
Description
Example
AnsiString();
Constructs an empty String object
String s;
AnsiString(
const char* src);
Constructs a String
object from a char* (a character array)
String ("This is a test");
char* buff = "Hello";
String s2(buff);
AnsiString(
const AnsiString& src);
Constructs a String object from another String object
String s1 = "String One";
String s2(s1);
AnsiString(
const char* src,
unsigned char len);
Constructs a String object from a char* with the length specified in len
String s("", 255);
AnsiString(char src);
Constructs a String object from a char data type
char c = '\n';
String
lf(c);
AnsiString(int src);
Constructs a String object from an int data type
int x = 200;
String
s(x);
AnsiString(double src);
Constructs a String object from a double data type
double d = 24.53;
String
s(d);
Operators
Table B shows the String operators and gives an example of each. These operators let you perform operations
on strings using common-sense syntax rather than relying on cryptic function names.
Table B:String class operators
Operator
Description
Example
=
Assignment
s = "Test";
+
Concatentation
s = path + filename;
==
Equality
if (s == "Hello World")...
!=
Inequality
if (s != "Logon"). . .
<
Less than
if (s < "a")...
>
Greater than
if (s > "z")...
<=
Less than or =
if (s <= "1")...
>=
Greater than or =
if (s > = "10")...
[ ]
Index
Label1->Caption = s[10];
Primary functions
The String class's many functions let you manipulate strings just about any way you choose. We'll describe the
functions you'll probably use most often with the String class.
AnsiCompare()
The AnsiCompare() function compares one string to another, using the following syntax:
int AnsiCompare(const AnsiString& rhs);
The return value will be -1 if the string is less than rhs, 1 if the string is greater than
rhs, and 0 if the string's
contents equal rhs. You don't use this function to test for equality, but rather to sort strings. For example,
String s1 = "Adam";
int result = s1.AnsiCompare("Bob");
In this case, result will be -1 because A is less than B. You can also use the < and > operators to sort strings.
AnsiCompareIC()
The AnsiCompareIC() function compares one string to another without regard to case sensitivity, using the
following syntax:
int AnsiCompareIC(const AnsiString& rhs);
AnsiLastChar()
The AnsiLastChar() function uses the syntax
char* AnsiLastChar();
to return the last character in a string. For example, the code
String s = "This is a test";
Label1->Text = s.AnsiLastChar();
displays t in the Label component.
c_str()
The c_str() function returns a pointer to the character array used to hold the string's text. It uses this syntax:
char* __fastcall c_str();
You must use this function any time you need a char pointer to the string's text.
For example, the Windows API function DrawText() requires a pointer to a character buffer. In such a case,
you'll need to use the c_str() function as follows:
The c_str() function is also handy if you need to use a String object with the C-style string manipulation
functions, such as strcpy().
Delete()
The Delete() function deletes count characters from the string beginning at index. The function's syntax is as
follows:
void Delete(int index, int count);
In this example,
String s = "This is a test";
s.Delete(1, 5);
Label1->Caption = s;
the label will display is a test, since the code deletes the first five characters of the original string. Remember
that the string indexing is 1-based, not 0-based.
Insert()
The Insert() function uses the syntax
void Insert(const AnsiString& str, int index);
to insert the string str starting at position index. For example, if you use the code
String s = "data.txt";
s.Insert("c:\\myprog\\", 1);
s will contain the text c:\myprog\data.txt after the call to Insert().
IsEmpty()
The IsEmpty() function has this syntax:
bool IsEmpty();
The function returns true if the string is empty and false if the string contains text.
IsPathDelimiter()
The IsPathDelimiter() function returns true if the character at the specified index is a backslash and false if it
isn't. The function's syntax is as follows:
bool IsPathDelimiter(int index);
IsPathDelimiter() could be useful for traversing a string containing a path and filename.
LastDelimiter()
The LastDelimiter() function uses the following syntax:
int LastDelimiter(const AnsiString& delimiters);
You can use this function to find the last character specified in delimiters.
For instance, to extract just the path from a string containing a path and filename, you could use these lines:
String s = "c:\\myprog\\data.txt";
int pos = s.LastDelimiter("\\");
s.Delete(1, pos);
This code finds the last backslash in the string and then deletes everything from the beginning of the string to
that position.
Length()
The Length() function returns the number of characters in the string. Its syntax is:
int Length();
The returned value will be the length of the text in the string, unless you've modified the length using the
SetLength() function. The returned value doesn't include a terminating null character.
For example, you could use these lines:
String s = "Hello World";
int length = s.Length();
In this case, length will contain the value 11, since there are 11 actual characters in the text string.
LowerCase()
The LowerCase() function uses the syntax
AnsiString LowerCase();
This function converts the string to lowercase and returns the converted string. (However, it doesn't modify the
text in the string object.) LowerCase() is useful for converting a string to lowercase (such as strings containing
filenames) or for comparing two strings regardless of case.
For example, in the line
if (s1.LowerCase() == s2.LowerCase()) DoSomething();
the code doesn't modify the text in the strings--it simply compares the strings' lowercase representations for
equality. If you actually want to convert a string to lowercase you'll need to use code like the following:
String s = "TEST";
s = s.LowerCase();
Pos()
The Pos() function uses the following syntax:
int Pos(const AnsiString& subStr);
It returns the position of the string passed in subStr. If the string can't be found, then Pos() returns 0. The
AnsiPos() functions perform exactly the same way.
The following code finds the position of a substring within the target string:
String s = "This is a test";
int p = s.Pos("test");
Label1->Caption = "Pos: " + String(p);
This code will display the text Pos: 11 in the label component. Again, remember that the string index is 1-based, not 0-based.
SetLength()
You can use the SetLength() function to set the length of the buffer holding the string object's text. Its syntax is
as follows:
void SetLength(int newLength);
Normally, the String class dynamically allocates space for the text in the string as needed, so there's rarely any
reason to set the length yourself. For example:
String s; // length is 0
s = "test"; // length is 4
s.SetLength(50); // length is 50
s = "new string"; // length is 10
If you set the length to a value less than the current length of the text in the string, then the string will be
truncated.
StringOfChar()
The StringOfChar() function has the following syntax:
AnsiString StringOfChar(char ch, int count);
This function creates a string filled with the character ch, with a length of count. For example, the following
code fills a string with 20 spaces:
String s;
s = s.StringOfChar(' ', 20); SubString()
The SubString() function uses this syntax:
AnsiString SubString(int index, int count);
It returns a substring of the string object starting at index and having a length of count. For example, if you
execute the code
String s = "My name is BillyBob";
String sub = s.SubString(12, 8);
then the value of sub will be BillyBob.
ToDouble()
The ToDouble() function converts a string to a double and returns the result. The syntax is simply
double ToDouble();
If the string can't be converted to a double, then VCL throws an EConvertError exception. Here's an example:
String s = "123.456";
double d = s.ToDouble();
d *= .3;
In order for the string to be converted, it must contain a valid floating-point number and no alpha characters.
ToInt()
The ToInt() function converts a string to an integer value and returns the result. The syntax is as follows:
int ToInt();
For example,
String s = "123";
int i = s.ToInt();
If the string can't be converted, then VCL throws an EConvertError exception.
ToIntDef()
The ToIntDef() function converts a string to an integer value and returns the result. Its syntax is
int ToIntDef(int defaultValue);
If the string can't be converted, then the function returns the value supplied in
defaultValue; VCL doesn't throw
an exception.
Trim(), TrimLeft(), TrimRight()
The three Trim functions trim blank spaces from a string object and return the result, using the following
syntaxes:
The Trim() function removes both leading and trailing spaces, TrimLeft() removes leading spaces, and
TrimRight() removes only trailing spaces. These functions don't modify the text in the string object.
display the string Values: 10, 20 in the label. Notice that the OPENARRAY macro passes the values to the
Format() function. This process is necessary because C++ doesn't have open arrays--however,
AnsiString::Format() calls the VCL version of Format(), which takes an open array as a parameter.
The Format() function is one of the few cases in which Pascal and C++ just don't work well together. You can
use one of two alternative methods for building the string from the previous example:
// example 1
char buff[20];
sprintf(buff, "Value: %d, %d", 10, 20);
String s = buff;
// example 2
String s = "Value: " + String(10) + ", " + String(20);
In my humble opinion, these methods are easier to use than the Format() function.
FmtLoadStr()
The FmtLoadStr() function is a combination of the Format() and LoadStr() functions. The syntax
AnsiString FmtLoadStr(int ident, const TVarRec *args, int size);
loads the string resource ident and formats it according to the value of the args parameter. See the descriptions
of Format() and LoadStr() for more information.
IsDelimiter()
The IsDelimiter() function uses this syntax:
bool IsDelimiter(const AnsiString& delimiters, int index);
The function returns true if the character at index matches one of the characters in the string delimiters. Here's
an example:
String s = "c:\\myprog\\data.txt";
if (s.IsDelimiter("\\", 3)) DoSomething();
The delimiters parameter can be a string of several delimiters you want
to test against. For instance, if you wanted to check to see whether
the character at position 10 was a space, a minus sign, a plus sign, a
comma, or a dollar sign, you'd use this code:
String s = "This is a string";
bool test = s.IsDelimiter(" -+,$", 10);
LoadStr()
The LoadStr() function loads a string resource using the following syntax:
AnsiString LoadStr(int ident);
Of course, you must have a string table resource bound to your executable program, as does the following
example:
String s;
s = s.LoadStr(100); //load string with ID of 100
Poor class design?
Note that the CurrToStr(), CurrToStrF(), FloatToStrF(), Format(), FormatFloat(),
FrmtLoadString(), and LoadStr()
functions really don't belong in the AnsiString class at all. They don't operate on the string object for which
you call them, nor do they use the string object in any way. In addition, these functions are provided as
standalone functions in the VCL Sysutils unit. It's always easier to use the standalone version of these
functions rather than the AnsiString version, as shown in these two examples:
// method 1
String s;
s = s.LoadStr(ID_STRING1);
// method 2
String s = LoadStr(ID_STRING1);
I suspect that Borland put these functions into the AnsiString class at some early point in C++Builder's
development, then forgot them later, when they were no longer necessary. Regardless, they're part of the
AnsiString class and they're useful functions, so we've listed them here.
Kent Reisdorph is a editor of the C++Builder Developer's Journal
as well as director of systems and services at TurboPower Software Company, and
a member of TeamB, Borland's volunteer online support group. He's the author of Teach
Yourself C++Builder in 21 Days and Teach Yourself C++Builder in 14 Days.
You can contact Kent at editor@bridgespublishing.com.
DLL을 작성하다 보면 DLL을 호출하는 부분이 누구인지 궁금할 때가 있다.
이럴때 DllMain 부분을 다음과 같이 작성하면 누가 호출하는지 알수가 있다..
VCL과 MFC에서 사용하는 방법이 조금 다르다... 다음은 MFC에서 사용하는 방법으로 작성하여 본 것이다..
VCL에서는 호출하는 정의가 조금 다르다.. 그래서.. DLL_PROCESS_ATTACH 부분이 조금 다르다...
An optional entry point into a dynamic-link library (DLL). When the system
starts or terminates a process or thread, it calls the entry-point function for
each loaded DLL using the first thread of the process. The system also calls the
entry-point function for a DLL when it is loaded or unloaded using the LoadLibrary and FreeLibrary functions.
Warning There are serious limits on what you can do in a
DLL entry point. To provide more complex initialization, create an
initialization routine for the DLL. You can require applications to call the
initialization routine before calling any other routines in the DLL.
Alternatively, the initialization routine can create a file with an ACL that
restricts access, and each routine in the DLL would call the initialization
routine if the file does not exist.
[in] Handle to the DLL module. The value is the base address of the DLL. The
HINSTANCE of a DLL is the same as the HMODULE of the DLL, so
hinstDLL can be used in calls to functions that require a module handle.
fdwReason
[in] Indicates why the DLL entry-point function is being called. This
parameter can be one of the following values.
Value
Meaning
DLL_PROCESS_ATTACH
1
The DLL is being loaded into the virtual address space of the
current process as a result of the process starting up or as a result of a call
to LoadLibrary. DLLs can use this opportunity to initialize any instance
data or to use the TlsAlloc function to
allocate a thread local storage (TLS) index.
DLL_THREAD_ATTACH
2
The current process is creating a new thread. When this occurs,
the system calls the entry-point function of all DLLs currently attached to the
process. The call is made in the context of the new thread. DLLs can use this
opportunity to initialize a TLS slot for the thread. A thread calling the DLL
entry-point function with DLL_PROCESS_ATTACH does not call the DLL entry-point
function with DLL_THREAD_ATTACH.
Note that a DLL's entry-point function is called with this value only by
threads created after the DLL is loaded by the process. When a DLL is loaded
using LoadLibrary, existing threads do not call the entry-point function
of the newly loaded DLL.
DLL_THREAD_DETACH
3
A thread is exiting cleanly. If the DLL has stored a pointer to
allocated memory in a TLS slot, it should use this opportunity to free the
memory. The system calls the entry-point function of all currently loaded DLLs
with this value. The call is made in the context of the exiting
thread.
DLL_PROCESS_DETACH
0
The DLL is being unloaded from the virtual address space of the
calling process as a result of unsuccessfully loading the DLL, termination of
the process, or a call to FreeLibrary. The
DLL can use this opportunity to call the TlsFree function to free any TLS indices allocated
by using TlsAlloc and to free any thread local data.
Note that the thread that receives the DLL_PROCESS_DETACH notification is not
necessarily the same thread that received the DLL_PROCESS_ATTACH
notification.
lpvReserved
[in] If fdwReason is DLL_PROCESS_ATTACH, lpvReserved is NULL
for dynamic loads and non-NULL for static loads.
If fdwReason is DLL_PROCESS_DETACH, lpvReserved is NULL if
FreeLibrary has been called or the DLL load failed and non-NULL if the
process is terminating.
Return Value
When the system calls the DllMain function with the DLL_PROCESS_ATTACH
value, the function returns TRUE if it succeeds or FALSE if initialization
fails. If the return value is FALSE when DllMain is called because the
process uses the LoadLibrary function, LoadLibrary returns NULL.
(The system immediately calls your entry-point function with DLL_PROCESS_DETACH
and unloads the DLL.) If the return value is FALSE when DllMain is called
during process initialization, the process terminates with an error. To get
extended error information, call GetLastError.
When the system calls the DllMain function with any value other than
DLL_PROCESS_ATTACH, the return value is ignored.
Remarks
DllMain is a placeholder for the library-defined function name. You
must specify the actual name you use when you build your DLL. For more
information, see the documentation included with your development tools.
During initial process startup or after a call to LoadLibrary, the
system scans the list of loaded DLLs for the process. For each DLL that has not
already been called with the DLL_PROCESS_ATTACH value, the system calls the
DLL's entry-point function. This call is made in the context of the thread that
caused the process address space to change, such as the primary thread of the
process or the thread that called LoadLibrary. Access to the entry point
is serialized by the system on a process-wide basis. Threads in DllMain
hold the loader lock so no additional DLLs can be dynamically loaded or
initialized.
If the DLL's entry-point function returns FALSE following a
DLL_PROCESS_ATTACH notification, it receives a DLL_PROCESS_DETACH notification
and the DLL is unloaded immediately. However, if the DLL_PROCESS_ATTACH code
throws an exception, the entry-point function will not receive the
DLL_PROCESS_DETACH notification.
There are cases in which the entry-point function is called for a terminating
thread even if the entry-point function was never called with DLL_THREAD_ATTACH
for the thread:
The thread was the initial thread in the process, so the system called the
entry-point function with the DLL_PROCESS_ATTACH value.
The thread was already running when a call to the LoadLibrary
function was made, so the system never called the entry-point function for it.
When a DLL is unloaded from a process as a result of an unsuccessful load of
the DLL, termination of the process, or a call to FreeLibrary, the system does not call the
DLL's entry-point function with the DLL_THREAD_DETACH value for the individual
threads of the process. The DLL is only sent a DLL_PROCESS_DETACH notification.
DLLs can take this opportunity to clean up all resources for all threads known
to the DLL.
If you terminate a process by calling TerminateProcess or TerminateJobObject, the DLLs of that
process do not receive DLL_PROCESS_DETACH notifications. If you terminate a
thread by calling TerminateThread, the
DLLs of that thread do not receive DLL_THREAD_DETACH notifications.
The entry-point function should perform only simple initialization or
termination tasks. It must not call the LoadLibrary or LoadLibraryEx function (or a function that
calls these functions), because this may create dependency loops in the DLL load
order. This can result in a DLL being used before the system has executed its
initialization code. Similarly, the entry-point function must not call the FreeLibrary function (or a function that calls
FreeLibrary) during process termination, because this can result in a DLL
being used after the system has executed its termination code.
Because Kernel32.dll is guaranteed to be loaded in the process address space
when the entry-point function is called, calling functions in Kernel32.dll does
not result in the DLL being used before its initialization code has been
executed. Therefore, the entry-point function can call functions in Kernel32.dll
that do not load other DLLs. For example, DllMain can create synchronization objects such as critical
sections and mutexes, and use TLS. Unfortunately, there is not a comprehensive
list of safe functions in Kernel32.dll.
Windows 2000: Do not create a named synchronization object
in DllMain because the system will then load an additional DLL. This
restriction does not apply to subsequent versions of Windows.
Calling functions that require DLLs other than Kernel32.dll may result in
problems that are difficult to diagnose. For example, calling User, Shell, and
COM functions can cause access violation errors, because some functions load
other system components. Conversely, calling functions such as these during
termination can cause access violation errors because the corresponding
component may already have been unloaded or uninitialized.
Because DLL notifications are serialized, entry-point functions should not
attempt to communicate with other threads or processes. Deadlocks may occur as a
result.
If your DLL is linked with the C run-time library (CRT), the entry point
provided by the CRT calls the constructors and destructors for global and static
C++ objects. Therefore, these restrictions for DllMain also apply to
constructors and destructors and any code that is called from them.
요약:
When trying to creating a simple C++ designtime package that might
refer to ‘DesignIntf’,’DesignEditors, the Delphi compiler complains
that 'DesignIntf.dcu' and 'Proxies.dcu'cannot be found.
When trying to compile a C++ package that
refers to DesignIntf, DesignEditors in say, a pascal unit you might see
errors as follows:
[DCC Error] XXXX.pas(X): F1026 File not found: 'DesignIntf.dcu'
Or if you add the DesignEditors.pas location to the project’s "search path", the error is:
[DCC Error] DesignEditors.pas(609): F1026 File not found:'Proxies.dcu'
This issue is documented in readme for BDS2006. This is the note from BDS 2006's readme file:
C++ Notes If you are creating a C++ Package that contains a Delphi unit thatuses the DesignIntf and DesignEditors tools, you must choose Project|Options|Pascal Compiler|Other Options and then add "-LUDesignIDE" for the compiler to work correctly.
So in CB2007/RAD Studio 2007, all you need to do is:
Project|Options|Pascal Compiler|Other Options|"Use these packages whencompiling", then add "DesignIDE" in the combobox.