We'll begin by writing a program with a dedicated dynamic link library and see how they work together. The program is called STRPROG (”string program“), and the dynamic link library is called STRLIB (”string library“). STRLIB has three exported functions that STRPROG calls. Just to make this interesting (and to force you to think about some of the implications), one of the functions in STRLIB uses a call-back function defined in STRPROG.
STRLIB is a dynamic link library module that stores and sorts up to 256 character strings. The strings are capitalized and stored in STRLIB's own data segment. STRPROG can use STRLIB's three functions to add strings, delete strings, and obtain all the current strings from STRLIB. The program has two menu items (Enter and Delete) that invoke dialog boxes to add and delete these strings. STRPROG lists all the current strings stored in STRLIB's data segment in STRPROG's client area.
This function defined in STRLIB adds a string to STRLIB's data segment:
BOOL FAR PASCAL AddString (lpStringIn)
The parameter lpString is a far pointer to the string. The string is capitalized within the AddString function. If an identical string already exists in STRLIB's data segment, this function adds another copy of the string. AddString returns TRUE (nonzero) if it is successful and FALSE (0) otherwise. A FALSE return value can result if the string has a length of 0, if memory could not be allocated to store the string, or if 256 strings are already stored.
This STRLIB function deletes a string from STRLIB's data segment:
BOOL FAR PASCAL DeleteString (lpStringIn)
Again, the parameter lpString is a far pointer to the string. If more than one string matches, only the first is removed. DeleteString returns TRUE (nonzero) if it is successful and FALSE (0) otherwise. A FALSE return value indicates that the length of the string is 0 or that a matching string could not be found.
This STRLIB function uses a call-back function located in the calling program to enumerate the strings currently stored in STRLIB's data segment:
short FAR PASCAL GetStrings (lpfnGetStrCallBack, lpParam)
The call-back function must be defined as follows:
BOOL FAR PASCAL GetStrCallBack (LPSTR lpString, LPSTR lpParam)
The GetStrCallBack function must be exported from the program that calls GetStrings. The lpfnGetStrCallBack parameter to GetStrings must be obtained from MakeProcInstance. GetStrings calls GetStrCallBack once for each string or until the call-back function returns FALSE (0). GetStrings returns the number of strings passed to the call-back function. The lpParam parameter is a far pointer to programmer-defined data. Note that all the pointers passed as function parameters are far pointers, because STRLIB must reference data in the caller's data segment.